Dashboard/System design/24. Sharded Counters

SYSTEM DESIGN · LESSON 24 OF 52 · 6 MIN VIDEO · 3 TOPICS

Sharded Counters

Module Progress (0 / 3 Lessons Completed)0%
System design Track (0 / 52 Modules) · Overall 0/80 (0%)0%
⌁

Narrated HD Video Lesson (6 min) · Pro

Lesson Overview + Section #01 + RESHADED Checklist + Quiz #1 are unlocked free below (no login required). Unlock all 80 HD video lessons and deep-dive sections — or watch our two free sample video lessons first.

0% READSec 01/03 · System Design: The Sharded Counters
SYSTEM DESIGN VIDEO LESSON & GUIDE

Sharded Counters

Sharded counters solve extreme single-key write contention when viral social posts, live streams, or flash sales concentrate tens of thousands of concurrent increments per second onto one logical entity. A single database row serializes updates behind a row lock (~1,000 writes/s ceiling), causing cascading timeouts when a celebrity post receives 50,000 likes/s. In L4-L6 interviews, candidates are evaluated on diagnosing hot-key lock serialization, splitting writes across independent counter shards, decoupling million-QPS reads via background aggregation and caching, and dynamically adapting shard counts per entity.

WHAT YOU WILL MASTER IN THIS LESSON
  • Quantify single-row lock serialization limits (1,000 writes/s vs. 50,000 viral writes/s) and explain why vertical hardware scaling fails.
  • Design a write-sharded, read-aggregated counter topology partitioned across independent storage nodes.
  • Offload high-ratio read traffic (1,000:1 views-to-likes) using periodic background aggregation and an eventually consistent read cache.
  • Size shard counts dynamically from follower heuristics and live write telemetry, and compare Random, Round-Robin, and hash(user_id) routing.
SYSTEMATIC BUILDING-BLOCK CHECKLIST (FROM MODULE 01)

4-Point Infrastructure Primitive Audit

Check off each dimension as you study this building block—interviewers test what it solves, how it works inside, what it costs, and how it fails.

0 / 4 Checked
INTERVIEWER SCORECARD · WHAT MAKES A STRONG HIRE?

5 Signals Bar Raisers Grade (L4 vs. L5 Senior vs. L6 Staff)

Senior (L5 / E5 / Senior SDE) Bar — Autonomous Pacing, Depth & Proactive Trade-Offs: Owns the 45-minute clock without prompting, derives every component quantitatively, surfaces failure modes (hot shards, stampedes, dual writes) BEFORE being asked, and defends consistency vs. latency trade-offs.
1. Scoping
✓ STRONG HIRE SIGNAL

Clarifies users, read/write skew, p99 latency, consistency bounds, and explicit non-goals in minutes 2–8.

✗ NO-HIRE RED FLAG

Jumps straight into drawing load balancers and microservices at minute 2 without asking a single question.

2. Right-Sized Design
✓ STRONG HIRE SIGNAL

Starts with the simplest working architecture justified by the math (e.g., 1 NVMe SQL primary + replicas + Redis for 40 writes/s & 3 TB).

✗ NO-HIRE RED FLAG

Recites a memorized Kafka + multi-DC Cassandra diagram that contradicts their own traffic calculations.

3. Technical Depth
✓ STRONG HIRE SIGNAL

Drives deep into 1–2 hard bottlenecks (partition keys, hot-key mitigation, transactional outbox, KV-cache batching) at minute 27.

✗ NO-HIRE RED FLAG

Skims across 12 boxes at identical buzzword depth and runs out of time before the deep dive.

4. Explicit Trade-Offs
✓ STRONG HIRE SIGNAL

Uses 3-part reasoning: 'A queue here because SMTP spikes to 3 s; checkout stays <60 ms, and we accept eventual email delay + retry dedup.'

✗ NO-HIRE RED FLAG

Name-drops tools ('We put Kafka and ZooKeeper here') without stating the alternative or operational cost.

5. Communication
✓ STRONG HIRE SIGNAL

Thinks aloud every 30–45s, checks alignment at phase transitions, and treats 'What if X fails?' as a cue for where points are awarded.

✗ NO-HIRE RED FLAG

Goes silent for 5 minutes while drawing or defends a flawed assumption defensively when nudged.

ARCHITECTURE & DATA-FLOW SLIDES · SLIDE 1 OF 1Section 01: System Design: The Sharded Counters
ARCHITECTURE / DATA FLOW DIAGRAM
3M likes in 60 sUserspressing like49,000 more waiting each sLike servicebottleneck: one lockCounter rowpost 7 · likesslowOther postssame database server
SECTION 01 OF 3

1. System Design: The Sharded Counters

★ FREE PREVIEW
Key Takeaway: A single database counter row serializes concurrent increments behind a mutual-exclusion row lock; when viral traffic hits 50,000 writes/s against a 1,000 writes/s lock ceiling, splitting the key is mandatory.
ARCHITECTURE / DATA FLOW DIAGRAM
3M likes in 60 sUserspressing like49,000 more waiting each sLike servicebottleneck: one lockCounter rowpost 7 · likesslowOther postssame database server

Why Correct Counting Requires Row-Level Serialization

In a naive social media schema, each post stores its total likes in a single row (post_id = 7, likes = 1204). Recording a like executes a read-modify-write increment (likes = likes + 1). Without concurrency control, two simultaneous requests both read 1,204 and both write back 1,205, silently losing a like. To preserve atomicity, the database acquires an exclusive row lock for the duration of each update transaction. For 99.9% of ordinary posts receiving a few likes per minute, the lock is uncontended and single-row updates work effortlessly.

  • Lost-Update Hazard: Concurrent unlocked read-modify-write operations overwrite each other under race conditions.
  • Serialization Ceiling: Even on fast NVMe storage with group commit, lock acquisition, WAL flush, and lock release cap a single hot row around ~1,000 updates/s (1 ms serialized critical section).

Anatomy of a Celebrity Viral Meltdown

Suppose an account with 100,000,000 followers publishes a post that attracts 3,000,000 likes in the first 60 seconds—an arrival rate of 3,000,000 / 60 s = 50,000 writes/s targeting post_7. Because the row lock drains at most 1,000 writes/s, inbound traffic arrives 50x faster than service capacity, adding 49,000 queued requests every second. Database worker threads and connection pools saturate waiting on post_7's lock, requests time out, and unrelated posts co-located on the same database shard suffer collateral latency spikes (noisy-neighbor cascading failure).

Traffic ProfileInbound Write RateSingle-Row Drain CapacityQueue Backlog GrowthSystem Behavior
Ordinary Post0.1 - 5 writes/s~1,000 writes/s0 (lock free > 99.5% of time)Sub-5 ms p99 commits
Celebrity Viral Post50,000 writes/s (3M / 60 s)~1,000 writes/s+49,000 queued writes/sLock timeouts, pool exhaustion, node-wide brownout

Why Buying a Bigger Server Fails: The Hot-Key Bottleneck

Scaling the database vertically from 16 to 128 CPU cores provides almost zero relief for post_7, because 127 cores still sit blocked waiting for the single exclusive lock on that one row (Amdahl's Law applied to serialized state). Sharding the database by post_id alone also fails, because every write for post_7 still hashes to the exact same single node and row. Eliminating a single-key bottleneck requires replacing one logical row with multiple independent physical rows.

Interview Signal: Whenever a system design prompt mentions celebrity posts, viral video view counters, live auction bids, or flash-sale inventory, immediately flag single-key lock contention (hot key) before proposing read caches—caches only absorb reads, never writes.
SECTION 02 OF 3

2. High-Level Design of Sharded Counters

🔒 PRO SECTION
Key Takeaway: Decomposing one hot counter into N independent shard rows distributes write locks in parallel across servers (50,000 / 1,000 = 50 shards), trading O(1) parallel writes for O(N) summed reads.

Section 1 + Checkpoint Quiz #1 are unlocked free outside the paywall. Unlock all architecture slides, QPS & sharding trade-off tables, and all 80 narrated HD video lessons with Full Access — or inspect our sample payment system lesson first.

SECTION 03 OF 3

3. Detailed Design of Sharded Counters

🔒 PRO SECTION
Key Takeaway: A periodic background aggregator collapses O(N) shard reads into O(1) cached lookups, dynamic shard scaling reserves multi-shard overhead strictly for hot posts, and hash(user_id) routing makes like/unlike atomic.

Section 1 + Checkpoint Quiz #1 are unlocked free outside the paywall. Unlock all architecture slides, QPS & sharding trade-off tables, and all 80 narrated HD video lessons with Full Access — or inspect our sample payment system lesson first.

ACTIVE RECALL & TRADE-OFF PROBES

System Design Checkpoint Quizzes (1)

CHECKPOINT QUIZ #1

In "System Design: The Sharded Counters" (Anatomy of a Celebrity Viral Meltdown), how does a production-grade design handle "Celebrity Viral Post" (System Behavior)?