KatInterview

Build a Fault-Tolerant Distributed Counter at Scale

System DesignModel answer
HardModel answerSystem Designdistributed-systemsconsistencyconcurrency

Last updated

Design a horizontally scalable counter service that guarantees atomic updates, read-after-write consistency, and correct behavior under retries and failures.

The Problem

You are building a general-purpose counter service that many clients write to simultaneously. Think of it as the engine behind inventory tracking, rate limiting, viral like counts, or global quota enforcement — anywhere an exact integer must stay correct under high concurrency.

What to design

Describe a complete system. Cover each of the following:

APIs Define the write and read endpoints. What parameters does a caller send? What does the response include? How does the API surface support safe retries?

Data model How are counters stored, partitioned, and identified? What metadata does each entry carry?

Concurrency control Walk through at least two strategies (for example, optimistic compare-and-swap, distributed locking, and single-writer-per-key via consensus). Explain why you choose one over the others.

Idempotency and delivery semantics Retries are inevitable. How do you ensure a timed-out request never double-counts? Where exactly does the deduplication check happen, and why does that location matter?

Leader election and failover Who decides which node handles writes to a given counter? What happens during a leader change, and how quickly do clients recover?

Partition tolerance If part of the cluster becomes unreachable, what can and cannot proceed? Name the CAP trade-off you are making and justify it.

Clock skew Where do clocks appear in the design? How do you prevent drift from causing ordering bugs or incorrect expiry?

Hotspot mitigation A single counter receives 200k writes per second — far beyond one node. How do you scale it without losing correctness?

Monitoring and rollback What signals tell you a counter node is struggling? How do you roll back a bad deployment without losing acknowledged writes?

Constraints and assumptions to state out loud

Before answering, agree on scale. A reasonable baseline:

DimensionStarting assumption
Total counters100 million
Aggregate writes1 million/s
Aggregate reads5 million/s
Hottest single keyup to 200k writes/s
Write latency SLOp99 < 20 ms within a region
Read latency SLOp99 < 10 ms within a region
Availability target99.99%
DurabilityNo acknowledged increment is ever lost

Challenge these numbers if they seem wrong for the use case you are solving. Interviewers expect you to drive the scoping conversation.

What a strong answer looks like

  • Commits to a concrete architecture early, then defends it against failure scenarios.
  • Clearly names trade-offs rather than presenting every option as equally good.
  • Gives rough numbers (batch size, replication factor, dedup TTL, failover window) to show the design is grounded in reality.
  • Handles the "hottest key" scenario without hand-waving.

Hints

Hint 1

Start by nailing down consistency requirements before touching architecture. Read-after-write for a single key is a very different contract from cross-key transactions — scope this explicitly so you don't over-engineer.

Hint 2

Compare optimistic CAS, distributed locks, and single-writer-per-shard for concurrency control. Think about what happens to each approach when 200k writes/s hit the same key — retry storm behavior is the deciding factor.

Hint 3

Idempotency is only airtight if the dedup check and the counter update happen atomically in the same replicated operation. If they are separate steps, a failover between them silently breaks your exactly-once guarantee.