{"head":{"title":"What Is Database Sharding? Definition | OceanLab","description":"Sharding splits one database's data across several databases by a key, so each holds a part. When it is needed, how keys are chosen and the usual mistakes.","canonical":"https://oceanlab.in/glossary/sharding"},"crumbs":[{"name":"Home","path":"/"},{"name":"Glossary","path":"/glossary"},{"name":"sharding","path":"/glossary/sharding"}],"slug":"sharding","term":"Sharding","aka":["horizontal partitioning","database sharding"],"category":"System design","short":"Splitting one large dataset across several databases, called shards, by a key such as customer ID, so each shard holds only part of the data.","body":[["Each row belongs to one shard, chosen by a shard key. For example, customers 1 to 1,000 on shard A and the rest on shard B, or by a hash of the customer ID. The app, or a routing layer, sends each query to the right shard."],["Sharding spreads both storage and write load, which a ",{"href":"/glossary/read-replica","text":"read replica"}," cannot do. In exchange, queries across shards, joins and moving data between shards all get harder."]],"why":["Sharding lets a database grow past what one machine can handle. It is also a big, lasting design choice. Most products never need it, and those that do want a shard key that matches how data is used, often the tenant in ",{"href":"/glossary/multi-tenancy","text":"multi-tenant"}," SaaS."],"example":["A messaging platform outgrows its single database. Since almost every query is scoped to one workspace, it shards by workspace ID. Each workspace's data lives together, so normal queries hit one shard, and the largest customers can get their own."],"inBuild":["We design data so that a natural shard key exists, such as a tenant ID on every table, even when we start on one database. That keeps sharding possible later without rewriting the data model. Before sharding we use indexes, caching, replicas and a bigger primary first."],"mistakes":[["Sharding too early, adding complexity years before it is needed."],["Picking a shard key that most queries do not use, so they hit every shard."],["Hot shards, where one huge customer or date range takes most of the load."]],"related":[{"href":"/glossary/read-replica","label":"Read replica","detail":"A read-only copy of a database that is kept in sync with the main database and takes read queries off it."},{"href":"/glossary/multi-tenancy","label":"Multi-tenancy","detail":"One running copy of a software product serving many customers, called tenants, with each tenant's data kept apart."},{"href":"/glossary/connection-pooling","label":"Connection pooling","detail":"Keeping a set of open database connections and sharing them across requests, instead of opening a new connection for each one."},{"href":"/glossary/data-warehouse","label":"Data warehouse","detail":"A database built for analysis and reporting, where cleaned data from many systems is kept together in one place."},{"href":"/glossary/microservices","label":"Microservices","detail":"A way of building an application as many small services, each owning one job and its own data, deployed independently."}],"tools":[],"links":[{"href":"/pricing/categories/databases","label":"Database pricing","detail":"Managed database prices, with sources."}],"sources":[],"legal":false,"updatedAt":"2026-10-25"}