How Netflix built a distributed counter
Counting sounds trivial — until it has to be right at global scale.
A single counter row can't absorb millions of increments without becoming a contention hotspot. Netflix's approach batches events through a durable log, aggregates them, and stores rolled-up counts — trading instant exactness for massive throughput and eventual accuracy.
The naive counter
The obvious design is a single row holding the total, and every event runs an increment against it. This works fine at low volume, but under high concurrency every writer must take a lock on that one row, so the increments serialize into a queue and throughput collapses no matter how much hardware you throw at it. The row has become a contention hotspot.
Append to a log
The key inversion is to stop mutating shared state and instead treat each increment as an immutable event appended to a durable, partitioned log such as Kafka. Appends never contend because writers are just adding to the end of independent partitions, so the system absorbs a firehose of events without any lock. The count is no longer stored — it is derived from the stream of events.
Aggregate asynchronously
A separate rollup service consumes the log in time windows, sums the increments within each window, and writes compact aggregate results. Because this happens off the write path, the ingestion side stays fast while the expensive summation runs in the background at its own pace. The aggregator can also be scaled independently and replayed from the log if it falls behind or crashes.
Serve rolled-up counts
Reads are served from the pre-aggregated store, which holds a small, ready-to-return total rather than millions of raw events. The visible count lags real time by one aggregation window, so it is eventually consistent rather than exact-to-the-instant, but in exchange the design scales to millions of events per second. For view counts and likes, that tiny delay is a trade nobody notices.
Why counting is secretly hard
Counting looks like the most trivial operation in computing — add one to a number — which is exactly why it makes such a good system design problem. The difficulty has nothing to do with arithmetic and everything to do with scale and concurrency. When a video goes viral, a "view count" or "like count" might need to absorb millions of increments per second from all over the world, and every one of those increments wants to modify the same value.
That shared-value requirement is the trap. Getting a count right on one machine is easy; getting it right when thousands of servers are all trying to increment the same logical number, without losing updates and without grinding to a halt, forces you into the core distributed-systems trade-offs between consistency, throughput and latency. Netflix's published design for exactly this problem is a clean tour through those trade-offs.
Why the naive counter fails
The intuitive solution is one database row, UPDATE counters SET value = value + 1 WHERE id = ?, run on every event. It is correct, and it works right up until the load matters.
The problem is contention. To increment safely, each transaction must lock the row so two writers do not read the same value and both write back the same result (a lost update). Under high concurrency, that lock turns concurrent writers into a single-file queue — everyone waits their turn on the same row. This is a hotspot: no amount of extra hardware helps, because the bottleneck is serialization on one piece of shared state, not a shortage of CPU. A first improvement is a sharded counter, splitting the total across N rows so writers hit different rows and you sum them on read; this spreads contention but complicates reads and still couples writing to database mutation.
The key idea — turn a mutation into an event
The breakthrough is to stop thinking of the count as a number you edit and start thinking of it as the result of a stream of events you accumulate. Instead of "increment the counter," each service simply emits an immutable "this happened" event and appends it to a durable, partitioned log like Kafka.
Appends do not contend. Writing to the end of a log — especially one split into many partitions — has no shared value to lock, so ingestion scales almost linearly with the number of partitions and brokers. Just as importantly, the log is durable and replayable: once an event is written it will not be lost, and the true count is now *derivable* from the sequence of events rather than stored as fragile mutable state. This inversion, replacing an in-place update with an append-only event, is the single most important move in the whole design and shows up all over event-driven architecture.
Aggregation and rollups
Events pile up quickly, and you cannot answer "how many views?" by scanning millions of raw records on every read. So a separate aggregation (rollup) service consumes the log and does the summing off the hot path.
It reads events in time windows — say, every few seconds or minutes — sums the increments in each window, and writes a small, compact aggregate to a counter store. Reads then hit that pre-computed total, which is tiny and instant to return. This separation of concerns is what makes the whole thing scale: the write path is optimized purely for cheap, contention-free ingestion, while the read path is optimized purely for fast lookups of a ready-made answer, and neither ever blocks the other. Because the aggregator is driven by the durable log, it can be scaled out, and if it crashes or falls behind it simply resumes from its last committed offset and replays — no events are lost, the count just catches up.
The trade-off and where it applies
Nothing is free, and here the price is timing. Because aggregation runs in windows, the count you read is always slightly behind the true, real-time total — it is eventually consistent rather than exact-to-the-instant. For a brief window the number is approximate; once the aggregator catches up, it is exact.
The engineering judgment is recognizing when that trade is acceptable, and for the counts this design targets it almost always is. Nobody is harmed if a YouTube view count or a tweet's like total lags reality by a few seconds, so trading instant exactness for enormous throughput and durability is clearly the right call. The lesson generalizes well beyond Netflix: when a single piece of shared mutable state becomes a write hotspot, the escape hatch is to replace synchronous in-place mutation with asynchronous, append-only event processing and accept eventual consistency. The same pattern powers metrics pipelines, analytics counters, rate limiters and billing systems. Reserve strongly-consistent, synchronous counters for the rare cases — like financial balances — where being momentarily wrong is genuinely unacceptable.
A naive single-row counter serializes all writes — it becomes a lock hotspot under load.
Sharded counters split the count across N rows and sum on read, spreading write contention.
Event-based designs append increments to a durable log (like Kafka) and aggregate asynchronously.
Rollup/aggregation services compute windowed totals and persist compact results.
The trade-off is eventual consistency: counts are approximate for a short window, exact afterward.
Quick quiz
Why does a single-row counter fail at scale?
What's the trade-off of the event-log + aggregation design?
How Apache Kafka works
A distributed commit log that decouples who produces data from who consumes it.
Where do we cache — and how it goes wrong
Caching lives at every layer. So do the ways it can bite you.
A crash course on scalability
How one box on a laptop becomes a system that serves millions.