This repository has been archived by the owner. It is now read-only.
MRedis is a multi-server wrapper for the excellent Python Redis client.
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
mredis Fixed a typo, thanks bjones Jan 6, 2011
LICENSE Fixup for broken delete function Dec 13, 2010


MRedis is a multi-server wapper for the Python Redis client at

While the redis-py module supports multiple connections it does not provide any type of server hashing to automatically distribute keys across the connection pool.

The goal was to provide a drop in replacement/enhancement of the redis-py client. As such, the function signatures are the same wherever possible.


  • redis-py


Use pip, easy_install or run "python install"


import mredis
import time

servers = [{'host': 'localhost', 'port': 6379, 'db': 0},
           {'host': 'localhost', 'port': 6380, 'db': 0}]

mr = mredis.MRedis(servers)

# Build a set of keys for operations
keys = set()
for x in xrange(0, 100):
    key = 'key:%.8f' % time.time()

# Set these keys across the two Redis servers
for key in keys:
    mr.set(key, time.time())

# Get a list of keys from all servers as a dictionary by server
fetched = mr.keys('key:*')

# Loop through the servers and build the key list per server
results = []
for server in fetched:

    # Get the list of keys
    temp = fetched[server].split(' ')

    # Loop through the keys retrieving them from the servers
    for key in temp:
        results.append('%s->%s' % (key, mr.get(key)))

# Demonstrate we fetched as many keys as we set
print '%i keys fetched' % len(results)


The following functions return results from the server in a dictionary keyed by server host:port:db

  • Redis.bgrewriteaof
  • Redis.bgsave
  • Redis.dbsize
  • Redis.flushall
  • Redis.flushdb
  • Redis.lastsave
  • Redis.keys
  • Redis.randomkey


While the goal is to make the client behave as closely as possible to redis-py, due to the nature of distributing keys across multiple servers, pipelining can not be handled the same way.

Because of this, pipeline objects are added per server and should only be used for the same key.

Using multiple keys in a pipeline may and most likely will result in data being stored or used in the wrong server and will yield unpredictable results.

When you request a pipeline object, you pass in a key which will return the proper server for the pipeline.

Using pipelining:

servers = [{'host': 'localhost', 'port': 6379, 'db': 0},
           {'host': 'localhost', 'port': 6380, 'db': 0}]

mr = mredis.MRedis(servers)

pipeline = mr.pipeline(key)
pipeline.lpush(key, value).lpop(key, value).llen(key)

Purposefully omitted functionality

The following functions are currently unimplemented due to complexity in keeping the same behavior with multiple servers.

  • Redis.mget
  • Redis.mset
  • Redis.msetnx
  • Redis.rename
  • Redis.renamenx
  • Redis.blpop
  • Redis.brpop
  • Redis.sdiff
  • Redis.sdiffstore
  • Redis.sinter
  • Redis.sinterstore
  • Redis.smove
  • Redis.sunion
  • Redis.sunionstore
  • Redis.zinterstore
  • Redis.zunionstore
  • Redis._zaggregate

All Hash and Channel related functionality.

Any function listed as deprecated in the redis-py code is not implemented in mredis.


Redis.sort will not store currently, needs to be extended to manually store to the correct server.

Currently the only hash_method supported is "standard" which is a crc32 value of the key mod the number of servers.