PHPackages                             rediscluster/rediscluster - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. rediscluster/rediscluster

ActiveLibrary[Database &amp; ORM](/categories/database)

rediscluster/rediscluster
=========================

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

0.5.2(13y ago)291.3k7MITPHPPHP &gt;=5.3.0

Since Mar 13Pushed 12y ago4 watchersCompare

[ Source](https://github.com/salimane/rediscluster-php)[ Packagist](https://packagist.org/packages/rediscluster/rediscluster)[ Docs](https://github.com/salimane/rediscluster-php)[ RSS](/packages/rediscluster-rediscluster/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (1)Versions (3)Used By (0)

rediscluster-php
================

[](#rediscluster-php)

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

Project Goals
-------------

[](#project-goals)

The goal of `rediscluster-php`, together with [rediscluster-py](https://github.com/salimane/rediscluster-py.git), 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-php` uses [phpredis](https://github.com/nicolasff/phpredis.git)when connecting to the redis servers, thus the original api commands would work without problems within the context of a cluster of redis servers.

Continuous Integration
----------------------

[](#continuous-integration)

Currently, `rediscluster-php` is being tested via travis/drone.io ci for php version 5.3 and 5.4: [![Travis Status](https://camo.githubusercontent.com/ec77ef251d3b9cd58565b9da77c14ce88e50318ce2663bfad56c700529322aff/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f73616c696d616e652f7265646973636c75737465722d7068702e706e673f6272616e63683d6d6173746572)](http://travis-ci.org/salimane/rediscluster-php) [![Drone.io Status](https://camo.githubusercontent.com/6755c1c9e5628d94ac8b3326b79be860950cf09ecbeca7068f08d2c8228b31d1/68747470733a2f2f64726f6e652e696f2f6769746875622e636f6d2f73616c696d616e652f7265646973636c75737465722d7068702f7374617475732e706e67)](https://drone.io/github.com/salimane/rediscluster-php/latest)

Installation
------------

[](#installation)

Download via [Composer](http://getcomposer.org/)Create a `composer.json` file if you don't already have one in your projects root directory and require rediscluster:

```
{
  "require": {
    "rediscluster/rediscluster": "0.5.*"
  }
}
```

Install Composer:

```
$ curl -s http://getcomposer.org/installer | php
```

Run the install command:

```
$ php composer.phar install
```

This will download `rediscluster` into the vendor/rediscluster/rediscluster directory. To learn more about Composer visit

Running Tests
-------------

[](#running-tests)

```
$ git clone https://github.com/salimane/rediscluster-php.git
$ cd rediscluster-php
$ vi Tests/config.php
$ phpunit
```

Getting Started
---------------

[](#getting-started)

```
php -a
Interactive shell

php > require "/home/salimane/htdocs/rediscluster-php/vendor/autoload.php";
php > $cluster = array(
php (     //node names
php (     'nodes' => array(
php (       //masters
php (       'node_1' => array('host' => '127.0.0.1', 'port' => 63791),
php (       'node_2' => array('host' => '127.0.0.1', 'port' => 63792),
php (     )
php ( );
php >
php > $r = new RedisCluster\RedisCluster($cluster, 4);
php > var_dump($r->set('foo', 'bar'));
bool(true)
php > var_dump($r->get('foo'));
string(3) "bar"
```

Cluster Configuration
---------------------

[](#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 &amp; Write Masters
-------------------------------

[](#read-slaves--write-masters)

`rediscluster`, by default, 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 :

```
php > $r = new RedisCluster\RedisCluster($cluster, 4); // read redis commands are routed to slaves
...
php > $r = new RedisCluster\RedisCluster($cluster, 4, true); // read redis commands are routed to masters
...
```

Partitioning Algorithm
----------------------

[](#partitioning-algorithm)

In order to map every given key to the appropriate Redis node, the algorithm used, based on crc32 and modulo, is :

```
((abs(crc32()) % ) + 1)
```

A function `getnodefor` is provided to get the node a particular key will be/has been stored to.

```
php > print_r($r->getnodefor('foo'));
Array
(
    [node_2] => Array
        (
            [host] => 127.0.0.1
            [port] => 63792
        )

)
php >
```

Hash Tags
---------

[](#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:

```
php > $r->get("bar{foo}")
...
php > $r->mset(array("bar{foo}" => "bar", "foo" => "foo"))
...
php > $r->mget(array("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
----------------------------

[](#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.

```
php > foreach(array('b1', 'a2', 'b3') as $i) $r->sadd('bar', $i);
php > foreach(array('a1', 'a2', 'a3') as $i) $r->sadd('foo', $i);
php > var_dump($r->sdiffstore('foobar', 'foo', 'bar'));
int(2)
php >
php > print_r($r->smembers('foobar'));
Array
(
    [0] => a1
    [1] => a3
)
php >
php > print_r($r->getnodefor('foo'));
Array
(
    [node_2] => Array
        (
            [host] => 127.0.0.1
            [port] => 63792
        )

)
php > print_r($r->getnodefor('bar'));
Array
(
    [node_1] => Array
        (
            [host] => 127.0.0.1
            [port] => 63791
        )

)
php > print_r($r->getnodefor('foobar'));
Array
(
    [node_2] => Array
        (
            [host] => 127.0.0.1
            [port] => 63792
        )

)
php >
```

Redis-Sharding &amp; Redis-Copy
-------------------------------

[](#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](https://github.com/salimane/redis-tools#redis-sharding)and [redis-copy](https://github.com/salimane/redis-tools#redis-copy)

Information
-----------

[](#information)

- Code: `git clone git://github.com/salimane/rediscluster-php.git`
- Home:
- Bugs:

Author
------

[](#author)

`rediscluster-php` is developed and maintained by Salimane Adjao Moustapha (). It can be found here:

[![Bitdeli badge](https://camo.githubusercontent.com/a44cc0109457724132d986f5055e6247781b96c291ef6563168998737706c35a/68747470733a2f2f64327765637a68766c38323376302e636c6f756466726f6e742e6e65742f73616c696d616e652f7265646973636c75737465722d7068702f7472656e642e706e67)](https://bitdeli.com/free)

###  Health Score

30

—

LowBetter than 65% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 98% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~60 days

Total

2

Last Release

4746d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/40b316f7ba880daeeaf96c19c7ded747d978f647e8cfbab645bf4f9a08da4c5b?d=identicon)[salimane](/maintainers/salimane)

---

Top Contributors

[![salimane](https://avatars.githubusercontent.com/u/403938?v=4)](https://github.com/salimane "salimane (49 commits)")[![bitdeli-chef](https://avatars.githubusercontent.com/u/3092978?v=4)](https://github.com/bitdeli-chef "bitdeli-chef (1 commits)")

---

Tags

clusterphpredisdatabaseredisnosqlphpredisKey valueclustershardingredisclusterdata store

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/rediscluster-rediscluster/health.svg)

```
[![Health](https://phpackages.com/badges/rediscluster-rediscluster/health.svg)](https://phpackages.com/packages/rediscluster-rediscluster)
```

###  Alternatives

[cubettech/lacassa

Cassandra based query builder for laravel.

358.5k](/packages/cubettech-lacassa)[soosyze/queryflatfile

The Queryflatfile is PHP library for simple database not SQL

181.0k1](/packages/soosyze-queryflatfile)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
