AlgoViz

Sharded counters

medium 22 min

One counter that everybody increments becomes a hot key: all writes wait in one line.

Where it shows up

The same pattern appears across everyday software.

  • Firestore distributed counters

    Firebase documents this pattern: a counter with a subcollection of shards. Its guide says ten shards can take ten times the writes of a traditional counter.

  • DynamoDB write sharding

    The developer guide suggests adding a random number to the partition key, for example 1 to 200, and merging the results when reading.

  • Java LongAdder

    Keeps several variables that grow under contention and sums them on demand. The documentation recommends it for statistics, not for synchronization.

  • Linux per-CPU counters

    The kernel offers per-CPU operations so that each processor counts in its own memory, avoiding costly synchronization between them.

  • Redis Active-Active counters

    When several regions increment the same key, Redis documents that the counters accumulate the operations of all members: one counter per region, summed.

  • Analytics dashboards

    Each server counts page hits locally and a collector adds them every few seconds. The shards are the servers.