Three Common Partitioning Strategies
There are three commonly used strategies for deciding how to split data across charts: lookup, range, and hash.
A Simple Example: FaceTool Social Media
Imagine a global social media platform called FaceTool, similar to Facebook. As user traffic grows, the system can no longer store all user data on a single database server. To scale, the team decides to partition data across three charts. More charts can be added later.
Each user’s data must be moved to one of these three charts. The question becomes: how do we assign a user to a chart?
Lookup Strategy: Mapping Keys to Charts
The lookup strategy uses a mapping where each key is assigned to a specific chart.
In a user table, a new column—chart identifier—is added. This column is populated for all existing users, historical data, and new users during sign-up.
For example, a random chart is assigned to every user. This assignment is applied consistently across all data.
The strategy offers strong control over chart assignment.
We can assign charts randomly, which often results in a nearly even distribution of data.
Or we can implement intelligent assignment—such as measuring each chart’s capacity and assigning users to the least utilized chart.
This idea will be explored further in the course under cell-based architecture.
Drawbacks of the Lookup Strategy
A key drawback is the hard dependency on the mapping.
Every read or write request must perform a lookup to determine which chart holds the user data.
This is where the strategy gets its name—due to the required lookups.
In practice, teams often build a dedicated lookup service to cache the mapping.
This service speeds up lookups but introduces new challenges:
- How to ensure the lookup service is always available?
- How to make it fast?
- How to scale it?
- How to maintain consistency?
These questions relate to availability, performance, scalability, and consistency.
The mapping itself can grow large over time and may require partitioning.
This is a common issue in large-scale systems.
While these technical challenges can be solved, they highlight the trade-offs of the lookup strategy.
Range Strategy: Continuous Key Ranges
The range strategy assigns each chart a continuous range of keys.
For simplicity, use the user’s last name as a key.
In reality, a unique user ID would be used.
Each chart is responsible for a range of last names.
For example:
- First chart: last names starting with A to H
- Second chart: last names from I to Q
- Third chart: last as needed
With this setup, no lookup service is needed.
If we know the user’s last name and the partition boundaries, we can directly identify the correct chart.
The range strategy is easy to implement.
It is especially effective for applications that frequently perform range queries.
Example: Monthly Order Range Queries
A system often needs to retrieve all orders placed in a specific month.
Each chart stores data for one month.
When a range query is issued, the system retrieves data from a single chart only.
Problem with Order ID Partitioning
If data is partitioned by order ID, orders from the same month are spread across multiple charts.
This makes range queries inefficient because data must be fetched from many charts.
Hot Chart Problem
Partitioning by order ID creates imbalance.
One chart may handle significantly more load than others.
This leads to the hot chart problem.
Hash Strategy Overview
Partition data by user's last name.
Instead of using only the first letter, use the full last name string.
Compute a hash value from the string.
Assign ranges of hash values to each chart.
Methods for Hash Range Assignment
- Evenly space hash boundaries
- Use consistent hashing (discussed later)
Data Storage in Hash Strategy
Each chart stores users whose hash value falls within its assigned range.
This structure resembles the range strategy.
Key Benefit of Hash Function
The hash function distributes data more evenly.
Two last names starting with the same letter may produce different hash values.
In range partitioning, such users end up on the same chart.
In hash partitioning, they are likely assigned to different charts.
This results in more uniform data distribution.
Trade-off
Hash strategy eliminates efficient range queries.
Computing the hash adds overhead.
This overhead may become critical in high-load systems.
Questions
- How do we ensure the lookup service is always available?
- How can we make the lookup service fast and scalable?
- How do we maintain consistency in the lookup mapping over time?
- What happens to the mapping as the system grows over time?
- How does the range strategy affect the performance of range queries?
- Why might one chart become overloaded under the range strategy?
- How does the hash strategy improve data distribution compared to the range strategy?
- What trade-off does the hash strategy introduce compared to the range strategy?
Key takeaways
- The lookup strategy assigns each key to a chart via a mapping, offering control but requiring a lookup service.
- The lookup strategy introduces dependencies on the mapping, which can grow large and require partitioning itself.
- The range strategy assigns continuous key ranges to charts, enabling efficient range queries but risking uneven load distribution.
- The range strategy can lead to a 'hot chart' problem where one chart bears significantly more load than others.
- The hash strategy uses a hash function on keys to distribute data across charts, improving balance by spreading users with similar keys to different charts.
- The hash strategy reduces hot spots but eliminates efficient range queries due to the non-sequential nature of hash values.
- Computing a hash adds overhead, which may become critical in high-load systems.
- The choice of partitioning strategy depends on whether the system prioritizes query efficiency or data balance.