Caching, sharding and replication system design questions

13 system design interview questions on caching, sharding and replication, each with the correct answer and a full explanation.

A read heavy profile service uses cache aside with Redis. On a cache miss, what does the service do?
Show answer

Answer

Read the database, store the value in Redis with a TTL, return it
In cache aside the application owns the cache: on miss it reads the source of truth, populates the cache with a TTL and returns. Redis does not load from your database; that would be read through, which needs a cache layer that knows the database. Returning a miss to the client pushes the problem to callers. Rewriting the row changes nothing in the cache.

Source: Cache-Aside pattern

Cache aside, many concurrent writers, staleness must be short. What does the write path do?
Show answer

Answer

Update the database, then delete the cache key
Deleting after the database commit means the next read reloads fresh data, and deletes are idempotent so racing writers cannot leave an older value behind (a slow reader refilling an old value can still race a delete, which memcache leases close). Writing the cache first leaves a value that was never committed if the database write fails. Setting the new value races: two writers can commit A then B but set the cache B then A, leaving A forever. Waiting for TTL means stale reads for the whole TTL. Facebook's memcache uses delete on write for this reason.

Source: Cache-Aside pattern, Scaling Memcache at Facebook

A key read 10,000 times per second expires, and all those requests miss at once and hit the database. What fixes it?
Show answer

Answer

Let one request rebuild the key while the others wait or get the stale value
This is a cache stampede. Request coalescing (a short lock or lease on refill, as in Facebook memcache leases) or probabilistic early refresh means only one rebuild hits the database. A lower TTL makes stampedes more frequent. Replicas only raise the ceiling and still take a burst of identical queries. Memory has nothing to do with expiry.

Source: Scaling Memcache at Facebook

One key gets 500,000 reads per second, more than a single Redis shard can serve. The rest of the cluster is idle. What helps?
Show answer

Answer

Replicate that key: a short TTL in process cache, or copies under suffixed keys on several shards
A single key always maps to one shard, so adding shards or changing the hashing scheme still routes all 500k reads to one node. Spreading copies (key#1 to key#N read randomly) or a local cache in the app tier splits the load. Write through changes write behaviour, not read load on the key.

Source: Scaling Memcache at Facebook, System Design Interview: An Insider's Guide, Volume 1

View counters receive 1 million increments per second. The database handles 20,000 writes per second, and losing a few seconds of counts on a crash is acceptable. Which cache write strategy fits?
Show answer

Answer

Write back: increment in the cache and flush aggregated counts periodically
Write back (write behind) absorbs the 1M/s in memory and turns them into a small number of batched writes, at the cost of losing unflushed data on a crash, which the constraints allow. Write through and cache aside both send every increment to the database, 50x its capacity. Read through only describes the read path.

Source: The System Design Primer

A chat app stores messages. The main query is "latest 50 messages in conversation X", and group chats have up to 1,000 members. What is the shard key for messages?
Show answer

Answer

Conversation id
Sharding by conversation id puts every message of a conversation on one shard, so the main query hits one partition, as Discord does with channel plus time bucket. Message id scatters a conversation across all shards. Timestamp makes the newest shard take every write. Sender id splits a group conversation across up to 1,000 shards.

Source: How Discord Stores Trillions of Messages, Sharding pattern

A cache cluster places keys with hash(key) mod N on 10 nodes. You add an 11th node. Roughly what fraction of keys move?
Show answer

Answer

About 91%
A key stays only if hash mod 10 equals hash mod 11, which holds for about 10 in 110 values, so about 91% move and the cache effectively goes cold. About 9% (1/11) is what consistent hashing would move. 50% is a guess with no basis. Mod N placement has no notion of keys staying put.

Source: Consistent Hashing and Random Trees, System Design Interview: An Insider's Guide, Volume 1

A consistent hash ring has 4 physical nodes. Load is uneven, and when a node fails its whole range lands on a single neighbour. What fixes both?
Show answer

Answer

Give each node many virtual nodes on the ring
With many virtual nodes per server, each server owns many small ranges, so load evens out and a failed node's ranges spread over all survivors. Replication adds copies but the neighbour still inherits the range. Range partitioning has its own hot spot problems and does not spread failover. A larger hash space does not change how few points the 4 nodes occupy.

Source: Dynamo: Amazon's Highly Available Key-value Store, Consistent Hashing and Random Trees

An orders table is range partitioned on created_at. All writes land on the newest partition and it is saturated. What fixes it?
Show answer

Answer

Partition by a hash of order id or customer id
A monotonic key concentrates every insert on the latest range. Hashing a high cardinality key spreads writes across all partitions. Read replicas do not take writes. A bigger machine buys time but the hot spot remains. An auto increment id is just as monotonic as a timestamp.

Source: Sharding pattern, Bigtable: A Distributed Storage System for Structured Data

A DynamoDB table keyed by user id receives most of its writes on one celebrity's key, throttling that partition. Reads of that user's data can tolerate fanning out. What is the standard fix?
Show answer

Answer

Write sharding: append a random suffix 0 to N to the key and read all N on query
Per partition throughput is capped, so table capacity does not help a single hot key. Suffixing spreads one logical key over N partitions, and the reader merges N results. A GSI with the same key has the same hot spot. Consistency mode does not change write distribution.

Source: Amazon DynamoDB: Using write sharding to distribute workloads evenly

A user edits their profile, reloads and sees the old value because reads go to async replicas. Other users may lag a few seconds. What fixes it with the least cost?
Show answer

Answer

Route that user's reads to the leader for a short window after they write
Read your writes only needs to hold for the writer, so pinning their reads to the leader (or a replica caught up to their write position) for a short window is enough. Synchronous replication fixes it for everyone at a large write latency cost. Retrying until it changes cannot tell an unchanged value from lag. A cache in front adds another stale copy.

Source: Designing Data-Intensive Applications, The System Design Primer

A leader with asynchronous replication crashes and a follower is promoted. What is the risk?
Show answer

Answer

Writes the old leader acknowledged but had not replicated are lost
Async replication acknowledges before followers have the write, so failover can drop the tail, and if the old leader comes back it may hold conflicting writes (split brain). Followers only have what was shipped, not the dead leader's local WAL. Promotion does not need the old leader. Failover does not upgrade consistency.

Source: Designing Data-Intensive Applications

Users in the US and EU mostly write from their home region. Write latency must stay low, cross region RTT is 80 ms, and losing a few seconds of writes in a regional disaster is acceptable. Which setup fits?
Show answer

Answer

Home each user in one region and replicate asynchronously to the other
Homing users keeps writes local, and async replication gives a small RPO, which the constraints accept. Synchronous commits add at least 80 ms to every write. A single US leader makes EU writes slow and a US outage stops all writes. Last write wins across regions silently drops concurrent updates.

Source: Designing Data-Intensive Applications

These questions come from the System Design Interview Practice course in Bertopia.

More system design topics

Practise these on your commute