• Pratik Temkar •
7 min read

Scaling your Database - Partitioning and Sharding

Everything about Partitioning and Sharding in Distributed Databases

When you launch a new application, you usually start with a single database server. It is simple, predictable, and honestly the right choice most of the time. You focus on building features, shipping fast, and validating your idea.

Then one day traffic spikes. Maybe an influencer tweets about your product. Maybe you land an enterprise customer. Suddenly your database is handling thousands of reads and writes per second and it starts to struggle.

Scaling a database is one of the most important backend engineering problems. If you are into distributed systems like I am, this is where things start getting interesting.

Let us walk through the journey of scaling a database, and clearly understand the difference between partitioning and sharding.


Exhaust the Simple Options First

Before jumping into distributed architecture, squeeze everything out of your existing setup. Many systems get over engineered way too early.

1. Indexing

If reads are slow, check your indexes first. Most performance issues are not scaling problems. They are indexing problems.

Make sure the fields you filter, sort, and join on are properly indexed. In systems like PostgreSQL, good indexing alone can delay scaling decisions for a long time.

2. Vertical Scaling

Scaling up means adding more CPU, RAM, and faster disks to your single database server.

This is usually the cheapest and simplest improvement. But hardware has limits. Eventually you will hit the ceiling of the biggest machine you can afford.

3. Read Replicas

If your workload is read heavy, introduce read replicas.

A replica continuously syncs from the primary database. You route read traffic to replicas and reserve the primary for writes.

This works extremely well for systems with high read volume. But it does not solve heavy write bottlenecks.

If you have exhausted indexing, vertical scaling, and replicas, and your database still cannot handle write throughput, now you are entering partitioning and sharding territory.


Partitioning vs Sharding

These terms are often used interchangeably, which causes confusion.

Here is the clean mental model:

  • Partitioning is about splitting data.
  • Sharding is about distributing partitions across machines.

Partitioning

Partitioning means dividing a large dataset into mutually exclusive segments called partitions.

You can partition a table inside a single database server. For example, you can split an orders table by date so each month lives in a separate partition.

This helps with:

  • Smaller index sizes
  • Faster scans
  • Easier maintenance
  • Improved query planning

You are not necessarily adding more machines. You are organizing data better.

Sharding

Sharding means distributing those partitions across multiple physical database servers.

Now you are scaling horizontally.

Instead of one server holding all users, you might have:

  • Users 1 to 1 million on Server A
  • Users 1 million to 2 million on Server B
  • Users 2 million to 3 million on Server C

At this point, you are not just tuning a database. You are designing a distributed system.


Data Partitioning Strategies

Whether you are partitioning on one server or sharding across many, you need a deterministic rule to decide where each row goes.

1. Range Based Partitioning

Data is routed based on a continuous range of values.

Example:

  • User IDs 1 to 25 go to Partition A
  • 26 to 50 go to Partition B

This works well when queries are mostly range based.

The danger is hot partitions.

If you partition by an auto incrementing ID, all new writes go to the latest partition. One partition becomes overloaded while others sit idle.

The same happens with time series data. If you partition by timestamp, today’s partition handles all the write load.

2. Hash Based Partitioning

To avoid hot partitions, you can hash a column such as user_id.

The hash function distributes rows evenly across partitions. Even sequential IDs like 1 and 2 land in completely different partitions.

This balances write load very well.

The tradeoff is that range queries become expensive. Since sequential data is scattered everywhere, you must check multiple partitions.

There is no perfect strategy. It depends on your access patterns.


The Architecture of a Sharded Database

Once you shard across machines, your application must know where to send queries.

You could hardcode shard logic inside the application. That is called application level sharding. It tightly couples your business logic with infrastructure logic.

This becomes painful to maintain.

A better approach is introducing a proxy layer.

The application talks to the proxy. The proxy calculates the shard key, routes the query to the correct database, and returns the result.

Systems like Vitess, used by companies such as YouTube, abstract away this routing complexity and make MySQL behave like a scalable distributed system.

Now you are officially in distributed systems land:

  • Network hops
  • Partial failures
  • Load balancing
  • Rebalancing shards

Choosing the Right Shard Key

The shard key is the most important decision in a sharded architecture.

If you choose poorly, your system will suffer forever.

1. Cardinality

Pick a key with high cardinality.

Using user_id is good because it is unique and evenly distributed.

Using something like country or name is dangerous because values are skewed. You will end up with uneven shards.

2. Volatility

Never pick a key that changes frequently.

If you shard by a field that updates often, the database must physically move rows between shards. That is expensive and complex.

Choose something stable and immutable.


The Dark Side of Sharding

Sharding looks powerful on architecture diagrams. In reality, it is complex and should be your last resort.

Cross Shard Queries

If you need to join data across shards, the system must fetch data over the network. That increases latency and CPU usage.

Transactions Become Hard

ACID transactions across shards require distributed transaction protocols. Two phase commit adds latency and operational complexity.

In many real systems, teams give up strict cross shard transactions and accept eventual consistency.

Extra Latency

Adding a proxy introduces another network hop. Every query now travels further.

Operational Complexity

You now manage:

  • Multiple database servers
  • Rebalancing shards
  • Schema consistency
  • Backup coordination
  • Failure handling

You are not just running a database anymore. You are running a distributed database platform.


Conclusion

Sharding is a superpower.

It allows you to handle massive write throughput, store petabytes of data, and scale horizontally when a single machine is no longer enough.

But it comes at the cost of complexity.

As someone who enjoys distributed systems, I find sharding fascinating. But in real production systems, the boring solution is often the correct one.

Add indexes.
Scale vertically.
Introduce read replicas.
Partition tables smartly.

Only when a single primary database physically cannot handle your write load should you step into sharding.

That is when your database stops being just a storage engine and becomes a distributed system.

Got thoughts or feedback?

I'd love to hear from you! Feel free to reach out on X (Twitter) if you have any comments, questions, or just want to chat about the topics covered in this post.

Connect on X