Shard a Concurrent Map With Lock Striping for Request Counters

Last updated: November 29, 2026

Quick Overview

Implement a thread-safe map that reduces contention by splitting keys across a fixed number of independently locked shards, with an atomic read-modify-write operation and a consistent size.

Google
Software Engineering Fundamentals
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Software Engineering Fundamentals
Medium

0

0

0 solved


Implement a thread-safe map that reduces contention by splitting keys across a fixed number of independently locked shards, with an atomic read-modify-write operation and a consistent size.

Frontend servers at Google keep per-key in-memory counters (per-user, per-endpoint) that thousands of threads touch. A technical screen uses this to see if the candidate knows why a single global lock hurts, how striping works, and how to take multiple locks without deadlocking.

What the Interviewer Expects
  • Explains why one global mutex becomes a bottleneck
  • Maps keys to stripes with a hash and keeps each shard behind its own lock
  • Provides an atomic compute so callers avoid get-then-put races
  • Acquires all stripes in a fixed order for whole-map operations
  • Notes the trade-off between stripe count and memory or iteration cost
Key Topics to Cover
lock striping
contention
atomic read-modify-write
lock ordering
sharding
How to Approach This
  1. Apply SOLID principles. Single Responsibility makes code testable, Open/Closed makes it extensible.
  2. Choose data structures based on access patterns, not familiarity.
  3. Prefer immutable data and message passing over shared mutable state for concurrency.
  4. Design APIs with RESTful conventions, versioning, meaningful errors, and pagination from day one.
Possible Follow-up Questions
  • How would you resize the number of stripes without stopping the world?
  • How do you iterate without holding every lock at once?
  • When would you use read-write locks per stripe instead?
  • What happens to throughput if most traffic hits one hot key?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice System Design Problems
Sample Answer
Requirements
  • get, put, and compute(key, fn) safe under concurrent use.
  • compute is atomic: concurrent increments of one key never lose updates.
  • size() returns a consistent count.
  • Operations on dif...
High-Level Design

Allocate S locks and S dicts. A key lives in shard hash(key) % S. Single-key operations take only that shard's lock, so threads touching different shards do not contend. compute runs the user ...


Submit Your Answer
Markdown supported