AlgoViz

Introduction to data partitioning

easy 25 min

Data partitioning splits one large set of data into smaller parts by a rule, so that each part is easier to store, search and maintain.

Chapter mnemonic

Three ways to route a shard key

Read the shelf range, spin the hash wheel, or ask the desk.

You move a single tenant by editing a lookup entry. Which scheme is that?

Directory partitioning stores an explicit mapping from key to shard.

Video lesson · Chapter 1 of 5

When one directory fills its machine

0:00 / 3:40

Theory

Read or listen first. About 2 min.

What it is

Data partitioning means cutting one large set of data into parts and using a rule to say which part holds which row. Every row lives in exactly one part. Put all the parts together and you have the whole set again.

The parts may sit inside one database on one machine, or on several machines. When each part has a machine of its own, the parts are called shards and the method is called sharding.

Partitioning is different from replication. Partitioning gives each machine different rows. Replication gives several machines the same rows. Large systems usually do both.

Why it exists

A single table can outgrow its machine in four ways: the disk fills up, the indexes no longer fit in memory, the processor cannot answer all queries, or the network card is full. Smaller parts help with each of these, if they are placed on different machines.

Even on one machine, parts are useful. A question about March can skip the other months, and deleting an old month means dropping one small table instead of deleting millions of rows.

The price is a rule you must live with. Questions that follow the rule get faster. Questions that ignore it have to visit every part.

A short history

  1. 1983

    Teradata delivers the first shared-nothing database system, where each node owns part of the data and nodes share neither memory nor disk.

  2. 1986

    Michael Stonebraker uses the name shared nothing in a paper that argues for this design.

  3. 1997

    The online game Ultima Online runs copies of its world called shards. This is one common account of where the word came from.

  4. 2006

    Google describes Bigtable, which splits tables into ranges of rows called tablets.

  5. 2017

    PostgreSQL 10 adds declarative table partitioning, so parts can be declared in the table definition.

Words to know

Partition
One part of the data, chosen by the rule.
Shard
A partition that lives on its own database server.
Partition key or shard key
The field the rule looks at, for example user_id.
Cardinality
How many different values a field can have. Two for yes or no, millions for user_id.
Pruning
Skipping the partitions that cannot hold the answer.
Cross-shard query
A question whose answer is spread over several shards.
Skew
When some partitions hold or serve much more than others.

How it works

Step through the idea, or press Play.

Step 1 of 3

Choose a key. A field with many values that appears in your main query

Understand it

Start here

The jigsaw story

A giant jigsaw lies on one table, and finding a piece takes forever. Sorting the pieces into trays by colour makes every search shorter. The colour is the rule, and it decides which searches become easy and which stay hard.

One jigsaw, three trays

Another everyday version: a pizza too big for one plate is sliced, and each slice gets a plate of its own. Each plate is a shard.

The idea

What partitioning does

Take a table with 100 million users on one overloaded server. Apply the rule user_id mod 3. Each of three shards now holds about 33 million users, with its own disk, memory and processor.

Three machines, one rule

Users ask for user 7. The orders service computes user id mod 3, gets 1, and goes to shard 1, which holds users 1, 4, 7 and so on. Shard 0 and shard 2 are not contacted.
Each thick border is one machine. The blue arrow is the only one used for user 7.
  • Horizontal scaling

    Add servers to hold more data and take more writes.

  • Better performance

    Each shard answers fewer queries over a smaller index.

  • Shard key

    The field that decides where a row lives.

  • Data distribution

    A good key spreads rows and work evenly.

Rows per shard

100 million3≈33.3 million12345 mod 3=0\frac{100\ \text{million}}{3} \approx 33.3\ \text{million} \qquad 12345 \bmod 3 = 0
User 12345 lives on shard 0, because 12345 divides by 3 exactly. Simple remainder rules are easy to check by hand.

Why

The limits of one server

What runs out, and what a split gives you

LimitWhat you seeAfter splitting over machines
StorageThe disk fills upTotal space grows with each shard
MemoryIndexes no longer fit in RAMEach shard holds only its own indexes
CPUToo many queries a secondQueries are shared out
NetworkOne card carries all trafficEach shard has its own card

Three shards give at most about three times the capacity, when load is even and almost every query goes to one shard. Cross-shard work and skew take a share of that gain.

Scale out (partition)

  • More servers of ordinary size
  • Grows far beyond one machine
  • Losing one shard affects only its rows
  • Needs a rule, a router and care

Scale up (one bigger server)

  • Simple, no code change
  • Ends at the largest machine you can buy
  • The very largest machines are costly
  • Still one machine that can fail

Scaling up is covered in vertical scaling. Scaling out is powerful but not endless: coordination, cost and operations set a practical limit.

Types

Horizontal: split the rows

Horizontal partitioning keeps the same columns everywhere and puts different rows in each part. Spread over servers, this is sharding, and it is the usual way to scale writes.

Users table, split by rows

PartRowsColumns
Shard 1users 1 to 1,000,000all
Shard 2users 1,000,001 to 2,000,000all
Shard 3users 2,000,001 to 3,000,000all
The ranges must not overlap: each user belongs to exactly one shard.

Types

Vertical and functional: split columns or tables

Vertical partitioning keeps the same rows and splits the columns. Small, busy columns stay together. Large or rarely read columns move away, so each page read from disk holds more useful rows.

Users table, split by columns

PartColumnsWhy together
Coreid, name, emailRead on almost every request
Profileid, bio, avatar_urlRead when a profile is opened
Settingsid, settings_jsonLarge and rarely read
The id is repeated so that the parts can be joined again. That join is the cost.

Functional partitioning gives each business area its own database: orders in one, stock in another. Microsoft lists it beside horizontal and vertical partitioning. It is often the easiest first split, because tables from different areas are rarely joined.

Which kind of split?

The three kinds combine. Figma describes splitting whole tables first and rows later.

One machine

Partitions without more machines

You can split a table and keep all parts on one server. The database sees one logical table. When the query names the key, it skips the other parts. This is partition pruning.

One machine, three parts

Users reach the orders service. One database machine holds three parts: orders January, orders February and orders March. A question about March reads only the March part.
There is one thick border, so one machine. The grey dashed paths are parts that the query skips.
SQL
CREATE TABLE orders (  order_id   bigint,  user_id    bigint,  created_at date NOT NULL) PARTITION BY RANGE (created_at);CREATE TABLE orders_2026_03 PARTITION OF orders  FOR VALUES FROM ('2026-03-01') TO ('2026-04-01');-- Removing an old month is one quick stepDROP TABLE orders_2025_03;
PostgreSQL. The upper bound is not included, so months meet without a gap or an overlap.

Shard key

Choose the key well

Often good

  • user_id: many values, and most queries are about one user
  • tenant_id: keeps one customer's data together, if no customer is huge
  • order_id: many values, spreads well when orders are looked up one by one

Often bad

  • created_at with ranges: all new rows hit the newest shard
  • country: very uneven, and few values
  • status: a handful of values, so a handful of parts

A good key passes three tests: many values, no value far bigger than the rest, and present in the main query. The first two make an even spread possible. The third makes it useful.

Testing a shard key

A key that changes is trouble: the row must move to another shard.

Routing

How a request finds its part

One read that names the key

The router can be a library in the app or a separate proxy. The rule must be the same everywhere.

For text keys, turn the text into a number first with a stable hash. Do not use your language's built-in hash blindly. Python gives text a different hash in each process by default, so two servers would disagree about where a user lives.

Which rules exist, and when each fits, is the next topic: partitioning strategies. Rules that move little data when a machine is added are in consistent hashing.

Costs

Four challenges

  • Cross-shard joins

    The rows to join may be on different shards. The query scatters, gathers and joins in the middle. Keep related rows on the same shard.

  • Automatic ids

    Each shard counting 1, 2, 3 creates duplicates. Use UUIDs, or ids built from time, machine and sequence in the Snowflake style.

  • Rebalancing

    Adding a shard means moving rows. With a plain remainder, most rows move.

  • Transactions

    A change over two shards needs two-phase commit or a saga. Both are slower or harder than a local transaction.

Why a plain remainder moves so much

moved=1−1N+1=NN+13→4: 34=75%\text{moved} = 1 - \frac{1}{N+1} = \frac{N}{N+1} \qquad 3 \to 4:\ \tfrac{3}{4} = 75\%
Going from 3 to 4 shards with key mod N, only one key in four stays where it was. See sharding for fixed logical shards, which avoid this.

A count that does not name the key

The answer is ready when the slowest shard has replied.

Practice

Do and do not

Do

  • Choose a key with many values
  • Keep related rows on the same shard
  • Plan for rebalancing from the start
  • Use one connection pool per shard

Do not

  • Use timestamps as a range shard key without a plan for the newest range
  • Rely on cross-shard joins
  • Forget about hot spots
  • Skip testing the rebalancing

Decide

Do you need it yet?

Work down this list and stop when the target is met. Assume 500 GB of data and 3,000 writes a second at peak. One large server with a standby may well hold that, so measure before you split.

  1. 1

    Fix queries and indexes

    A missing index is the cheapest problem to solve.

  2. 2

    Use a bigger machine

    A resize takes minutes and changes no code.

  3. 3

    Add a cache and read replicas

    Both take reads away from the main database. Neither helps with writes.

  4. 4

    Partition on one machine

    Makes large tables easier to search and clean.

  5. 5

    Split by business area

    Move whole groups of tables to another server.

  6. 6

    Shard the rows

    When writes or data no longer fit on one machine.

Spreading data over machines also means living with broken networks between them. That trade is the subject of the CAP theorem.

Summary

The whole topic in one minute.

Partitioning divides rows, columns or whole tables into parts by a rule. Horizontal partitioning splits rows, vertical partitioning splits columns, and functional partitioning splits by business area.

It buys room and speed for questions that name the key, and charges for everything else: joins, transactions, unique numbers and moving data. Start with indexes, a bigger machine, replicas and partitions inside one machine. Spread parts over machines when one machine is no longer enough.

  • Partitioning splits data by a rule. Each row is in exactly one part.
  • Horizontal splits rows, vertical splits columns, functional splits by business area.
  • Parts on one machine help searching and cleaning. Parts on many machines add capacity.
  • A good key has many values, no giant value, and appears in the main query.
  • Joins, ids, rebalancing and transactions get harder, so split late and on purpose.

Sources