My account

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.

Not reviewedWritten with AI helpReading time: 15 minOnline shop exampleBlast radiusCell router

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

One shared systemCheckout (bad config)Orders, Catalog, PaymentOne database, one queue4,000 stores failFour cellsCell 1services + dataCell 2bad configCell 3services + dataCell 4services + data1,000 stores fail, 3,000 work
Left: one shared system, so one bad change reaches every store. Right: four cells, each with its own services and data. The bad change reaches only the stores in cell 2.

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.

  1. Stores 1 to 1,000 live in cell 1, stores 1,001 to 2,000 in cell 2, and so on.
  2. 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.
  3. A noisy store can only slow down its own cell.
  4. 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

Requeststore=1042Cell routercell map (in memory)1-1000 → cell-11001-2000 → cell-22001-3000 → cell-3Cell 1Cell 2Cell 3no business logic, no database call
A request comes in with the store id. The router looks up the store in the cell map and sends the request to that cell. The router does no business work.

Something must send each request to the right cell. This is the cell router (some teams call it the cell gateway).

  1. 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.
  2. The router looks up the key in a cell map: store id to cell.
  3. 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:

  1. No business rules, no calls to a database on each request. It keeps the cell map in memory and refreshes it in the background.
  2. If the map service is down, the router keeps using the last map it has.
  3. 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:

  1. 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.
  2. 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.
  3. 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:

  1. Wave 0: a small cell that serves only internal test stores.
  2. Wave 1: one real cell. Watch error rate and latency for some time.
  3. Wave 2 and more: the other cells, a few at a time.
  4. 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:

  1. 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).
  2. 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.
  3. 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.
  4. 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.
  5. 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:

  1. You have 8 workers. Each store gets a random set of 2 of them.
  2. A noisy store can break only its 2 workers.
  3. 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.
Cells and multi-tenancy: Cells are one way to place tenants. You can also put each tenant in its own database, or many tenants in one database. The Multi-tenancy lesson compares these choices.

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

  1. Blast radius is the share of customers one failure reaches. Cells make it small on purpose.
  2. A cell is a full copy of the system, with its own data, serving only some customers.
  3. A thin router uses a partition key, like the store id, and a cached cell map.
  4. Choose a key that most requests need alone, and that fits in one cell.
  5. Deploy code and config cell by cell, in waves, and stop on bad signals.
  6. Reports, global data and customer moves need their own design.
  7. Use cells when an outage is very costly and customers are mostly separate.

Done reading this lesson?Mark it as finished so it shows in your lessons list and report card.