Course contentsShow
← Databases & SQL
Lesson 1310 of 1,95312. Key-Value and Caching EnginesFree lesson

Horizontal Scaling with Sharding

Partition data keys across multiple nodes to exceed single-server memory limits.

In this lesson, you will learn how to bypass the physical memory limitations of a single server by distributing your keys across a cluster of multiple database nodes.

The Problem: The Single-Node Wall

As your application grows, you eventually hit a point where a single server's RAM cannot hold all your data. If your dataset exceeds the available memory, your cache becomes inefficient because it constantly evicts data to disk or experiences massive latency spikes. While you can keep scaling vertically (buying a bigger server), you will eventually hit a hardware ceiling.

The Solution: Sharding

Sharding is the process of partitioning your keyset across multiple distinct nodes. Instead of one server holding keys A-Z, you distribute them so Node 1 holds keys A-M and Node 2 holds keys N-Z. By spreading the data, you effectively multiply your total memory capacity by the number of nodes in your cluster.

Implementing Sharding via Consistent Hashing

To determine which node "owns" a specific key, we use a simple routing logic, most commonly a hash function. For example, if you have three nodes, you might perform a modulo operation on the hash of your key:

# Determine which node handles a specific key
nodes = ["node_1", "node_2", "node_3"]
key = "user_session_99"

# Hash the key and map it to a node index
node_index = hash(key) % len(nodes)
target_node = nodes[node_index]

print(f"Key {key} is stored on {target_node}")

Strategic Trade-offs

When you shard data, you lose the ability to perform cross-node operations easily. For instance, if you want to perform a SUNION (Set Union) across two sets stored on different nodes, the command will fail because the data is physically separated. Furthermore, your application logic now needs to be "cluster-aware," meaning it must calculate the correct target node before sending any command. Despite these constraints, sharding is the industry-standard approach for achieving massive scale in distributed key-value engines.

Key Takeaway: Sharding breaks your dataset into smaller, manageable chunks across multiple nodes, allowing you to scale your total system memory horizontally rather than relying on a single, expensive server.