← all topics

Design a Like Counter

Hello Interview structure · plain notes, one idea per line

Understanding the Problem

👍 What are we building?
  1. Is the real problem counting, or fake likes (bots, bought likes)? Assume counting, with a hook so an abuse system can retract likes later through the same path.
  2. Does deleting an account have to erase its likes? Assume yes, within 30 days, and the public count visibly drops. Surface that this is a product decision: a celebrity deletion visibly drains 50M likes off a public number.
  3. May the displayed count briefly dip or be a little stale? Assume yes: a few seconds, always labeled.

Functional Requirements

  1. Users should be able to like and unlike an item, and retries or duplicates must never change the count.
  2. Users should see an item's like count. It may lag a few seconds, but it must say so.
  3. The acting user should see their own like instantly, and it is always right.
  4. Users should be able to list who liked an item (paginated), and account deletion must erase likes with the public count dropping honestly.

Below the line (out of scope):

Non-Functional Requirements

Do the scale math first, because it picks the architecture
  1. The edge is exact. Retries, duplicates, crashes, and replays never change what the count converges to.
  2. The count is eventually consistent with a stated bound: staleness p99 under 5 seconds, measured at the sink from event time, not guessed, and labeled asOf.
  3. Like-write availability 99.95%, p99 under 150ms. Under overload, fail writes fast with retry-after rather than queueing them; reads keep serving.
  4. Read-your-writes for the actor (their own button), eventual for everyone else.

The Set Up

Defining the Core Entities

API or System Interface

POST   /v1/items/{itemId}/likes            // Idempotency-Key: one key per tap, kept across retries
DELETE /v1/items/{itemId}/likes/{userId}   // same key rules; userId must match the session
  → 200 either way                        // liking twice, or unliking nothing, is fine by design
GET    /v1/items/{itemId}/count            // → { count, asOf } - approximate, labeled
GET    /v1/items/{itemId}/likes/{userId}   // → { liked } - read-your-writes for the actor, else eventual
GET    /v1/items/{itemId}/likes?cursor=&limit=   // who liked, paginated
POST   /v1/likes:batchGet   { itemIds: [...] }   // feed pages: N counts + N button states in one call

High-Level Design

1) Users can like and unlike, and retries never change the count

Like and unlike: one conditional write, and a retry never changes the count The Client sends a like with an idempotency key to APIGateway, which forwards it to LikeService. LikeService checks the key in IdempotencyTable, makes one conditional write on the edge row in EdgeStore, and writes the actor's overlay to SessionCache. APIGateway returns 200 to the Client, even when the write was a no-op. Client APIGateway auth, rate limitabuse hook enters here LikeService userId from the session,never the request body EdgeStore (truth) one row per (item, user)set LIKED if UNLIKED or newop_id +1 in the same write IdempotencyTable (user, key) -> answer, ~1 day SessionCache 'you liked X just now' 1. like + key kept acrossretries 2. forward 3. key check, store answer same key -> replay stored 200 4. conditional write still 200 on a no-op 5. write overlay instant own-button, exact 6. 200, even on a no-op the not-so-happy paths: lost 200, user unlikes, old retry arrives -> the key replays the stored answer, state stays UNLIKEDoverload -> fail writes fast with retry-after, never queue

2) Users see a count that lags seconds, and says so

The count pipeline: dedupe per bucket, then one +N per item per second EdgeStore's change feed streams each committed flip into Kafka, keyed by bucket. Stage 1, DedupeWorkers that each own some bucket keys, drops any op_id at or below the last one seen per edge. A keyBy(item) shuffle brings all 64 buckets' deltas for an item to one WindowWorker, which writes one +N per second to CounterStore under an atomic fence on last_window. EdgeStore committed flips only,ordered per row Kafka item#b17 item#b3 item#b41 DedupeWorker owns some bucket keysdrop op_id ≤ last seenmemory TTL = 7d replay WindowWorker (per item) adds up one second at a timeemits ONE (item, sec, +N)'+8,214', not 8,214 writes CounterStore ADD count :nSET last_window = :wCONDITION last_window < :w 1. change feed streams out the tailer produceseach flip into KafkaDynamoDB Streams, 24h keyed item#hash(user)%64 - deterministic, never random 2. feeds per bucket stage 1: per-bucket dedupe 3. keyBy(item) shuffle all 64 buckets' deltas for an itemmeet at ONE window subtask 4. one +N/sec stage 2: one subtask per item the fence: one atomic update -a replayed second does nothing the not-so-happy paths: processor crash -> memory and read position rewind together, replayed seconds fail the fenceskip stage 2 and 64 workers write partials -> a per-item guard accepts the first, wrongly rejects the other 63

3) The actor sees their own like instantly

Two read paths: the cheap labeled count, and the actor's exact own button Path 1: a Reader asks the CDN for the count; on a miss the CDN asks CountService, which reads Redis, which reads CounterStore on a miss. Path 2: the Actor asks LikeService for the button state, which layers the SessionCache overlay over an edge read from EdgeStore. The two paths never mix. path 1: the count - cheap, approximate, labeled asOf Reader CDN a few seconds TTL CountService SWR + one refresh per item Redis read-through CounterStore {count, asOf} 1. GET count 2. on miss 3. read 4. on miss the two paths never mix path 2: the own button - instant, exact for the actor Actor LikeService button = overlayover an edge read SessionCache 'you liked X just now' EdgeStore the truth, one row per edge 5. button? 6. overlay 7. edge read feed pages: one batch call returnsN counts + N button states the not-so-happy paths: viral item + caches expiring together = a stampede -> serve stale while refreshing, one refresh per itemoverload -> counts go visibly stale, never wrong - the overlay keeps the actor's own button correct

4) Who liked an item, and erasure that drops the count honestly

Bucketed edges: a viral item's writes spread over 64 partitions LikeService spreads one viral item's ~17K writes a second across 64 buckets of EdgeStore, about 260 a second each. WhoLikedQuery has to ask all 64 buckets. ErasureJob deletes a celebrity's rows at a limited rate, and every delete flows out through the ChangeFeed into the count pipeline as an ordinary minus-one. EdgeStore - the truth, bucketed LikeService viral item writes:~17K/s on ONE item bucket 0 ~260/s bucket 1 ~260/s bucket 63 ~260/s ... row key: item + hash(user) % 64a single partition caps near 1K/sthe viral item does ~17K/sbuckets spread it: 64 x ~260/s 1. spread the writes ~17K/s -> ~260/s each ErasureJob celebrity deletion: 50M rowshard-delete edge + retry rows 3. delete rate-limited ChangeFeed -> the pipeline -1 b3 -1 b17 -1 b41 same path as live likes 4. every delete = a minus-one counts drop honestly WhoLikedQuery rare: admin + recount path 2. asks all 64 buckets the price of the bucket key the not-so-happy paths: no buckets -> the whole viral item lands on one ~1K/s partition and the truth melts firstcelebrity deletion is just a big polite user: ordinary minus-ones, spread across buckets, rate-limited - no special path

Potential Deep Dives

1) How do retries and duplicates never change the count?

Bad Solution: dedupe requests in application memory
  • The service remembers recent request ids in memory and drops repeats. A restart forgets everything; a retry that lands on another node was never seen.
  • Worse, any read-then-write ("is it liked? no? then like it") is check-then-act: two concurrent taps both read UNLIKED and both apply.
Good Solution: put idempotency in the data model
  • Layer 1, the row: one row per (item, user), enforced unique by the database. A second like updates that row; it can never create a second row.
  • Layer 2, the conditional write: set state to LIKED only if it is currently UNLIKED (or the row is new). A retry finds LIKED, changes nothing, still gets 200. Each real flip bumps op_id atomically in the same write (DynamoDB: if_not_exists(op_id,0)+1 inside the conditional UpdateItem; a no-op emits no stream record).
  • This makes double-likes harmless, but it cannot tell a stale retry from a genuine new intent.
Great Solution: add the request key, and name the hole it closes
  • Layer 3: each tap gets a key; retries of that tap reuse it. The server keeps (user, key) → answer for about a day and replays the stored answer without touching state.
  • The hole it closes: like → 200 lost → user unlikes → the old retry arrives. State checks alone see UNLIKED and would re-apply the like, flipping the row against the user's last choice. The key makes that retry replay its original 200 instead; the row stays UNLIKED.
  • Say where each layer's guarantee lives: the row kills duplicates of existence, the conditional write kills duplicates of transition, the key kills duplicates of intent.

2) One post goes viral. What melts first?

Bad Solution: partition the edge table by item_id and defend everything downstream
  • The viral item's 17K conditional writes/s land on ONE partition that takes about 1K/s. The source of truth melts before Kafka or the counter ever see load.
  • Defending the stream and the counter while the truth store dies is defending the wrong tier.
Good Solution: bucket the truth store's key
  • Row key item + hash(user) % 64: one viral item's writes spread over 64 partitions, ~260/s each. Deterministic by user, so (item, user) uniqueness and per-edge ordering survive.
  • Name the cost out loud: the full who-liked scan now fans out to 64 buckets (rare path), and the recount job inherits the same fan-out.
Great Solution: one lever per tier, and the keying doubles as the stream's dedupe locality
  • Truth store: the bucket key (above). Stream: the SAME item#bucket rule keys Kafka, so a retried event meets its own dedupe memory. Counter: windowed aggregation means the counter row sees one write per item per second, far under any per-row limit.
  • If a single counter row ever needs sharding anyway, the fence must shard with it deterministically: route windows by shard = window_id % K, never randomly, or a replayed window can land on a different shard and pass that shard's fence. One sentence, but it is the difference between sharded and broken.
  • The tuning dial to watch is per-bucket write throttles: a throttle firing means the armor is breached and the bucket count needs to grow for that keyspace.

3) Exactly once: crash the processor mid-window and tell me the count

Bad Solution: count in the stream processor and INCRBY the store
  • Input dedupe protects the processor's input; nothing protects its output. After a crash, state and offsets restore together, the same windows are rebuilt, and the increments already applied to the counter apply a second time.
  • "Offsets plus dedupe give effectively-once" attributes the guarantee to the wrong mechanism.
Good Solution: idempotent sink via a guard table keyed (item, window)
  • Right instinct, two real defects. The guard write and the counter write are separate tables: a crash between them either loses a window or double-applies it, which violates the design's own "never check-then-act" maxim at its most important write.
  • And the guard table grows by ~109 rows a day unless it gets a TTL at least as long as the replay horizon.
Great Solution: the fence lives on the counter row itself
UPDATE counter SET
  count = count + :n,
  last_window = :w
CONDITION last_window < :w
-- one atomic statement; a replayed window fails the condition and does nothing
  • Guard and increment are one atomic update on one row; the side table is deleted from the schema. The guarantee lives at the last write, so everything upstream may replay freely.
  • The one-second viral trace, end to end: 16.7K events land across 64 dedupe workers; the keyBy(item) regroup brings that item's deltas to ONE worker; it emits exactly one (item, second, +N); the sink is one atomic update with the fence on the counter row, so there is no gap between check and add. Crash anywhere: the replayed second re-presents, fails the fence, the count stays right.
  • The crash trace in one breath: the processor's memory and reading position rewind together to the last checkpoint, it re-reads the same events, rebuilds the same per-second totals, re-offers them, and each replayed second bounces off the fence. The count is at most seconds stale and never double-counted.
  • Kafka honesty: the tailer's producer needs enable.idempotence=true (ordering preserved with up to 5 in flight); there is no per-key in-flight knob. Dedupe memory is TTL'd to the 7-day replay window, with reconciliation named as the backstop past it.

4) The counter store dies. Counts drift. A celebrity deletes their account.

Bad Solution: recount the edges and overwrite the counter
  • The recount reflects time T; the overwrite lands at T+delta and destroys every delta applied during the scan, worst on hot items, which the job prioritizes. Reconciliation becomes the design's best corruption vector.
Good Solution: rebuild from the truth, replay the bounded tail
  • Counter store lost: recount the edges in parallel, then replay the last 7 days of stream on top. Full-history replay does not exist; the stream keeps 7 days, and the edge store is the rebuild source.
  • Take the recount from a frozen snapshot AT the replay start position, and initialize each rebuilt counter's fence at that same position, so the replayed windows apply exactly once. A live recount with the fence set 7 days back would count the tail twice.
Great Solution: fenced corrections through the pipeline, never overwrites
  • Drift: recount an item from a frozen snapshot of the edges (PITR/snapshot export at stream position T), wait until the sink watermark passes T, then send the difference as a correction through the normal pipeline. The correction enters the stage-2 keyed stream as its own window, so the row fence serializes it with live traffic instead of overwriting it.
  • Clamp at zero and alert: a negative count is a bug report, not a number.
  • Celebrity deletion needs no repair machinery at all: 50M unlikes flow as ordinary rate-limited minus-ones. The erasure job is just a big polite user.

5) Multi-region, honestly

Bad Solution: active-active replication of the edge store
  • Two regions writing the same edge resolve by last-writer-wins, which silently breaks the exactness the whole design is built on. Cross-region conditional writes are impractical; global tables are LWW.
Good Solution: one home region per item range
  • Each item range has one home region that owns its writes; other regions forward writes there (priced: far users pay a cross-region hop on writes) and serve reads locally, labeled stale.
  • Reads never need the home region; only the write path does.
Great Solution: epoch-fenced pin moves, with the on-call signal named
  • Every range carries an era number (epoch), and every conditional write requires the current era. To move a range: bump the era FIRST at the new home. The old home's in-flight writes now carry a stale era and fail closed instead of forking history.
  • The unreplicated tail from the old home becomes a reconciliation correction through the pipeline, never a merge.
  • What on-call sees, in order: the old-home rejection spike first, then traffic shifting to the new home. That ordering is the health check for the move itself.
  • One honest open point, stated rather than hidden: where the epoch check lives. A conditional write evaluates against the row being written, so either every edge row carries the epoch attribute (with a bump-on-first-write migration story) or the writer checks an out-of-band range lease, and that check must not reintroduce check-then-act. The fencing-token shape is right; commit to a storage location when asked.

6) How do you run it? SLOs, alerts, backpressure, and the cutover

Final Design

Like counter: final design A like goes Client, APIGateway, LikeService, which checks the key in IdempotencyTable, makes one conditional write on EdgeStore and writes the actor's overlay to SessionCache. EdgeStore's change feed goes into Kafka, then DedupeWorkers (each owning some bucket keys) and a keyBy(item) shuffle to one WindowWorker per item, which writes one +N per second to CounterStore; ReconciliationJob feeds corrections through the same window. Readers get the count through CDN, CountService and Redis, falling back to CounterStore. Client APIGateway auth + abuse hook LikeService userId from session EdgeStore (truth) buckets: item+hash(user)%64set LIKED only if UNLIKEDop_id +1 in the same write 1. like+key 2. forward 3. write IdempotencyTable SessionCache 2a. key replay 3b. overlay after success own button: overlay + edge read Kafka b17 b3 keyed item#hash(user)%64 4. change feed streams out committed flips only, ordered per rowthe tailer produces each flip into Kafka DedupeWorker owns some bucket keysdrop op_id ≤ last seen WindowWorker 1s totals:ONE (item,sec,+N) CounterStore ADD count :nSET last_window = :wCONDITION last_window < :w 5. feeds 6. keyBy(item) one subtask per item 7. one +N/sec ReconciliationJob recount from snapshot,correction as its own window 9. correction delta Reader CDN seconds TTL CountService SWR + one refresh/item Redis read-through 8. count? miss miss on miss the not-so-happy paths: processor crash -> replayed seconds fail the fence - counter store lost -> recount edges + replay 7d of streamregion move -> bump the range epoch first, stragglers fail closed - overload -> fail writes fast, counts go visibly stale, overlay keeps actors right

What is Expected at Each Level