Databases 9 min read

Understanding Redis Sharding: Benefits, Strategies, and Implementation

This article explains Redis sharding, covering its purpose, various partitioning methods such as range and hash partitioning, implementation approaches like client‑side and proxy‑assisted sharding, their drawbacks, and practical steps for migration and using Redis Cluster or Twemproxy.

JavaEdge
JavaEdge
JavaEdge
Understanding Redis Sharding: Benefits, Strategies, and Implementation

1. What is Redis sharding?

Sharding distributes data across multiple nodes, improving memory utilization and scaling compute and network bandwidth.

2. Sharding solutions

2.1 Range partitioning

Map key ranges to specific nodes, e.g., IDs 0‑100000 → R1, 100001‑200000 → R2. Simple but requires maintaining a range‑to‑instance map and works only for numeric keys.

2.2 Hash partitioning

Apply a hash function (e.g., Redis CRC16) to the key, take modulo of node count, and map to a node. Works for any key type. Consistent hashing is a popular variant used by many clients and proxies.

3. Implementations of sharding

3.1 Client‑side sharding

Clients directly select the correct node for each key; many Redis client libraries support this.

3.2 Proxy‑assisted sharding

Requests go through a proxy (e.g., Twemproxy) that forwards them to the appropriate node based on configuration.

3.3 Query routing

Clients can send queries to any node; the node routes the request to the correct shard. Redis Cluster uses a hybrid of query routing and client‑side sharding.

4. Drawbacks of sharding

Multi‑key operations (e.g., intersect) are not supported across shards.

Transactions involving multiple keys cannot be used.

Sharding granularity is per key, limiting large data structures.

Backup and maintenance become more complex because multiple RDB/AOF files must be handled.

Adding or removing nodes is difficult for most schemes; only Redis Cluster supports dynamic rebalancing.

5. Data store vs cache

When used as a cache, changing the key‑to‑node mapping can improve availability. As a persistent store, the mapping must remain stable, otherwise rebalancing is required.

6. Consistent hashing

Consistent hashing allows a key to move to a new node when that node becomes available, simplifying scaling for cache scenarios. For storage use, a fixed mapping is required.

7. Practical migration steps (pre‑sharding)

Start an empty instance on the new server.

Configure the new instance as a replica of the source.

Stop the client.

Update the IP address of the moved instance.

Send SLAVEOF NO ONE to promote the replica.

Restart the client with the new configuration.

Shut down the old instance.

8. Redis Cluster

Redis Cluster provides automatic sharding and high availability; it is the de‑facto standard when supported by the client.

9. Twemproxy

Twemproxy, developed by Twitter, is a fast C‑based proxy supporting the Redis protocol. It automatically shards across multiple instances and can exclude failed nodes, but it changes the key‑to‑instance mapping, so it is suited mainly for caching.

10. Client libraries with consistent hashing

Several Redis clients (e.g., redis‑rb, Predis) implement client‑side sharding with consistent hashing.

References: https://redis.io/topics/partitioning, "Redis in Action".

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Proxydatabaseshardingredisconsistent hashingPartitioning
JavaEdge
Written by

JavaEdge

First‑line development experience at multiple leading tech firms; now a software architect at a Shanghai state‑owned enterprise and founder of Programming Yanxuan. Nearly 300k followers online; expertise in distributed system design, AIGC application development, and quantitative finance investing.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.