More cashiers, then more shops
Vikram's online grocery, SabziBasket, runs on one PostgreSQL server. It has 2 million customers, and every morning rush pushes the database a little closer to the edge. (Vikram, SabziBasket and every number here are made up for teaching.)
Vikram's father ran a real grocery shop, and he scaled it in two steps:
- He added a second cashier with a copy of the price list. That is replication: the same data, more hands.
- When that wasn't enough, he opened a second shop selling half the catalogue. That is sharding: different data on different machines.
Each step solved one problem and created another. The copies of the price list drifted apart, and a customer who wanted items from both shops now needed a plan.
SabziBasket goes through the same steps, and each one breaks something:
| What breaks |
Idea it forces |
| "Reads are drowning the database" |
Replication topologies |
| "The app said 'not shipped' for an order that had shipped" |
Sync vs async replication |
| "Mumbai and Delhi both edited the price of onions" |
Multi-leader conflicts |
| "Writes no longer fit on one machine" |
Sharding and the shard key |
| "One shard is on fire during the mango sale" |
Hot partitions |
| "Adding a fifth shard moved 80% of the data" |
Rebalancing |
| "Support can't find an order by its number" |
Global secondary indexes |
| "Payment held, but stock ran out" |
2PC, sagas, the outbox |
| "Move off the monolith, and keep EU data in the EU" |
Migration and residency |
This guide follows Inside a database and uses the vocabulary in Consistency models.
1. "Reads are drowning the database": replication
Ninety per cent of SabziBasket's traffic is customers browsing and checking orders. Vikram adds read copies, which are the extra cashiers.
flowchart TD
subgraph SL["Single leader"]
W1@{ icon: "algomi:svc-cube", label: "Writes", pos: "b", h: 52 }
L1@{ icon: "algomi:data-cylinder", label: "Leader", pos: "b", h: 52 }
W1 --> L1
F1@{ icon: "algomi:data-cylinder", label: "Follower", pos: "b", h: 52 }
L1 --> F1
F2@{ icon: "algomi:data-cylinder", label: "Follower", pos: "b", h: 52 }
L1 --> F2
end
subgraph ML["Multi-leader"]
W2@{ icon: "algomi:svc-spark", label: "Writes in Mumbai", pos: "b", h: 52 }
LA@{ icon: "algomi:data-cylinder", label: "Leader Mumbai", pos: "b", h: 52 }
W2 --> LA
W3@{ icon: "algomi:svc-cube", label: "Writes in Delhi", pos: "b", h: 52 }
LB@{ icon: "algomi:data-cylinder", label: "Leader Delhi", pos: "b", h: 52 }
W3 --> LB
LA <-->|sync, may conflict| LB
end
subgraph LL["Leaderless"]
W4@{ icon: "algomi:svc-cube", label: "Writes and reads use quorums", pos: "b", h: 52 }
N1@{ icon: "algomi:data-cylinder", label: "Replica", pos: "b", h: 52 }
W4 --> N1
N2@{ icon: "algomi:data-cylinder", label: "Replica", pos: "b", h: 52 }
W4 --> N2
N3@{ icon: "algomi:data-cylinder", label: "Replica", pos: "b", h: 52 }
W4 --> N3
end
You've reached the free preview of this post.
Subscribe to the Blogs plan (₹99/mo) — or use any Algomi Suite, Career, Investor, or Trader plan — to keep reading.
See plans