sharded_eredis

Copyright (c) 2012 PlayMesh, Inc.

sharded_eredis is collection of pools of Redis clients, using eredis and poolboy. Each pool points to a different shard.

This project modifies the original repository by including a consistent hashing library to simplify presharding, suggested by antirez. Read the original blogpost here.

The name has been changed so as not to induce confusion for those originally using hiroeorz's sharded_eredis; the functionality has changed significantly.

eredis: https://github.com/wooga/eredis

poolboy: https://github.com/devinus/poolboy

Caveats!!

This library uses Redis but in a distributed fashion. Multi-object operations are no longer guaranteed to be atomic and are not supported by this library. Examples include multisets, multigets, source-destination operations, and multi-object transactions. Transactions technically still work if all keys involved in the transaction are on the same machine but the Redis transaction was not built to be distributed.

Setup

  • git clone git://github.com/jeremyong/sharded_eredis.git
  • cd eredis_pool
  • make get-deps
  • make

Testing

make test

Config

Add new pools. This configuration specifies 4 shards.

  {env, [
          {global_or_local, local}
          {pools, [
                   {pool0, [
                              {size, 10},
                              {max_overflow, 20}
                              {host, "127.0.0.1"},
                              {port, 6378}
                             ]},
                   {pool1, [
                              {size, 10},
                              {max_overflow, 20},
                              {host, "127.0.0.1"},
                              {port, 6380}
                             ]},
                   {pool2, [
                              {size, 10},
                              {max_overflow, 20},
                              {host, "127.0.0.1"},
                              {port, 6381},
                             ]}
                   {pool3, [
                              {size, 10},
                              {max_overflow, 20},
                              {host, "127.0.0.1"},
                              {port, 6382},
                             ]}
                  ]}
        ]}

You can include this using the erlang -config option.

The global_or_local option is used to determine whether the pools are registered as local or global. Personally, I start this application on every instance and run pools on each instance locally to minimize network latency.

Examples

application start.

 sharded_eredis:start().
 ok

key-value set and get

 sharded_eredis:q(["SET", "foo", "bar"]).
 {ok,<<"OK">>}
 
 sharded_eredis:q(["GET", "foo"]).       
 {ok,<<"bar">>}

Note that the name of the pool does not need to be supplied with each query (in contrast to prior behavior). The library will automatically determine which pool associated to the correct shard should be invoked.

The Redis documentation can be referred to here.






本文作者:陈群
本文来自云栖社区合作伙伴rediscn,了解相关信息可以关注redis.cn网站。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值