Database Sharding System Design (original) (raw)

Last Updated : 1 May, 2026

Database sharding is a technique used to scale databases by distributing data across multiple servers. It helps improve performance and handle large datasets efficiently.

database

It is basically a database architecture pattern in which we split a large dataset into smaller chunks (logical shards) and we store/distribute these chunks in different machines/database nodes (physical shards).

database_sharding

Methods of Sharding

Sharding can be done using Range-based, Hash-based, Directory-based, or Geographic-based partitioning to distribute data across multiple servers.

1. Key Based Sharding

Key Based Sharding is a technique, also known as hash-based sharding. Here, we take the value of an entity such as customer ID, customer email, IP address of a client, zip code, etc and we use this value as an input of the hash function. This process generates a hash value which is used to determine which shard we need to use to store the data.

**Example: You have 3 database servers and each request has an application id which is incremented by 1 every time a new application is registered.

To determine which server data should be placed on, we perform a modulo operation on these applications id with the number 3. Then the remainder is used to identify the server to store our data.

key_based_sharding

Advantages

Key-based sharding helps distribute data across shards using a hashing mechanism.

Disadvantages

Despite its efficiency, improper shard key selection can cause performance issues.

2. Horizontal or Range Based Sharding

In Horizontal or Range Based Sharding, we divide the data by separating it into different parts based on the range of a specific value within each record. Let's say you have a database of your online customers' names and email information. You can split this information into two shards.

**Example: In a customer database, one shard may store records where names start with A–P, while another shard stores records where names start with Q–Z.

horizontal_or_range_based_sharding

Advantages

Horizontal or range-based sharding distributes data across multiple shards based on specific value ranges.

Disadvantages

Managing and querying data across multiple shards can introduce additional complexity.

3. Vertical Sharding

Vertical sharding divides a database by separating columns of a table into different shards or tables. Each shard stores a specific set of columns related to a particular feature or functionality. This helps distribute workload and manage large tables more efficiently.

**Example: On Twitter users might have a profile, number of followers, and some tweets posted by his/her own. We can place the user profiles on one shard, followers in the second shard, and tweets on a third shard.

vertical_sharding

Advantages

Vertical sharding divides a database by separating columns into different shards based on functionality.

Disadvantages

Splitting columns across shards can introduce operational and workload challenges.

4. Directory-Based Sharding

In Directory-Based Sharding, we create and maintain a lookup service or lookup table for the original database. Basically we use a shard key for lookup table and we do mapping for each entity that exists in the database. This way we keep track of which database shards hold which data.

**Example: A user database may use a lookup table that maps each user ID to the specific shard where that user’s data is stored. When a request is made, the system first checks the lookup table and then routes the query to the correct shard.

pre_sharded_table

The lookup table holds a static set of information about where specific data can be found.

In the above image, you can see that we have used the delivery zone as a shard key:

Advantages

Directory-based sharding uses a central lookup service that keeps track of where each piece of data is stored.

Disadvantages

While flexible, this approach introduces dependency on the central directory.

Ways to optimize database sharding for even data distribution

Here are some simple ways to optimize database sharding for even data distribution:

Alternatives to database sharding

Below are some of the alternatives to database sharding:

  1. **Vertical Scaling: Vertical Scaling means upgrading the same server by adding more CPU, RAM, or storage to handle more load, but it has a fixed hardware limit so it can’t scale forever.
  2. **Replication: Replication means creating database copies on multiple servers for load balancing and high availability, but replicas may face synchronization issues.
  3. **Partitioning: Partitioning means splitting data into smaller parts within the same server to improve performance for large datasets without using multiple servers.
  4. **Caching: Caching means storing frequently used data in Redis/Memcached to reduce database load and speed up responses.
  5. **CDNs: CDNs mean using a Content Delivery Network for read-heavy data to reduce direct database access and deliver content faster.