Fastness of Redis

aditya goel
4 min readFeb 18, 2023

Question:- What is Redis ?

Answer → Redis is a very popular In-Memory database and most loved database amongst developer community because of following reasons :-

  • It’s rock-solid.
  • It’s Easy-to-use.
  • It’s fast-enough.

Question:- Why is Redis so fast ?

Answer → Here are the reasons of Redis being so fast :-

Reason #1.) The first reason Redis is fast is because : It is an In-Memory database.

Reason #2.) Memory-Access of RAM is several orders of magnitude faster than random disk I/O.

Reason #3.) Redis is primarily Single-Threaded.

Question:- Why are the benefits of Pure-Memory access ?

Answer → Pure memory access provides :-

  • High Read & Write Throughput.
  • Low Latency.

The trade-off is that, the dataset can’t be larger than the RAM memory.

Question:- From the coding prospective, accessing which option is better ?

Answer → Code-wise, In-memory data-structures are much easier to implement than On-Disk counterparts.

This keeps the code simple and it contributes to Redis’s rock solid stability.

Question:- Why would a Single-Threaded design would lead to high performance ? Would not it be faster, if it uses threads to leverage all the CPU-Cores ?

Answer → There are following reasons that Multi-threaded is not so fast :-

  • Multi-threaded applications require Locks Or Other Synchronisation mechanisms. They are notoriously hard to reason about.
  • In many applications, Added complexity is Bug-prone and sacrifices stability, making it difficult to justify the performance gain.
  • In case of Redis, the Single threaded code path is easy to understand.

Question:- How does a single threaded codebase handle many thousand of incoming requests and outgoing responses at the same time ? Would not a thread gets blocked, waiting for the completion of each request individually ?

Answer → This is where, I/O multiplexing comes into the picture.

  • With I/O Multiplexing, the Operating-System allows a single thread to wait on many socket-connections simultaneously.
  • Traditionally, this is done with select OR poll system calls. These System-Calls are not so highly performant, when there are many thousand of connections.

Question:- On LINUX Operating-System, how does this I/O Multiplexing happens ?

Answer → On Linux-System, epoll is the performant variant of I/O Multiplexing that supports many thousand of connections in constant time.

Question:- Are there any drawbacks of the Single-Threaded Design ?

Answer → It doesn’t leverage all the CPU cores available in modern hardware and therefore, For some workloads, it is quite common to have several Redis Instances running on a single server to utilise more CPU Cores.

Question:- Are there other Redis Compatible Servers ?

Answer → Yes, this is true :-

  • There are attempts at implementing new Redis Compatible servers to squeeze more performance out of a single-server.
  • With Redis’s ease of use, rock-solid-stability and performance, Redis still provides the best performance and stability trade-off amongst all the available options.

That’s all in this document. If you liked reading it, then do clap on this page. We shall see you in the next document.

--

--

aditya goel

Software Engineer for Big Data distributed systems