Sharding

Splitting the database into pieces · Part of Databases

Key idea

Practice

8 questions · about 4 min

Explained step by step

Data partitioning

Data partitioning is splitting a database or table into many smaller parts, spread over several machines. It improves how the database is managed, its performance and its availability. It's like splitting a huge library into 4 branches: each has fewer books and serves faster. There are 2 main ways: horizontal (by rows) and vertical (by columns).

Vertical partitioning

In horizontal partitioning (sharding), rows are split: each part has every column, but only some rows. In vertical partitioning, columns are split: the table is divided into smaller tables with fewer columns, each in its own partition. It's like splitting a student spreadsheet: horizontal is separating by class, vertical is separating personal details from grades.

Sharding

Sharding is separating a table's rows into several tables called shards, on different machines. Each shard has the same schema and columns, but only part of the data, and what one shard holds isn't in the others. It's done because, past a certain point, adding machines is cheaper than buying a huge server. It's like splitting a phone book into volumes A-F, G-M and N-Z. It can be done in the app or in the database.

There are several criteria for splitting. Hash-based: a hash algorithm picks the shard; it spreads evenly, but adding or removing servers is expensive. List-based: each shard has a list of values, like countries. Range-based: each shard has a contiguous range, like ids 1 to 1,000; ranges don't overlap. Composite: combines 2 or more criteria, for example first by country then by hash. It's like sorting mail: by lottery, by neighborhood or by street number.

Sharding has advantages: more availability (each shard is managed separately), more scalability, more security (sensitive data in another shard), faster queries and easier-to-manage data. And downsides: more complexity, JOINs across shards that are slow or impossible, because data must come from several servers, and the need to rebalance. It's like moving into 3 apartments: more space, but to gather your things you have to visit all 3.

Sharding fits when you want to use regular hardware instead of very expensive machines, keep data in different regions, scale fast by adding shards, lower each machine's load or handle many more connections at once. It doesn't fit at the start: with 1 database that still copes, you'd add complexity for nothing. It's like opening branches: it makes sense when the shop can't keep up, not on day one.

Partition key

The partition key is the column that decides which shard each row goes to. For example, the user id: with a hash, id % 4 says which of 4 shards it goes to. Choosing it well matters most, because it defines how load is spread and which queries touch a single shard. It's like deciding how to split students into classrooms: by last name, age or neighborhood. A bad criterion fills one room and leaves another empty.

Rebalancing

If data isn't spread evenly, or one shard gets much more load, you have to rebalance: move data between shards so requests are spread as evenly as possible. It happens a lot with ranges: if you split by last name, the 'S' shard can fill up with 'Smith'. It's like a supermarket line: if 1 till has 20 people and the others 2, you redirect people.

Terms in this lesson

Arki, the beaver mascot

Your next interview starts with 5 minutes today.

Free on iOS and Android. Start from zero, no account needed.

Key idea

Splitting the database into pieces