Skip to main content

a Python interface to a Cluster of Redis key-value store

Project description

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.

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rediscluster-0.5.1.tar.gz (12.4 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

rediscluster-0.5.1-py3.2.egg (16.4 kB view details)

Uploaded Egg

rediscluster-0.5.1-py2.7.egg (16.2 kB view details)

Uploaded Egg

File details

Details for the file rediscluster-0.5.1.tar.gz.

File metadata

  • Download URL: rediscluster-0.5.1.tar.gz
  • Upload date:
  • Size: 12.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for rediscluster-0.5.1.tar.gz
Algorithm Hash digest
SHA256 da8792c9e1c9311e36facf700721ef1a4e638cc06a6c93dccbe209e300dc6db7
MD5 023003545d8c922263e60da33d6e54ce
BLAKE2b-256 aececb9b7ab9c5a1285e22a9d923d02fa06414788506e987fba525cf7245cd08

See more details on using hashes here.

File details

Details for the file rediscluster-0.5.1-py3.2.egg.

File metadata

File hashes

Hashes for rediscluster-0.5.1-py3.2.egg
Algorithm Hash digest
SHA256 ec7cf10e76493b1d9c5e1ac58689d49e1de0773150ac1279f78d510ae3c20335
MD5 920cca6a03efc2c3f1e457b9a77db05f
BLAKE2b-256 8de04df85c170bfd3562f16e8615c72cfe3b99fbba12259b582f33af4c65e7cd

See more details on using hashes here.

File details

Details for the file rediscluster-0.5.1-py2.7.egg.

File metadata

File hashes

Hashes for rediscluster-0.5.1-py2.7.egg
Algorithm Hash digest
SHA256 a38555f7a68c035e76ad5a5f29a6b7e6ad143243b054f7bc2ff6f4512c38ca22
MD5 1469f33996581efe0243dd776864fb15
BLAKE2b-256 aca70966d51d35b2ada7cd345da160ffa32a9893c962701e35570e3bf7ea3fde

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page