Distributed Counter — Detailed#
flowchart TB
subgraph Write[Increment path]
EVT[Event sources]
SHARD[N micro-counters per key<br/>shard by event hash]
LOCAL[Local batch + flush]
REDIS[(Redis counters per shard)]
EXACT[(Durable per-shard ledger)]
end
subgraph Read[Read path]
AGG([Aggregator: sum across shards])
CACHE[(Approx cache 1s-10s)]
API[Counter API]
end
subgraph Approx[Approximate fallback]
HLL[HyperLogLog uniques]
CMS[CMS hot keys]
end
subgraph Recon[Reconciliation]
BATCH[Batch totals - exact]
DRIFT[Drift alarm]
end
Write --> Read
Approx --- Write
Recon --- Write
classDef client fill:#dbeafe,stroke:#1e40af,stroke-width:1px,color:#0f172a;
classDef edge fill:#cffafe,stroke:#0e7490,stroke-width:1px,color:#0f172a;
classDef service fill:#fef3c7,stroke:#92400e,stroke-width:1px,color:#0f172a;
classDef datastore fill:#fee2e2,stroke:#991b1b,stroke-width:1px,color:#0f172a;
classDef cache fill:#fed7aa,stroke:#9a3412,stroke-width:1px,color:#0f172a;
classDef queue fill:#ede9fe,stroke:#5b21b6,stroke-width:1px,color:#0f172a;
classDef compute fill:#d1fae5,stroke:#065f46,stroke-width:1px,color:#0f172a;
classDef storage fill:#e5e7eb,stroke:#374151,stroke-width:1px,color:#0f172a;
classDef external fill:#fce7f3,stroke:#9d174d,stroke-width:1px,color:#0f172a;
classDef obs fill:#f3e8ff,stroke:#6b21a8,stroke-width:1px,color:#0f172a;
class EVT,SHARD,LOCAL,API,HLL,CMS,BATCH,DRIFT service;
class EXACT,CACHE datastore;
class REDIS cache;
class AGG compute;
Why shard a counter#
- A single hot key (e.g. tweet likes for a celeb) bottlenecks updates.
- Split into N counters; sum on read.
- Trade-off: read fan-out vs write contention.
Approximate vs exact#
- For "views/likes at scale" approximate (HLL, CMS) is fine.
- Money counters must be exact → use ledger + read-side cached aggregate.
Glossary & fundamentals#
Concepts referenced in this design. Each row links to its canonical page; the tag column shows whether it is a high-level (HLD) or low-level (LLD) concept.
| Tag | Concept | What it is | Page |
|---|---|---|---|
HLD |
Sharding | horizontal partitioning across nodes | database-sharding |
HLD |
Probabilistic data structures | Bloom, HLL, Count-Min, MinHash, t-digest | probabilistic-data-structures |
HLD |
Event sourcing + CQRS | commands -> events; separate read model | event-sourcing-cqrs |