Skip to main content

a Python interface to a Cluster of Redis key-value stores.

Project Goals

The goal of rediscluster-py, together with rediscluster-php, is to have a consistent, compatible client libraries accross programming languages when sharding among different Redis instances in a transparent, fast, and fault tolerant way. rediscluster-py is based on the awesome redis-py StrictRedis Api, thus the original api commands would work without problems within the context of a cluster of redis servers

Travis CI

Currently, rediscluster-py is being tested via travis ci for python version 2.6, 2.7 and 3.2: Build Status

Installation

$ sudo pip install rediscluster

or alternatively (you really should be using pip though):

$ sudo easy_install rediscluster

From source:

$ sudo python setup.py install

Running Tests

$ git clone https://github.com/salimane/rediscluster-py.git
$ cd rediscluster-py
$ vi tests/config.py
$ ./run_tests

Getting Started

>>> import rediscluster
>>> cluster = {
...          # node names
...          'nodes' : { # masters
...                      'node_1' : {'host' : '127.0.0.1', 'port' : 63791},
...                      'node_2' : {'host' : '127.0.0.1', 'port' : 63792},
...                    }
...     }
>>> r = rediscluster.StrictRedisCluster(cluster=cluster, db=0)
>>> r.set('foo', 'bar')
True
>>> r.get('foo')
'bar'

Cluster Configuration

The cluster configuration is a hash that is mostly based on the idea of a node, which is simply a host:port pair that points to a single redis-server instance. This is to make sure it doesn’t get tied it to a specific host (or port). The advantage of this is that it is easy to add or remove nodes from the system to adjust the capacity while the system is running.

Read Slaves & Write Masters

rediscluster uses the master servers stored in the cluster hash passed during instantiation to auto discover if any slave is attached to them. It then transparently relay read redis commands to slaves and writes commands to masters.

There is also support to only use masters even if read redis commands are issued, just specify it at client instantiation like :

>>> r = rediscluster.StrictRedisCluster(cluster=cluster, db=0) # read redis commands are routed to slaves
>>>
>>> r = rediscluster.StrictRedisCluster(cluster=cluster, db=0, mastersonly=True) # read redis commands are routed to masters

Partitioning Algorithm

rediscluster doesn’t used a consistent hashing like some other libraries. In order to map every given key to the appropriate Redis node, the algorithm used, based on crc32 and modulo, is :

(abs(binascii.crc32(<key>) & 0xffffffff) % <number of masters>) + 1

this is used to ensure some compatibility with other languages, php in particular. A function getnodefor is provided to get the node a particular key will be/has been stored to.

>>> r.getnodefor('foo')
{'node_2': {'host': '127.0.0.1', 'port': 63792}}
>>>

Hash Tags

In order to specify your own hash key (so that related keys can all land on a given node), rediscluster allows you to pass a string in the form “a{b}” where you’d normally pass a scalar. The first element of the list is the key to use for the hash and the second is the real key that should be fetched/modify:

>>> r.get("bar{foo}")
>>>
>>> r.mset({"bar{foo}": "bar", "foo": "foo"})
>>>
>>> r.mget(["bar{foo}", "foo"])

In that case “foo” is the hash key but “bar” is still the name of the key that is fetched from the redis node that “foo” hashes to.

Multiple Keys Redis Commands

In the context of storing an application data accross many redis servers, commands taking multiple keys as arguments are harder to use since, if the two keys will hash to two different instances, the operation can not be performed. Fortunately, rediscluster is a little fault tolerant in that it still fetches the right result for those multi keys operations as far as the client is concerned. To do so it processes the related involved redis servers at interface level.

>>> r.sadd('foo', *['a1', 'a2', 'a3'])
3
>>> r.sadd('bar', *['b1', 'a2', 'b3'])
3
>>> r.sdiffstore('foobar', 'foo', 'bar')
2
>>> r.smembers('foobar')
set(['a1', 'a3'])
>>> r.getnodefor('foo')
{'node_2': {'host': '127.0.0.1', 'port': 63792}}
>>> r.getnodefor('bar')
{'node_1': {'host': '127.0.0.1', 'port': 63791}}
>>> r.getnodefor('foobar')
{'node_2': {'host': '127.0.0.1', 'port': 63792}}
>>>

Redis-Sharding & Redis-Copy

In order to help with moving an application with a single redis server to a cluster of redis servers that could take advantage of rediscluster, i wrote redis-sharding and redis-copy

Information

Author

rediscluster-py is developed and maintained by Salimane Adjao Moustapha (me@salimane.com). It can be found here: http://github.com/salimane/rediscluster-py

Metadata

Release files for rediscluster 0.5.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rediscluster 0.5.3
File Size Uploaded
rediscluster-0.5.3.tar.gz 9.9 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for rediscluster 0.5.3
File Interpreter ABI Platform
rediscluster-0.5.3-py3.2.egg Legacy Egg format - - Details
rediscluster-0.5.3-py2.7.egg Legacy Egg format - - Details

Total release size: 42.6 kB

Release files / rediscluster-0.5.3.tar.gz

Download URL rediscluster-0.5.3.tar.gz
Size 9.9 kB
Tags Source
SHA-256 checksum
How to use checksums
73004d9c80f70b25fc926ad0dcee3593c6755e080c6971b859e4638072c530fe
BLAKE2b-256 checksum
How to use checksums
0a2c0f2ab19a4f20ea7bd2dbb4032fc332894feabdb0346353fbd9cbdb0c152d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / rediscluster-0.5.3-py3.2.egg

Download URL rediscluster-0.5.3-py3.2.egg
Size 16.4 kB
Tags Egg
SHA-256 checksum
How to use checksums
3c525e2db062a86230bdee0a7de348d6bffd8556a351da3fa4a826f9916b3279
BLAKE2b-256 checksum
How to use checksums
84339c87e6003c1c87e24373ef6ab0f3a7a28b673f3ed9ba92dfa1b6745d6c77
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / rediscluster-0.5.3-py2.7.egg

Download URL rediscluster-0.5.3-py2.7.egg
Size 16.3 kB
Tags Egg
SHA-256 checksum
How to use checksums
6f698886b419e5e0299348569aea3d643426acf4d23d6b6ca19e0d1d2930c4b9
BLAKE2b-256 checksum
How to use checksums
b9857f23b1ee663635b4b07c1b853949bfe5c0c0fb6e0dfed547ea13d2bb0235
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.5.3 This release

3 release files

0.5.1

3 release files

0.5.0

3 release files

0.4.0

3 release files

0.3.1

3 release files

0.3.0

3 release files

0.2.7

5 release files

0.2.6

5 release files

0.2.5

5 release files

0.2.4

5 release files

0.2.3

5 release files

0.2.2

5 release files

0.2.1

5 release files

0.2.0

5 release files

0.1.3

5 release files

0.1.2

5 release files

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page