Sharding Redis is Easy.

While Redis doesn’t have a built-in support for clustering yet, there’s a pretty easy solution if you are usingĀ ruby client. It’s not documented and you can find it out only from reading tests, but this client has a support for consistent hashing and multiple Redis’ nodes just out of the box.

Here’s how you use it:

1
2
3
4
5
6
7
8
9
10
11
require rubygems
require redis
require redis/distributed

r = Redis::Distributed.new %w[redis://localhost:6379 redis://localhost:6378]

# show node for key "foo"
p r.node_for("foo")

# set the value of "foo"
r.set("foo", "value")

I fired off two instances of Redis on different ports just as an example but there can be any number of Redis instances hosted on different machines. So what happens next? When you are trying to write or read a key, client calculates an unique hash for this key and maps it to a specific Redis node. That way same keys are always routed to the same redis nodes.

There are also more features like adding nodes which you can find out from reading distributed*.rb testsĀ here.

More read if you are interested in scaling Redis.

Redis Memory Usage
Redis Sharding at Craigslist

Comments