How does Redis work?

Snehil Verma
Snehil Verma
Published in
2 min readSep 20, 2023

Let’s start with what is redis.
Redis is a tool that is used as a caching layer in the backend application. To read more about Redis please follow the link.

Why is Redis a high-throughput-low-latency system?

Storage- Redis while writing the data on the system stores it on the RAM. RAM access is faster than accessing the disk storage. There is a catch, obviously, we can’t store all the data in my RAM so Redis returns the response to the client and periodically stores that information from RAM to the disk.

Read- Redis stores data in the form of a key-value pair that represents a hash map. This is really important for Redis to retrieve data quickly because the time complexity for searching reduces down to O(1). A hash map is the most commonly used data structure in Redis to perform bare minimum operations. To learn about the other data structures please follow this link.

Architecture- Redis has the concept of an event loop. Event loop, yes it is the same concept on which javascript works. Redis has its own event library that is implemented in C language.
Redis keeps the main thread unblocked and pushes all the queries into the event queue. This helps the main thread to keep accepting new queries at sale. In the queue the is a function Epoll that is the part of file descriptor(fd) library in Linux. Once the fd reads the data that is stored it sends the response to the Epoll function and it send it back to the event loop. The event loop sends it back to the main thread from which the data is sent back to the client.

Some alternatives of Redis are Aerospike, Memcached. Happy Reading!!

--

--