What problems does sharding introduce, and how are they handled?
HardConceptDatabases: Replication & Sharding
Answer
Sharding removes the single-node limit but gives up features that a single database provides for free. Expect follow-up questions on joins, transactions, hot spots and resharding.
- Cross-shard queries and joins: denormalise data so common queries hit one shard, or aggregate results in the application (scatter-gather).
- Distributed transactions: avoid them with a careful shard key; otherwise use two-phase commit, or sagas with compensating actions.
- Hot spots: a celebrity account can overload one shard; split hot keys further, add a random suffix, or cache aggressively.
- Resharding: moving data as the cluster grows is costly, so create many logical shards up front and map them to fewer physical nodes, or use consistent hashing.
- Global secondary indexes and unique constraints need separate index tables or services.
More in Databases: Replication & Sharding
| # | Problem | Platform | Difficulty | Open | 1st1st Visit | 2nd2nd Visit | RevRevise |
|---|---|---|---|---|---|---|---|
| 1 | How do you choose between a SQL and a NoSQL database? | Concept | Easy | Read the answer to How do you choose between a SQL and a NoSQL database? | |||
| 2 | What is database replication, and how do synchronous and asynchronous replication differ? | Concept | Medium | Read the answer to What is database replication, and how do synchronous and asynchronous replication differ? | |||
| 3 | What is sharding, and how do you choose a shard key? | Concept | Medium | Read the answer to What is sharding, and how do you choose a shard key? | |||
| 4 | What problems does sharding introduce, and how are they handled? | Concept | Hard | Read the answer to What problems does sharding introduce, and how are they handled? |