Detailed Design of Sharded Counters 00:00:01 A five-second scoreboard keeps the stands responsive Thousands of spectators repeatedly refresh the same scoreboard. A cold read adds every counter shard. Hold that sum for five seconds, then compare the read cost with invalidating on every update. The cache is empty, so all shards are read. The sum is kept for 5 seconds. The next readers take one key. Twenty servers flushing ten times a second would empty it 200 times a second. A timestamp makes bounded display staleness understandable. If the cache regenerates expensively, coordinate refreshes so the end of its freshness window does not trigger a storm of shard scans. 00:00:40 A fast buffer can vanish before its flush A worker accumulates increments in memory for one hundred milliseconds before flushing. Trace the buffer swap, the remote write and a failure between those steps. Which increments can disappear? Three clients add one each. After 100 ms the buffer holds 226. The buffer is zero again before the network call. If the server dies with a full interval in memory, those are gone. An uncertain flush also risks duplication on retry. If the count drives billing, use durable event identities and replayable records or a transactional handoff with an explicit recovery rule. 00:01:21 Two old replicas can form a majority Three copies hold versions of a single shard. In the first trace the newest value reached only the primary. Predict whether asking any two copies must include that value. The primary has 1,000. Replicas are behind. The answer may be anything from 995 to 1,000. Two replicas are a majority of three, and both are old. Every write reached two copies, so any two contain the newest. Fixed read and write quorums intersect when their sizes sum above the replica count. Here the conclusion assumes ordered versions and completed writes, and does not establish a snapshot across different shards. 00:02:02 A hot machine is not always a hot counter Four shard hosts show forty-five, fifty-five, ninety-five and seventy-five percent utilization. Before growing the counter, inspect what else shares the busiest host and which resource is saturated. 45, 55, 95 and 75%. The average is 67.5%. Its machine also hosts a busy search index. After moving it, loads are even. Weeks later all shards are above the trigger. New shards start at zero, nothing moves, each takes half the former rate. Balance by the bottleneck that actually limits increments. Later, if all shards approach their sustainable capacity, more independently placed buckets can share new writes without redistributing the historical count. 00:02:52 Remove a shard without changing the public score Four shard values total one hundred twenty-five. Stop sending new increments to the retiring shard before combining its thirty-three with a surviving shard. Track the sum across the transfer. 31 plus 28 plus 33 plus 33. Shard 4 receives no more. 33 goes to shard 1 in one transaction. Shard 4 can be deleted. Prevent readers from counting the transferred value twice or seeing it in neither place. Use an appropriate atomic transfer or versioned read cutover, and verify the invariant before deleting the old shard.