carlos8f / haredis

High-availability redis in Node.js.
https://npmjs.org/package/haredis
154 stars 21 forks source link

using many haredis clients #13

Closed sdarwin closed 11 years ago

sdarwin commented 11 years ago

Let's say you have a single redis cluster on the back-end with 3 redis servers. On the front-end there are 10 web servers which are clients of that redis cluster. They all use haredis.

Series of events:

  1. A redis server dies.
  2. The first client realizes this and triggers a failover event. It promotes another redis server to master, and issues slaveof on the rest of the slaves.
  3. What will the other haredis clients do? Will they realize what happened and not choke? They will need to react differently from the first client, in that they will need to switch to using the new master, and NOT elect a new master, and NOT issue slaveof commands... presumably....
carlos8f commented 11 years ago

when a client detects a problem (more or less than 1 master in the cluster) it calls the prototype.failover method, which attempts to lock all the redis servers by writing a time-sensitive key to them pointing to the client's id. this way multiple clients will (in theory) not try to elect a new master at the same time. the clients that are waiting also listen on a gossip pub/sub channel, and will switch to the new master right after it gets elected by the client that got the lock.