Systemsfor Aravinda
0 / 78 core
Week 7 · ArchitectureAdvanced

Cell-based architecture

1.5 hours · optional track

Read first: Service boundaries, Load shedding

In one sitting

A cell is a full slice of the stack that serves a subset of users, so a failure stays inside that slice. Shuffle sharding places a tenant on a subset of cells so two tenants rarely share an exact fate, and a routing layer sends each request to its cell. A new-grad loop usually should not reach for cells.

A new-grad loop usually should not reach for cells, and it usually does not ask for them. The graded answer is one service, one database, and a plan for the hot key. This page is still worth an evening, because cells are how a large fleet keeps one tenant’s failure from becoming everyone’s outage, and they cost a full copy of the stack per slice.

Core concepts

A cell is a full slice of the stack that serves a subset of users. The API, the workers, the cache, and the database that hold those users’ data all sit inside the cell. It is not a shard of one table with the rest of the product still shared. A partition splits one dataset so a key has a home. A cell splits the failure domain so a bad deploy, a full disk, a poisoned cache, and a pathological query take out the tenants in that slice and leave the others up. If the cells share a database, you drew a costume. The query will cross the costume.

Blast radius is the set of users who feel the failure. In one shared stack the radius is everyone. In eight cells with tenants spread evenly, a dead cell is about an eighth of the product, which is still bad, and it is not the whole company. You chose that fraction when you chose the number of cells and the placement. Putting every large tenant in the first cell, because they signed up early, quietly puts the radius back. The biggest customers fail together, which is the failure you were trying to break.

Shuffle sharding places a tenant on a subset of cells, or of the nodes in a stateless tier, so two tenants rarely share the exact same set. A failure of one combination hits the tenants on that combination, not every tenant who happened to touch any popular node. It fits a tier where any member of the subset can do the work: a stateless API fleet, a fanout worker, an edge that does not own the row. It does not, by itself, save a database that all of those tenants share. If the damage is one query against a shared store, you needed a cell boundary around the store, not a clever assignment of stateless replicas in front of it.

The arithmetic is small and worth doing once so the phrase is not magic. Eight cells, and each tenant is assigned a pair, uniformly. The number of pairs is 8 times 7 divided by 2, which is 28. Fix one tenant’s pair. A second tenant draws the same pair in 1 of 28 assignments. A given cell sits in a tenant’s pair with probability 2/8, so a dead cell is in the subset of about a quarter of the tenants. The other three quarters do not send it work. That is a derivation from the assignment rule, under the assumption that any cell in the pair can serve the tenant. The moment the tenant’s data lives in exactly one home cell, you are back to blast radius equals that cell, and shuffle sharding is the wrong picture for the data plane. Use it where the work can move. Use a home cell where the bytes cannot.

Routing sends the user to their cell. The map is tenant id to cell id. It is small, it is cacheable, and it has to be more available than a cell, or you have moved the single point of failure upward and named it a router. Keep it thin. It does not apply pricing, and it does not authorize the row. A wrong map entry sends a tenant to a cell that does not have their data, which looks like data loss and is a routing bug. A thick router, full of business rules, is a shared fate across every cell: one bad deploy of the router darkens the fleet you split apart so that a bad deploy would stay local.

A later cellular design at Slack kept calls inside one zone, so a failure would not fan across zones. That is the same idea. The call graph stays inside the slice that is allowed to fail together. Cross-cell calls on the path the user waits for undo the cell. You are back to a chain whose availability multiplies, except now the chain also crosses a boundary you promised was a firewall. Latency across zones is already a different budget from a same-zone hop. A cell that reaches into another cell for the user’s request pays that budget and imports the other cell’s failure.

What you copy into a smaller system is the instinct, not the fleet. A bulkhead is a separate pool so one noisy dependency cannot spend every thread. Load shedding rejects the expensive query when the pool or the queue is past a bound. Those are cells at the size of one process. A five-person system runs them. It does not run eight full stacks, eight databases, and a routing layer so that a design diagram has the word “cell” on it.

flowchart TB
  router[Routing layer]
  home[Home cell]
  quiet[Quiet cells]
  bad[Bad query]
  db[Home database]
  router --> home
  router --> quiet
  bad --> home
  home --> db

The bad query enters the home cell and stops at that cell’s database. The quiet cells have no arrow from it. If the database were drawn once, outside the cells, the arrow would hit everyone and the boxes would be decorative.

A worked example

One tenant runs a bad query. The product is multi-tenant. The report scans that tenant’s entire history and saturates the database it lands on: CPU pegged, connection pool full, ordinary requests on that database timing out. You have seen this without any cells. The difference is who else is on that database.

Assumptions:

  • 8 cells. Each cell is a full slice: its own API fleet, its own workers, its own database.
  • 1,000,000 tenants, placed evenly, one home cell each. A planning assumption about size, not a measured company.
  • The bad query is heavy enough to make its database miss deadlines for everyone else on it. You do not need a QPS figure for that. One scan can do it.
  • Shuffle sharding shows up as a second placement, for a stateless tier, and you should see that it does not save the database the scan already found.
tenants per cell
1,000,000 / 8 = 125,000
fraction in the blast radius
125,000 / 1,000,000 = 0.125
one dead cell is 12.5% of tenants, not 100%

shuffle shard, stateless tier
8 cells, pair per tenant
pairs = 8 * 7 / 2 = 28
a second tenant matches a fixed pair in 1/28 of assignments
a given cell is in the pair with probability 2/8 = 0.25
the other 0.75 do not send it traffic

Tenant Huge lives on cell 3. The report runs there. Cell 3’s database saturates. Tenants on cell 3, all 125,000 of them, see timeouts. Cells 1, 2, and 4 through 8 stay at their normal latency. You have lost an eighth of the product. You have kept seven eighths. That is the point of the slice, and it is still an incident for the eighth. Do not describe 12.5% as fine. Describe it as the radius you chose instead of 100%.

Inside cell 3 you shed the report. It is the expensive traffic, and you can tell it apart from the interactive reads if you bothered to mark it. Reject it, or run it only when the pool has spare capacity, with a bound on how many such scans may run. A queue of unbounded reports is a memory leak that will finish the cell the shed was supposed to save. You do not add capacity to all eight cells because one tenant wrote a bad report. You do not restart the fleet. The other cells were never sick.

If Huge’s data had lived in a database shared with every tenant, the same scan would have taken the product down. More partitions of that one database do not save you when the scan is one hot key or one tenant’s range that already sits on one node: a hot key is not fixed by more partitions. A cell works because the other tenants’ bytes, and the other tenants’ queries, are on different machines. Placement did that. Hardware did not.

Now the shuffle-sharding confusion, because it will get offered as the fix. Suppose the stateless API is eight replicas and each tenant is assigned a pair. Huge’s report traffic, if it were CPU on the API, would land only on Huge’s pair. Tenants whose pair is disjoint feel nothing. About a quarter of tenants include any one dead replica and can fall over to the other member of their pair, under the assumption that either member can serve them. That is a real technique, and it is the wrong one for this incident. The report’s damage is the database in the home cell, and every stateless replica in front of that database is a different door to the same fire. Assigning pairs does not move Huge’s rows out of cell 3. Do not answer “shuffle shard” when the question is “this tenant’s query killed the store.”

What about the other large tenants? If they all landed in cell 3 because cell 3 was the original stack and you were afraid to move them, the even-spread arithmetic is a fiction. The blast radius is “everyone who pays us,” which is the shared-fate design again. Placement of the large tenants is the job. Even spread of a million small ones will not save you if the ten that matter share a cell.

Routing in this example is a map from tenant id to cell id, cached at the edge. Huge always goes to cell 3. A request with no map entry does not get sprayed at random. Spraying is how you would run the report against a random cell that does not have the data, or against every cell. Missing entries fail, or they go to an explicit default you can watch. The router stays boring. The week someone adds “while you are routing, also compute the discount” is the week a pricing bug darkens all eight cells at once.

Moving Huge off cell 3, after the incident, is a migration. Copy their data to the destination cell, cut reads and writes over, update the map, and keep the old cell able to serve until the cut is done. A config flip that changes the map first is a tenant pointed at an empty cell. You will be tempted because the map is one row. The bytes are not one row.

Operate the cell as its own little product. Deploys roll one cell, watch that cell’s user-facing SLI, then continue. A canary that is global is not a canary. Metrics stay on the cell. A label per tenant id will take the metrics system down. Cardinality is not a reason to skip the page. It is a reason to page on the cell’s rate, errors, and duration, and to look up the tenant from the request log when you already know which cell is hot. Observability of eight cells is eight places to look, which is the operational cost you accepted in exchange for the radius.

Tradeoffs

Choice What you gain What you spend
One shared stack Joins, one deploy, one database to understand One tenant’s query is everyone’s outage
Cells, one home cell per tenant Blast radius is about 1/N of tenants if placement is even N stacks to deploy, and a move is a data migration
Shuffle sharding on a stateless tier Two tenants rarely share an exact fate. A dead node touches only tenants assigned to it It does not isolate a database those tenants still share
Cells so small they approach one tenant each The noisiest tenant sits alone Cost and toil grow with the tenant count. A small team cannot run this
Thick routing layer Smart placement, maybe a feature along the way The router is shared fate for every cell, and a domain bug darkens all of them
Bulkhead and shed, no cells The noisy dependency or query cannot spend the whole process A bad query can still saturate the one shared database. You have not bought fleet isolation

The last row is the right trade until the shared database is actually being taken down by one tenant and shedding is not enough. Cells are that next step. They are not the first step.

Pitfalls

  • A cell diagram whose database is still shared. The symptom is the bad query slowing every tenant, while the architecture review claims the blast radius is one cell.
  • Every large tenant left on the original cell. The symptom is an even tenant count and an incident that still hits the customers you cannot lose.
  • Cross-cell calls on the user path. The symptom is a failure in cell 3 showing up in cell 1’s latency, because cell 1 waited on cell 3. Keep the calls inside the cell that is allowed to fail.
  • A router that contains the business rule. The symptom is one bad routing deploy taking every cell down, which is the outage the cells were built to prevent.
  • A map change that precedes the data copy. The symptom is a tenant whose requests succeed at the router and return nothing from the new cell.
  • Tenant id as a metric label. The symptom is the metrics system falling over, during the incident, because cardinality tracked your users instead of your cells.
  • Answering a hot database with shuffle sharding of the stateless tier. The symptom is the API looking well isolated while the one store behind it is still on fire.
  • Building eight cells for a five-person team because a large company did. The symptom is eight deploys and no one watching seven of them. Copy the bulkhead. Leave the fleet.

In the interview

Do not draw cells in the first five minutes. “One service, one database, tenants share it. The risk is one tenant’s scan saturating that database. I shed or cap that query, and I keep a separate pool so it cannot take every thread. I am not introducing cells unless one tenant can sink the fleet and a cap is not enough.” That is a path, a bottleneck, and a tradeoff: shared fate, against an operational shape you can actually run.

If they push, define the cell as a full slice, not a table shard. Give the number. Eight cells, even placement, one bad query, 12.5% of tenants, the other cells healthy, shed inside the sick cell. Mention the router as a thin map. Mention that shuffle sharding is how two tenants avoid the exact same subset on a tier where any member can serve, and that it does not move rows. Skip the 28-pair formula unless they ask how often two tenants share a fate. You can derive it in twenty seconds if they do.

The question that usually follows is “how does a tenant move?” Copy the data, cut traffic, then update the map, and keep the old cell warm until the cut finishes. A wrong map looks like data loss. The second follow-up is the shared router. Say you would rather have a boring map than a smart one, because the map’s blast radius is every cell. One honest cost: you now deploy and watch N stacks. If you cannot staff that, you do not have cells. You have a drawing.

What to copy at early career is the radius. Cap the query, isolate the pool, do not let one client spend the process. What not to copy into a five-person system is the full cellular fleet. You can keep your calls inside one process, which is the same containment at a size you actually operate.

Check yourself

How is a cell different from a shard of one table?

A shard is a slice of one dataset, chosen so a key has a home and a hot range stays a hot range. A cell is a slice of the whole stack for a subset of users: the services and the data, failing together. A shard with a shared API, a shared cache, and a shared pool is still one blast radius. More shards do not contain a query that can saturate the node it already landed on, and they do not contain a bad deploy of the shared API.

Eight cells, one million tenants, even spread, one home cell each. A bad query kills the home cell. What fraction of tenants are in the blast radius?

125,000 tenants, which is 12.5 percent. The other seven cells do not see the query, provided they do not share the database and the sick cell is not called from the healthy ones on the user path. 12.5 percent is the radius you bought. It is not a claim that the incident was small. If the large tenants were all placed in that cell, the fraction of accounts is a vanity number and the fraction of the business is not.

Each tenant is assigned 2 of 8 cells, uniformly, and either cell can serve them. What is the chance a second tenant shares the exact pair?

There are 28 pairs, because 8 times 7 divided by 2 is 28. The second tenant matches a fixed pair in 1 of those 28 assignments, if assignment is uniform. A single dead cell sits in about 25 percent of pairs, since 2/8 is 0.25, and those tenants can still be served by the other cell in the pair. This limits shared fate on a tier where any assigned cell can do the work. It does not relocate a tenant’s rows off the database the bad query already saturated.

Why do calls from one cell into another, on the path the user waits for, give the blast radius back?

The caller waits on the callee, so a failure or a slow scan in the second cell shows up in the first cell’s latency and errors. The radius is the chain, not the box you drew. Keep service calls inside the cell so a failure does not fan into the cells that were healthy. A thin router in front is shared on purpose and has to stay too boring to fail in an interesting way.

Based on: Slack Engineering; Google SRE book and SRE workbook; Designing Data-Intensive Applications, Kleppmann and Riccomini.