There is a limit to anything you do and same applies to a software. After a certain point, there is a need to scale the database.
Scaling can either be vertical scaling or horizontal scaling.
In summary, vertical scaling is increasing the resources on existing machine while horizontal scaling means adding more machines to manage the workloads.
Today, we talk about horizontal scaling for a relational database.
The example is inspired from Booking.com architecture for customer review system.
Let’s jump in.
Read Chapter 2 and 3 from my O’Reilly book ‘System Design on AWS’ for detailed dive deep on Relational and Non-Relational Database.
If you don’t know me, I’m Mandeep Singh, a software engineer, author and educator. I love building simple, scalable systems. I share my learnings via books, newsletter, social media posts and YouTube. Subscribe to follow along on the journey!

======
So what are the system requirements?
Horizontal scaling to serve increasing customer review traffic.
Withstand availability zone failures.
Let’s start thinking on design.
Design
Let’s think on how the existing system looks like?
Vertical Scaling
Figure 1 shows the simplistic view of application and database interaction. The primary DB instance is responsible for all the writes operation and read replicas are responsible to serve read traffic.
Now the requirement is to scale the primary instance. Vertical scaling would have meant replacing the existing machine with bigger machine. Vertical scaling is simple strategy and works for most of the use cases.
Moving on to horizontal scaling.
Horizontal Data Partitioning
Horizontal scaling means more writer instances, how will the application figure out where the data should be written or read from. Figure 2 shows a glimpse of database horizontal scaling.
The request router shown in the Figure 2 can be an independent deployment or a small component running along with application servers. As an initial setup, the recommendation is to start with component being part of application layer itself. The routing layer can decide where the request should be routed to.
The data partitions are also referred to as shards and horizontal data partitioning is also termed as sharding.
So what should be logic for reads/writes for the request router? We’re talking about the customer review system.
NUM_SHARDS = 4
def get_shard(accommodation_id: str) -> int:
return accommodation_id % NUM_SHARDSLet’s run this with some examples.
| Accommodation Id | NUM_SHARDS | Shard ID |
| ---------------- | ---------- | -------- |
| 12 | 3 | 0 |
| 13 | 3 | 1 |
| 10124 | 3 | 2 |
| 10124 | 4 | 0 |The request router and the number of allocated shards seems simple enough, so why is not everyone doing horizontal scaling from the start?
Well, there are couple of issues.
If the number of partitions change (increase/decrease), the function changes. It’s a problem because at write, the shard number of x but during reads, it became y; not a good scenario to be in.
To solve the above problem, data needs to be moved to a particular node. This is also called resharding but it consumes so much time.
Apart from too much execution time on above, an AZ should be shutdown during data redistribution. This means the request router will not be able to figure out where the request should be routed (to the older shard or newer shard where data copy is in progress because you don’t know if the requested data is already copied or not).
The request router component inferred above is called as coordinator service in booking.com architecture.
Users ----> Coordinator Service ----> Data shardsLet’s talk about design improvements to address all the bottlenecks included above.
Sharding with Consistent Hashing
The hashing algorithm used earlier to identify the shard number requires complete data rebalancing.
We need an implementation which requires minimal amount of data movement. Consistent Hashing is something useful here.
Consistent Hashing
Let’s start by understanding what consistent hashing is? Let’s take an example. Figure 3 shows four servers, think of them as writer instances.
Hashing Function = f(x) = f(server IP address) = location in Hash ring. The hash function could hold any logic.
Depending on the hashing function, the servers are placed in the ring.
Values in the ring are defined from 0-1000. First server is placed at 0, second at 500 and so on.
Assume now review has to be stored for property Id
Say, Hash(property ID) = 233
Check if there is server on 233?
If yes, then store the data
If no, move in clockwise direction and store the data on first server found. In this case, server at location 500.
Now let’s say a new server is added to the ring. Figure 4 shows a new server placed between placeholder 900 and 1000.
The resharding of entire machine is solved with the consistent hashing approach. Remapping is done for only k/n keys. [k = number of keys; n = number of servers] So in this case, the remapping is done only for keys which lies in the keyspace 900 and 1000.
There is possibility of hotspots though, a single node receiving too much traffic as compared to others.
Handling Server Hotspots
There are two ways to handle the server hotspots issues.
Introduce read replicas for read queries
Refer to Figure 2, every writer shard has associated read replicas to serve the read traffic. This helps in distributing the read traffic and also isolates the reads and writes.
Virtual node placement
This is extensively done for large scale deployments for horizontally scaling relational and non-relational databases. The physical number of servers could be just 4 but there is a logical layer on top. The request router identifies an virtual node where data resides and this helps in more even data distribution.
Another problem was AZ shutdown during the data migration. Let’s talk about handling that next.
Monotonicity
Monotonicity describes a function (or preference) ordering that consistently moves in one direction (non-decreasing or non-increasing) across the interval. For consistent hashing, this is stated as:
If items are initially assigned to a set of buckets , and then some new buckets are added to form , then an item may move from an old bucket to a new bucket, but not from one old bucket to another.
In summary, data can only be transferred from old shards to the new shards during an ongoing resharding. Any redistributed key will either stay at its current shard or will be moved to newly provisioned hardware.
Let’s elaborate more on this.
Keys that are part of N-shard scheme, old scheme; should also be part of (N+M)-shard scheme.
Coordinators (aka request router) are not aware of new shards until data is fully loaded.
Coordinators only route request to old shards.
Once data is fully moved
Some coordinators are informed about new scheme. And gradually other coordinators become aware too. The communication is handled by gossip protocol. Gossip protocol, in short, servers inform their nearby servers about an event, in this case, a new scheme.
If there are any issues with metrics, the traffic can be kept on older scheme.
After confidence, update old shards to stop loading keys that were reassigned to new shards.
Figure 5 shows the view of steps specified above.
This is all for today’s discussion. Let’s conclude on some key learnings.
Conclusion
Migrations are tough, specially the ones involving the data. Here are few recommendations.
Observability is key to ensure data consistency and system reliability.
Keep fallbacks in place. ‘If something can go wrong, it will go wrong’. Always assume this holds true and work accordingly on design and rollouts.
Reading is not something you prefer? Watch the dive deep on YouTube instead.
If you found this article helpful, please consider restacking and sharing it with your audience. This spreads the word and help me continue on this journey :)
Happy Learning!







