Why is Redis so fast?

Redis is an open-source in-memory database/data store, which means that its access is 1000 times faster than random disk access. Unlike traditional databases, Redis holds all the information in memory as opposed to performing continuous reads and writes. This allows for constant-time data access, which is crucial for operations requiring high speed. Redis uses Jemalloc, a general purpose memory allocation library that focuses on offering robust concurrency support as well as minimising memory fragmentation. While this seems rather a nice catchy sentence, minimising memory fragmentation offers several benefits, as follows:

  • Improved performance. Fewer cache misses and potentially higher cache efficiency that can result in faster application execution as more data can be stored in faster-access memory spaces.
  • Efficient usage of memory. By reducing fragmentation, a system can make more efficient use of its available memory. This means that more data can be stored within the same amount of physical memory, reducing the need for expensive memory allocation and deallocation operations.
  • Reduced Memory Waste: Fragmentation leads to “holes” of unused memory scattered throughout the memory space, which can significantly waste valuable memory resources. Minimising fragmentation ensures that these holes are either eliminated or reduced, thus lowering the amount of wasted memory.
  • Predictable Performance: Applications that rely on consistent and predictable performance benefit from reduced memory fragmentation, as it leads to more stable memory usage patterns. This predictability is crucial for real-time systems and high-performance computing applications where timing is critical.
  • Longer Runtime without Degradation: Systems that run for extended periods without restarting (such as servers) can suffer from increased fragmentation over time, which can degrade performance. Allocators that minimise fragmentation can help maintain consistent performance over longer runtimes.
  • Enhanced Stability and Reliability: By ensuring efficient memory usage and reducing the chances of memory allocation failures due to fragmentation, systems can achieve higher stability and reliability. This is particularly important in embedded systems and critical applications where failures can have significant consequences.

The fact that Redis is so fast is, however, due to a combination of factors beyond the speed of in-memory data storage. For example:

  • Single-Threaded Architecture: Redis follows a single-threaded architecture, which removes the need for context switching and synchronisation between threads. This leads to reduced overhead and increased performance. This might seem rather counter-intuitive, but, in simple terms, it means that Redis handles tasks without the complications of multiple threads. This setup, combined with smart use of system features like event loops and I/O multiplexing, allows Redis to efficiently manage many connections and operations at once.
  • Non-Blocking I/O: Redis employs non-blocking I/O techniques, enabling it to manage mutiple connections at once without halting other processes, which helps maintain swift request handling, even when the system is under significant load.
  • Optimised Data Structures: Redis provides optimised data structures like Lists, Sets, Sorted Lists, Hashes, Bitmaps, Bitfields, Streams, etc which are designed to perform specific operations efficiently. For example: with Sorted Sets it is possible set operations such as union, intersection, set difference, and compute cardinality.
  • Lua Scripting: Redis offers Lua scripting support, enabling developers to craft intricate commands and run them directly on the server. This feature minimises network delay and latency and boosts efficiency by executing scripts server-side.

References:

Jemalloc General Purpose Memory Allocator

Lua Scripting

Posted on February 28, 2024, in Development and Testing. Bookmark the permalink. Leave a comment.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.