Interview practice Database replication and sharding interview questions 15 database interview questions on replication lag, sharding and hot keys, each with the correct answer and a full explanation.
Reads go to async replicas. A user saves a new name, reloads, and sees the old one. What is the fix?
Show answer Answer
Read own writes from the primary
The replica has not applied the write yet; routing the writer's reads to the primary for a while gives read-your-writes. A cache is just another lag source.
Source: Designing Data-Intensive Applications, Martin Kleppmann, O'Reilly, 2017 , StackScholar: Database replication and sharding interview questions
Postgres, one async standby. The primary's disk dies; you promote the standby. What can be lost?
Show answer Answer
Commits not yet sent to the standby
With async replication the primary acknowledges a commit before the standby has the WAL, so anything in that gap is gone after promotion.
Source: PostgreSQL Documentation: High Availability, Load Balancing, and Replication , Designing Data-Intensive Applications, Martin Kleppmann, O'Reilly, 2017
Postgres: the one standby in synchronous_standby_names dies. What happens to writes on the primary?
Show answer Answer
Commits hang until a standby is back
Each commit waits for the sync standby's confirmation, so with no standby commits block. Postgres never falls back to async on its own.
Source: PostgreSQL Documentation: Synchronous Replication
Reads go to random replicas. A new comment shows, vanishes on the next refresh, then returns. Fix?
Show answer Answer
Pin each user to one replica
Replicas lag by different amounts, so random routing can step back in time; sticking a session to one replica gives monotonic reads.
Source: Designing Data-Intensive Applications, Martin Kleppmann, O'Reilly, 2017
Postgres primary at 95% CPU from INSERT and UPDATE, reads light. Will read replicas help?
Show answer Answer
No, all writes stay on the primary
Replicas scale reads, but every write still runs on the one primary and replays on each replica. For write load, batch, scale up or shard.
Source: PostgreSQL Documentation: High Availability, Load Balancing, and Replication , StackScholar: Database replication and sharding interview questions
Messages sharded by hash(message_id) make a chat's latest 50 hit every shard. Which key fixes it?
Show answer Answer
conversation_id
Sharding by the key the hot query filters on puts a conversation on one shard, so the read hits one node. created_at puts all new messages on one shard.
Source: Hello Interview: Core Concepts for System Design , Designing Data-Intensive Applications, Martin Kleppmann, O'Reilly, 2017
Followers shard by hash(user_id). A celebrity with 80M followers makes one shard hot. What helps?
Show answer Answer
Salt the hot key into sub-keys
One key always hashes to one shard, so user_id plus a suffix spreads its writes, and reads gather the sub-keys or hit a cache.
Source: Amazon DynamoDB Developer Guide: Using write sharding to distribute workloads evenly , Designing Data-Intensive Applications, Martin Kleppmann, O'Reilly, 2017
An events store is range-sharded by created_at. What problem shows up under steady ingest?
Show answer Answer
All new writes hit the newest shard
Monotonic keys make the newest range a single write hotspot. Prefix with a well-spread key such as source_id and keep time second.
Source: Designing Data-Intensive Applications, Martin Kleppmann, O'Reilly, 2017 , Amazon DynamoDB Developer Guide: Designing partition keys to distribute your workload
Keys go to hash(key) mod N. You go from 4 shards to 5. Roughly what fraction of keys must move?
Show answer Answer
About 80%
A key stays only if hash mod 4 equals hash mod 5, true for 4 of every 20 residues, so about 80% move. Consistent hashing moves about 1/5 instead.
Source: Consistent Hashing and Random Trees, Karger et al., 1997 , Dynamo: Amazon's Highly Available Key-value Store, DeCandia et al., 2007
Postgres primary: 300 GB, 3,000 queries/s, 35% CPU, a few slow queries raise p99. Best next step?
Show answer Answer
Fix slow queries and indexes first
This load fits one node: fix the slow queries and indexes, then add replicas or a bigger box. Sharding adds cross-shard queries and rebalancing for no gain.
Source: Designing Data-Intensive Applications, Martin Kleppmann, O'Reilly, 2017 , Hello Interview: Core Concepts for System Design
Users are sharded by user_id. Why avoid 2PC for a money transfer across two shards?
Show answer Answer
2PC blocks while the coordinator is down and holds locks across the network.
Source: Designing Data-Intensive Applications, Martin Kleppmann, O'Reilly, 2017 , Pattern: Transactional outbox, Chris Richardson, microservices.io
Users are sharded by user_id. How does a saga move money between two shards?
Show answer Answer
Debit the sender with an idempotent ledger entry and an outbox event, then credit the receiver idempotently using the transfer id.
Source: Designing Data-Intensive Applications, Martin Kleppmann, O'Reilly, 2017 , Pattern: Transactional outbox, Chris Richardson, microservices.io
Cross-shard transfer saga: what if the credit step fails permanently?
Show answer Answer
Run a compensating credit back to the sender.
Source: Designing Data-Intensive Applications, Martin Kleppmann, O'Reilly, 2017 , Pattern: Transactional outbox, Chris Richardson, microservices.io
Cross-shard transfers: how do retries and reconciliation find stuck ones?
Show answer Answer
Keep a transfer row with its state.
Source: Designing Data-Intensive Applications, Martin Kleppmann, O'Reilly, 2017 , Pattern: Transactional outbox, Chris Richardson, microservices.io
Transfers between users on different shards are frequent. What do you reconsider?
Show answer Answer
The shard key, so related accounts live together.
Source: Designing Data-Intensive Applications, Martin Kleppmann, O'Reilly, 2017 , Pattern: Transactional outbox, Chris Richardson, microservices.io
These questions come from the Database Interview Practice course in Bertopia.
More database topics Practise these on your commute