Avoiding breaks when using replication - Redis

master-slave, php, redis, replication, session

Solution

For example I have replication period for redis each 10 seconds

I assume you are referring to the setting `repl-ping-slave-period` - that's the time interval for a ping from master to the slave to confirm the link is up.

Redis does not have a replication period. Master sends commands to the slaves as soon as possible. The slave can still lag, but this lag isn't because of the 10s setting you specified.

You can verify this locally. Open a telnet session, connect to localhost 6379 (or wherever your master is). Enter `sync`. The master will first send a dump file, and then send you commands as and when you modify data on the master. You will also see a ping every 10s.

How I should configure thing like PHP session to not break user experience?

Its best to have a dedicated server for Redis master and slave. Don't install Redis on the machine which has your webserver.

All 3 webservers should connect to Redis master directly. If the master goes down for some reason, the webservers should switch over to the slaves.

How do you switchover from master to slave? For now, you'd have to do that manually. Salvatore has been talking about Redis-Sentinel, which will solve this problem, but that solution does not exist today.

Several people have implemented solutions that automate the failover, but for such a small setup, I'd advise going with monitoring scripts and doing the failover manually.

Problem

We have 1 master with Redis. And 2 slaves of that master. Slaves also used as WebServer. We are using Redis for PHP sessions. We are load balancing using Round Robin DNS. So it means request hits at "random" server. How I should configure thing like PHP session to not break user experience. For example I have replication period for redis each 10 seconds. User logins and session created. After some seconds user requests new page and hits at other server. But in that server Redis is not replicated yet. What to do in this case when data is critical in this matter? We have also non critical data in redis, so we dont want to set replication period to very small period and make load to master. Actually this is not applicable only for Redis. I don't know yet, maybe we will have same problem with MySQL.

Original source