Each shard sends its top 2 searches. Is the merge right?
Question
A shop shows "trending searches today". Search logs are split across three servers, or shards, and each server counts only the searches it saw. To build the top 2, a service asks each server for its own top 2 and adds up the counts.
Today's counts:
| server A | server B | server C |
|---|---|---|
| shoes 60 | tent 58 | socks 62 |
| jacket 50 | boots 52 | gloves 51 |
| umbrella 45 | umbrella 45 | umbrella 45 |
| hats 12 | shoes 30 | scarves 15 |
- What top 2 does the service show? What is the real top 2?
- Change the design so the top 2 is always right.
Show hints (3)
- "ADD UMBRELLA UP"
Add up umbrella over all three servers. Where does it rank on each server, and in the real total?
- "WHEN MERGING WORKS"
Suppose every search is counted on exactly one server. Could a search missing from its server's top 2 still be in the overall top 2?
- "TWO WAYS OUT"
Send all counts of one search to one server. Or ask the servers a second question: which searches have counts big enough to matter?
Show checked explanation
No. It shows socks 62 and shoes 60; the real top 2 is umbrella 135 and shoes 90. Umbrella is third on every server, so no server sends it. Merging is safe only when each search is counted on one server: route counts by a hash of the search, or add a threshold round.
from collections import Counter servers = [ Counter(shoes=60, jacket=50, umbrella=45, hats=12), Counter(tent=58, boots=52, umbrella=45, shoes=30), Counter(socks=62, gloves=51, umbrella=45, scarves=15), ] def top(counts, k=2): return sorted(counts.items(), key=lambda item: (-item[1], item[0]))[:k] merged = Counter() for counts in servers: merged.update(dict(top(counts))) print("shown:", top(merged)) print("real: ", top(sum(servers, Counter())))
shown: [('socks', 62), ('shoes', 60)]
real: [('umbrella', 135), ('shoes', 90)]Code run on CPython 3.12 against the examples above. Checked by an editor other than the author.
Walkthrough
Each server sends its top 2, and the service adds them up:
| search | sent by | shown count | real count |
|---|---|---|---|
| socks | C | 62 | 62 |
| shoes | A | 60 | 90 |
| tent | B | 58 | 58 |
| boots | B | 52 | 52 |
| gloves | C | 51 | 51 |
| jacket | A | 50 | 50 |
| umbrella | nobody | 0 | 135 |
Two things go wrong. No server sends umbrella, the real winner. And shoes shows 60, not 90, because server B kept its 30. This is the trap with heavy hitters, the most frequent items: a search popular everywhere but on top nowhere.
When is the merge right? When each search is counted on one server only. Take any search and the server that counts all of it. If the search is not in that server's top 2, two searches there rank above it. Their counts are full totals too, so the search can't be in the overall top 2 either.
So the exact fix is to count each search in one place. Each log server counts locally for a minute, then sends each count to the search's owner: one server picked by a hash of the search text. Owners hold full totals, so their top 2 lists merge correctly.
The cost: every count crosses the network once more, and a hot search loads its owner. Local counting keeps this small: umbrella costs one message per log server per minute, not one per search typed.
Common mistakes
- Asking each server for its top 20 to build a top 10. That lowers the risk but proves nothing: a search 21st on every server can still win.
- Showing the merged counts as real totals. They are only lower bounds: shoes shows 60, but its total is 90. Even when the right searches win, their order can be wrong.
- Ignoring a server that did not answer. The list then covers only part of the logs. Retry, or mark the result as partial.
Follow-up questions
- "Can you get the exact top 2 without moving all the counts?" Yes, in three rounds. Round 1 is your top-2 merge. Its counts are lower bounds, so the real 2nd place has at least 60. A search with 60 or more has at least 20 on one of the 3 servers. Round 2: ask every server for all searches at 20 or more. Round 3: fetch their full counts.
- "There are millions of different searches, and exact counts don't fit in memory. What then?" Each owner counts with a Count-Min sketch: a few rows of counters. A search adds its count to one counter per row, chosen by that row's hash. Its estimate is the smallest of those counters: maybe too high, never too low. A sketch can't list searches, so the owner keeps a small heap of top searches, updated on each count.
What a strong answer covers
Builds a case where a search tops no server but wins overall. Says merging is safe only when each search is counted in one place. Fixes it by routing or a threshold round, and names the cost.
Open in interactive practice deck →