Redis request handling internal
<p>I have some confusion in redis. I am self learning redis.</p>

<p>I have got to know that redis is single threaded and it works on the concept of event loop. So read/write operations are serialized in redis and there is no race condition.</p>

<p>My confusion is - when I naively think about single threaded architecture, I can imagine that there is a buffer where all read/write requests gather and the thread schedules them one by one. But in a real life internet application where thousands or millions of request are to be processed, how does redis handle those requests without significant latency? If some write operation takes say few milliseconds time, does it block other read write operation during that period of time? </p>

<p>Does redis implement any locking concept like relational db? If no, then how redis handles thousands of read/writes without significant latency?</p>

<p>Any internals / examples would be great for my further study.</p>

<p>Your understanding of Redis internal is quite correct. There is no locking system. All operations are atomic and blocking.</p>

<p>The recommendation when using Redis, is to make multiple short requests, instead of a long one. Take in account the time complexity mentioned in Redis Commands documentation when writing your requests, if you work on a large number of keys or a large data structure. Avoid the KEYS command, prefer it the SCAN family of commands. Be even more careful when writing a Lua script which will be sent to Redis using the EVAL command.</p>

<p>Each request having a very short execution time, the clients won't be impacted, in most of the use cases, by the fact Redis commands won't respond to any other command during the execution of a given one.</p>

<p>Most of the time, the limiting factor won't be Redis itself, but the network. </p>

<p>However, in some use cases, you may hit Redis limits (which are very high). In these cases, you can use <a href="http://redis.io/topics/partitioning" rel="nofollow">multiple Redis instances</a> in master-slave mode (replication, monitored by <a href="http://redis.io/topics/sentinel" rel="nofollow">Redis Sentinel</a>), and make some kind of load balacing between the instances for reading requests. You can also use a tool like <a href="https://github.com/twitter/twemproxy" rel="nofollow">twemproxy</a> in front on several Redis instances.</p>

python redis nosql node-redis