Full answer
Requirements
Functional:
- Increment and read counters: views, likes, shares per post or video.
- Unique viewers per video (approximate is fine).
- Likes are per user: a user can like once and unlike.
Non-functional:
- Handle 10 billion increments per day overall and a viral item receiving 1M increments per minute.
- Reads are fast and frequent (every time a post is shown).
- View counts may lag a few seconds and be slightly approximate; like state for a user must be correct.
Capacity estimates
Assumptions: 10B increments per day overall; one viral item at 1M increments per minute; 1B counters; about 50 bytes per counter row; a single Redis key handles in the order of tens of thousands of increments per second on one core (an assumption; it shares that core with other keys).
- Overall rate: 10B / 86,400 = about 116,000 increments per second.
- Viral item: 1M / 60 = about 16,700 per second on one key, which would be the dominant load on its shard.
- Counter storage: 1B x 50 B = 50 GB, small.
- Unique counts: a Redis HyperLogLog uses up to 12 KB per key with a standard error of 0.81%; for 10M videos that is up to 120 GB, so keep HLLs only for items that need them, or merge per day.
API
POST /v1/counters/{entity}/{id}/increment {"field": "views", "by": 1} -> 202
GET /v1/counters/{entity}/{id} -> {"views": 1048576, "likes": 20311}
PUT /v1/posts/{id}/likes/me -> 200 (idempotent like)
DELETE /v1/posts/{id}/likes/me -> 204 (unlike)
Data model
likes(post_id, user_id, created_at), primary key(post_id, user_id): the uniqueness makes like and unlike idempotent. The like count is maintained from changes to this table, not from raw button taps.counters(entity, id, field, value)in a store that supports atomic increments (Redis, Cassandra counters, or a SQL table updated in batches).- Optional
counter_shards(entity, id, field, shard_no, value)for hot keys.
High-level design
flowchart LR
C[Clients] --> API[Counter API]
API --> K[Kafka: increment events, keyed by item]
K --> AGG[Aggregators: sum per key per second]
AGG --> CS[(Counter store)]
CS --> RC[(Read cache)]
API --> RC
API --> LK[(Likes table)]
LK -. change events .-> KWrite path
- The API publishes an increment event, keyed by item ID so all events for one item go to one Kafka partition and one aggregator.
- Each aggregator keeps an in-memory map
{key: pending_count}and every second (or every 10,000 events) writes oneINCRBY key nper key, then commits its Kafka offsets. - A viral item at 16,700 increments per second becomes one write per second to the store.
If the aggregator crashes after writing but before committing offsets, some events are replayed and counted twice. For views that small error is acceptable; to remove it, store the last applied offset per partition together with the counts.
Hot counters without a log
Where increments must go straight to Redis, split the key into N sub-counters (views:v42:0 to views:v42:15). Each increment picks a random sub-counter; a read sums all of them (one MGET) and caches the result for a second. Sixteen sub-counters cut per-key load by 16x, and spreading them across slots spreads load across nodes.
Read path
Counts are read far more than written. Serve from a cache with a short TTL (1 to 5 seconds). Show rounded numbers for large values ("1.2M"), which also hides small lag.
Bottlenecks and trade-offs
- Bottleneck: the hot key on one shard or one Kafka partition; aggregation fixes the store side, and an extremely hot item can be split across several partitions with a salted key.
- Exact vs approximate: likes come from a unique table and are exact; views tolerate small error in return for much cheaper writes.
- Freshness vs cost: a 1-second aggregation window means counts lag by about a second but cut writes by orders of magnitude for hot items.
- Sub-counters spread writes but make reads more expensive; use them only for keys detected as hot.
- Fraud: view counts that matter (monetisation) need filtering of bots and repeated views before counting, usually in the stream processor.
By level
Fresher: Explain why updating one row for every view is slow for popular posts, and the idea of adding up increments and writing them together. A fresher should know that likes need uniqueness per user.
Mid: Design the event pipeline with per-key aggregation, sub-counters for hot keys, idempotent likes, and caching of reads. Expect the per-minute spike converted into per-second load on one key.
Senior: Cover exactly-once options with offset storage, reconciliation from raw events, fraud filtering, multi-region counting and memory cost of unique counting at scale. Expect an option for exactly-once counting and its cost.
Key points
- Exact counts come from unique facts (a likes table); approximate counts can be aggregated
- Key events by item, aggregate in memory, write batched increments
- Sub-counters spread load for hot keys and are summed on read
- HyperLogLog gives unique counts at up to 12 KB with about 0.81% error
- Cache reads with short TTLs and accept slight lag
Common mistakes
- Doing one database UPDATE per view on a viral item
- Counting likes by incrementing on every tap without uniqueness
- Claiming exact counts while replaying events at least once
- Storing a set of all viewer IDs to count unique views
Follow-ups
- How would you make view counts exactly once?
- How would you show a live counter during a live stream?
- How do you reconcile counters if the aggregator had a bug?
Tips
- Ask which counts must be exact
- Convert per-minute spikes into per-second load on one key
- Mention HyperLogLog for uniques
Evaluation rubric
- Requirements distinguish exact and approximate counts
- Correct capacity math including the hot key
- Batched aggregation and hot key strategy
- Trade-offs on accuracy, freshness and read cost