Valkey Event Library
This past weekend I was working on yet another issue on Valkey (written in C). I had to do a lot of digging to understand certain parts of the system as I was completely lost. An interesting aspect of the system I found was the Event Library. Coming from a go background, I assumed that each network request was handled by a separate thread. “But threads are expensive resources”, I thought. How tf is it still fast and efficient if it has to handle these requests using multithreading and concurrency?
After some research, I discovered that instead of using different threads to handle each network request, an event library/event loop system is used.
How does this event library/event loop system work?
Let me paint a quick picture for you:
The network server listens for incoming connections on the port (default is 6379) and accepts them.
Each accepted request is associated with a descriptor(socket descriptor). A descriptor is an integer value that the OS Kernel uses to identify a file or other IO resources such as network sockets or pipes.
Once the descriptor is saved, perform a non-blocking read/write operation on it. This has to be non-blocking because if we block, we have to wait for each request to be handled before another. This quickly becomes a problem as the number of requests increases.
Performing non-blocking read/write operations.
Generally speaking, there are different ways to do this. However, since we have to do a lot of non-blocking read/write operations, we can make use of event libraries such as libevent, libuv etc which are specifically built for this purpose. Valkey uses an in-built event library (Don’t ask me why). The event library takes advantage of the OS kernel’s event notification mechanism(epoll for linux or kqueue for BSD-based systems) which subscribes to a file descriptor to get the latest changes to its state and then triggers a callback function based on this state.
In summary, the event library/event loop system is a very simple implementation at heart. It is a loop that processes network requests.
Valkey takes advantage of 3 things:
The underlying event notification mechanism of the OS kernel.
Everything is in memory, so most operations are really fast.
Network operations are slow so we can process one request while receiving another over the network.
Subscribe to my newsletter
Read articles from Samuel Adetunji directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by