Nikita Makovei

Case II · Sharding and the hot tenant

The Many Wells

One database became many. The data spread out evenly, except for the one customer who was larger than a whole shard.

Where
TextMagic
When
2024 – 2025
Pattern
Sharding and the hot tenant

I · Departure

One well is not enough

At TextMagic I optimized queries and schema on a sharded PostgreSQL cluster, and the SMS and voice API endpoints that read from it. The platform serves more than 25,000 businesses, and they are not the same size.

II · Initiation

Even on paper, uneven in life

Sharding splits rows between databases by a key, usually the tenant. A good hash spreads tenants evenly. It does not spread rows evenly, because one tenant can own a quarter of them.

Two things are worth seeing with your own eyes:

  • The hot tenant. Adding shards makes every bar lower except one. A tenant cannot be split by a key it does not have.
  • The price of a resize. With hash mod N, adding one shard moves most rows. With consistent hashing, it moves about one share.

Sandbox · Sharding and the hot tenant

A model, not production data

Press the steps from 1 to 4, then press "Add a shard" and compare the two bars at the bottom.

III · Return

What it cost

Sharding buys write capacity and pays with every query that crosses shards. The work is then less about the cluster and more about the queries: keep them inside one shard, and give the largest tenants their own plan.

Patterns in this case

What to call these things

Sharding Data
Rows are split between several databases by a key, usually the customer. Each database stays small enough for one machine. In the sandbox: the cylinders.
Hot tenant Multi-tenancy
One customer is much larger than the rest. A key spreads customers evenly, not rows, so this customer's shard is always the fullest. In the sandbox: the coloured band in step 2.
Pool and Silo Multi-tenancy
Most customers share databases (pool). The largest get a database of their own (silo), so they can be scaled and moved alone. In the sandbox: step 4, the dashed cylinder.
Consistent hashing Data
Keys and shards sit on one ring, so a new shard takes rows only from its neighbours. With hash mod N almost every row moves. In the sandbox: the two bars after a resize.
Scatter–Gather Integration
A query without the shard key must ask every shard and merge the answers. It is the main price of sharding. In the sandbox: not drawn; it is the reason to keep queries inside one cylinder.