Cell-based architecture
A cell is a full, independent copy of the system that serves only a part of the customers. A thin router sends each customer to its cell. When one cell breaks, from a bad deploy, a bad config or a noisy customer, only the customers in that cell feel it. You grow by adding cells, not by making one copy bigger. The price is more copies to run, and harder work for anything that must see all customers.
Author: bezzad
Contents
The problem: one bad change, every customer down
Our online shop is now a platform: 4,000 small stores sell on it. All of them use the same services, the same database cluster and the same queue.
On Black Friday, a developer changed one config value in the checkout service. The value was wrong. In two minutes, checkout failed for all 4,000 stores at the same time. Rolling back took 25 minutes.
A month later, a different problem: one big store ran a flash sale with a bot that sent 50 times its normal traffic. The shared database got slow, and every other store got slow with it.
The services were already split well. Each service had several copies behind a load balancer. But all the copies ran the same code and the same config, on the same data. So one mistake reached everyone. The architect’s question is no longer “how do we avoid every mistake?” It is “how many customers does one mistake reach?” That number is called the blast radius.
The idea: many small copies of the whole system
A cell is a complete copy of the system: its own services, its own database, its own queue and cache. Each cell serves only a part of the customers, and it does not call other cells.
- Stores 1 to 1,000 live in cell 1, stores 1,001 to 2,000 in cell 2, and so on.
- A bad config is deployed to one cell first. If it fails, a quarter of the stores (or fewer, with more cells) see it, not all of them.
- A noisy store can only slow down its own cell.
- To grow, you add a new cell. Every cell has the same, tested maximum size, so you never meet a size you have not tested.
This is different from normal scaling. Normal scaling adds more copies of one service, but they still share one database and one deploy. A cell is a copy of everything, with a hard wall around it.
The cell router
Something must send each request to the right cell. This is the cell router (some teams call it the cell gateway).
- Every request carries a partition key: here the store id. It comes from the domain name (
shoe-store.shop.example), a token claim or the URL. - The router looks up the key in a cell map: store id to cell.
- It sends the request to that cell, and nothing else.
The router is the only part shared by all cells, so it is now the most dangerous part. Keep it thin and boring:
- No business rules, no calls to a database on each request. It keeps the cell map in memory and refreshes it in the background.
- If the map service is down, the router keeps using the last map it has.
- It changes rarely, and it is deployed with even more care than the cells.
A small example of the lookup:
// The map is loaded at start and refreshed every 30 seconds.
public sealed class CellMap(IReadOnlyDictionary<string, string> map)
{
public string CellFor(string storeId) =>
map.TryGetValue(storeId, out var cell)
? cell
: "cell-new"; // new stores go to the newest cell
}
Choosing the partition key
The key decides everything, so choose it with care:
- Most requests must need only one key. In our shop, almost every request belongs to one store. That is a good key. If many requests needed data from several stores, cells would have to talk to each other, and the wall is gone.
- One key must fit in one cell. If one store can grow bigger than a whole cell, you need a plan: a bigger cell size for it, or a cell only for that store.
- The key must not change. Moving a store to another cell is possible, but it is a slow and careful job (see below).
Deploying cell by cell
The biggest win of cells is in deploys. You deploy in waves:
- Wave 0: a small cell that serves only internal test stores.
- Wave 1: one real cell. Watch error rate and latency for some time.
- Wave 2 and more: the other cells, a few at a time.
- If any wave looks bad, the deploy stops automatically and rolls back that cell.
The same rule is used for config changes, feature flags and database migrations. A change that goes to all cells at once breaks the main promise of cells.
The hard parts
Cells are not free. Know these costs before you choose them:
- Work that needs all customers. A report “sales of all stores today” can not read from one cell. Each cell sends events to a separate analytics store, and reports read from there (see the Data Ownership lesson).
- Global data. Some data belongs to no store, like user accounts that buy from many stores. Keep it in a small global service, and accept that this service has a big blast radius. Keep it small and very stable.
- Moving a customer between cells. Copy the data to the new cell, keep both in sync for a while, switch the cell map, then delete the old copy. Build and test this tool before you need it.
- Cost and operations. Each cell has its own database and cache. With 20 cells, you run 20 of each. You need very good automation: creating a new cell must be one command.
- Observability. Dashboards must show every metric per cell. “Error rate 2%” means nothing if it is 0% in 19 cells and 40% in one.
Shuffle sharding: a smaller blast radius for less money
Sometimes the problem is not a bad deploy but one noisy customer. Shuffle sharding helps here without full cells:
- You have 8 workers. Each store gets a random set of 2 of them.
- A noisy store can break only its 2 workers.
- Most other stores share one of those workers or none, so they still have a healthy one. The client retries on the other worker of its set.
With 8 workers and sets of 2, there are 28 different sets. So only about 1 store in 28 has exactly the same two workers as the noisy one. This idea is used inside a cell, or for shared parts like the router.
When to use cells
| Situation | Cells? |
|---|---|
| Many customers or tenants, each one mostly separate | Good fit |
| One outage costs a lot of money or trust | Good fit |
| A small product with one team | Too much work. Use good deploy practice first. |
| Most requests need data from many customers | Poor fit. The cells would need to talk all the time. |
| You can not automate creating an environment yet | Not yet. Build the automation first. |
Common mistakes
| Mistake | Result | Better way |
|---|---|---|
| Cells call each other directly | One cell’s failure spreads to the others. | Cells never call each other. Share data with events. |
| Business logic in the router | The shared part breaks often, and takes all cells down. | Keep the router a thin lookup with a cached map. |
| Deploying to all cells at once | The blast radius is everyone again. | Deploy in waves and stop on bad metrics. |
| No maximum cell size | One cell grows until it fails in a new way. | Set and test a maximum size. Add a new cell when it is near. |
| Metrics only for the whole system | One broken cell hides in the average. | Show every metric per cell. |
| No tool to move a customer | A big customer gets stuck in a full cell. | Build and test cell moves early. |
Summary in 7 lines
- Blast radius is the share of customers one failure reaches. Cells make it small on purpose.
- A cell is a full copy of the system, with its own data, serving only some customers.
- A thin router uses a partition key, like the store id, and a cached cell map.
- Choose a key that most requests need alone, and that fits in one cell.
- Deploy code and config cell by cell, in waves, and stop on bad signals.
- Reports, global data and customer moves need their own design.
- Use cells when an outage is very costly and customers are mostly separate.