Sharding
Шардирование
Sharding splits a database across multiple machines so each machine handles a subset of the data. It lets the system grow beyond the storage or processing capacity of one machine.
Also known as: шардинг
What sharding solves
Suppose one database server can no longer keep up with incoming writes. Adding a read replica might help with reads, but the same server can still be responsible for every write. With sharding, different servers handle different rows of the same logical table. Each subset is called a shard, and a shard can have replicas of its own.
How a request finds its data
A shard key is a field or combination of fields that determines where a record belongs. The application or database routing layer uses that value to locate the shard. With range-based sharding, nearby key values can live together, so a range query may touch only a few shards. But if the key is an increasing timestamp, the newest writes may all land on the same shard. Hash-based sharding typically spreads distinct key values more evenly, at the cost of scattering values that were close together in the original key space.
Example: a multi-tenant app
Say a project management app shards its tasks by workspace_id. Listing tasks for workspace 42 is a single-shard read. A support query that looks up a task by task_id alone may have to ask every shard unless there is another way to look up its location, such as a directory that maps task IDs to workspaces. A report across all workspaces can also fan out, run partial aggregations on each shard, and combine the results.
Keeping a workspace together makes its everyday queries cheap. The trade-off appears when one workspace generates so much traffic that it saturates its shard. Hashing workspace_id can spread different workspaces apart, but it cannot split one busy workspace across servers.
What it costs to operate
Sharding is a form of horizontal partitioning across nodes, not just dividing a table into partitions on one server. It adds network and coordination costs to cross-shard joins and transactions; those operations may be supported, but they are generally more expensive than local ones. Sharding also does not create redundancy. Without a working replica or recovery mechanism, losing a shard can make its share of the data unavailable.
How it comes up in interviews
A good system design answer starts with the workload: which queries dominate, what is limiting capacity, and which records need to stay together? Propose a shard key, then test it against a busy customer, a query that lacks the key, and the need to add capacity. Adding a server only helps once some data and traffic move to it. A system that maps logical buckets to servers can move selected buckets without changing the assignment of every key.