solomonHou / redis

Automatically exported from code.google.com/p/redis
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Add this to faq: A technique for guaranteeing syncronous writes to slaves #552

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
I described this technique here: 
http://groups.google.com/group/redis-db/browse_thread/thread/e8cb5653aad0e5f1

A commenter advised this should be added to the FAQ.

I thought of this method to guarantee writes to slave (kinda like 
mongodb's ability to tell you when a write has been written to 
replicas): 

* Have client establish two connections - one to master redis and one 
to slave redis 

Now when client wants to write KEY to both master and slave: 

1. Client SUBSCRIBEs to  KEY on *slave* 
2. Client writes KEY (e.g. set foo bar) to master 
3. Client publishes KEY to master 

Assuming redis-master will serialize the updates: 
4. Master will replicate KEY to slave 
5 Master will propogate *PUBLISH KEY* to slave 

Finally client: 
6. Gets KEY event on SUBSCRIBE! 

You can probably replace Pub and Subs with RPUSH and BLPOP 
respectively. 

This works because master will serialize operations from a client to slave - so 
write to KEY and then publish event for key are guaranteed to happen in the 
same order on slaves.

It requires two connections for every request, but it is theoretically more 
faster than requiring redis to fsync on every write (using aof).

Original issue reported on code.google.com by kashifsh...@gmail.com on 19 May 2011 at 2:06

GoogleCodeExporter commented 8 years ago
Regarding the suggestion to use RPUSH and BLPOP instead of PUBLISH and 
SUBSCRIBE, it seems that it would not work because the slave is read only, and 
the BLPOP operation removes the element off the slave that it is reading it 
from.  I think PUBLISH / SUBSCRIBE is the way to go.

Original comment by joecroni...@gmail.com on 2 Jun 2011 at 10:48

GoogleCodeExporter commented 8 years ago
You would BLPOP on the slave, then upon completion, BLPOP on the master. It is 
2 commands to the master and 1 to the slave, but it has the same effect.

Original comment by josiah.c...@gmail.com on 3 Jun 2011 at 12:35

GoogleCodeExporter commented 8 years ago
Thanks for clearing that up.  I just tested using BLPOP the way you described, 
and it works great.  Much appreciated!

Original comment by joecroni...@gmail.com on 3 Jun 2011 at 3:57

GoogleCodeExporter commented 8 years ago

Original comment by pcnoordh...@gmail.com on 14 Jun 2011 at 7:13