I have setup a Redis-Cluster with version 3.0.5 of Redis-Server (Ubuntu 14.04)
For simplicity, we shall ignore replication. I have three redis instances running on localhost, ports 7001, 7002 and 7003. They are all made masters of a cluster with this command
redis-trib.rb create 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003
I like the idea of using twemproxy
twemproxy-config.yml
twem-1:
listen: 127.0.0.1:22121
hash: fnv1a_64
distribution: ketama
redis: true
preconnect: true
servers:
- 127.0.0.1:7001:1
- 127.0.0.1:7002:1
- 127.0.0.1:7003:1
I initialize twemproxy with nutcracker -c twemproxy-config.yml -d
and then I can access twemproxy with redis-cli -h 127.0.0.1 -p 22121
Please have a look at this input and output
127.0.0.1:22121> set hello 4542342342424
OK
127.0.0.1:22121> set goodbye 345353535545
(error) MOVED 9354 159.203.136.204:7002
127.0.0.1:22121> get hello
"4542342342424"
127.0.0.1:22121> get goodbye
(error) MOVED 9354 159.203.136.204:7002
I am concerned that this may not be working properly. If I bypass twemproxy and connect using redis-cli -c -h 127.0.0.1 -p 7001
, I can see automatic forwarding taking place. Like so;
127.0.0.1:7001> get hello
"4542342342424"
127.0.0.1:7001> get goodbye
-> Redirected to slot [9354] located at 127.0.0.1:7002
(nil)
127.0.0.1:7002> set goodbye 3240923842094840
OK
127.0.0.1:7002> get goodbye
"3240923842094840"
Reference
Interesting read at code.hootsuite.com about use of twemproxy (about halfway down page)
Ultimate aim
My ultimate aim is to use the redis cluster for storing PHP session data across multiple webservers behind a load balancer. In php.ini
I would have session.save_handler = redis
and session.save_path = tcp://127.0.0.1:22121
(the twemproxy instance would run on each web server). The PHP session configuration bit isn't set up yet.
I hope I am making sense. Am I using the correct hashing code? I really would like to see twemproxy giving back OK instead of MOVED.
Thanks!
Update
Thanks to the answer from @the-real-bill I have setup redis on two nodes with standard redis running port 6380 and sentinel running port 16380, one is master the other is slave. Watching the logs, everything looks good.
I've looked at predis and phpredis
This is the bit I'm still a little unsure about and I am sure given more time I will work it out. Queries can only be written to a master, and queries can only be read from a slave. We've got Sentinel promoting one or the other depending on availabbility - how does the session handler know which one it can write to? Surely I need to provide two IP adddresses..