Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Redis achieve the high throughput and performance?

I know this is a very generic question. But, I wanted to understand what are the major architectural decision that allow Redis (or caches like MemCached, Cassandra) to work at amazing performance limits.

  1. How are connections maintained?
  2. Are connections TCP or HTTP?
  3. I know that it is completely written in C. How is the memory managed?
  4. What are the synchronization techniques used to achieve high throughput inspite of competing read/writes?

Basically, what is the difference between a plain vanilla implementation of a machine with in memory cache and server that can respond to commands and a Redis box? I also understand that the answer needs to be very huge and should include very complex details for completion. But, what I'm looking for are some general techniques used rather than all nuances.

like image 749
Gopal Avatar asked Feb 12 '23 10:02

Gopal


1 Answers

There is a wealth of of information in the Redis documentation to understand how it works. Now, to answer specifically your questions:

1) How are connections maintained?

Connections are maintained and managed using the ae event loop (designed by the Redis author). All network I/O operations are non blocking. You can see ae as a minimalistic implementation using the best network I/O demultiplexing mechanism of the platform (epoll for Linux, kqueue for BSD, etc ...) just like libevent, libev, libuv, etc ...

2) Are connections TCP or HTTP?

Connections are TCP using the Redis protocol, which is a simple telnet compatible, text oriented protocol supporting binary data. This protocol is typically more efficient than HTTP.

3) How is the memory managed?

Memory is managed by relying on a general purpose memory allocator. On some platforms, this is actually the system memory allocator. On some other platforms (including Linux), jemalloc has been selected since it offers a good balance between CPU consumption, concurrency support, fragmentation and memory footprint. jemalloc source code is part of the Redis distribution.

Contrary to other products (such as memcached), there is no implementation of a slab allocator in Redis.

A number of optimized data structures have been implemented on top of the general purpose allocator to reduce the memory footprint.

4) What are the synchronization techniques used to achieve high throughput inspite of competing read/writes?

Redis is a single-threaded event loop, so there is no synchronization to be done since all commands are serialized. Now, some threads also run in the background for internal purposes. In the rare cases they access the data managed by the main thread, classical pthread synchronization primitives are used (mutexes for instance). But 100% of the data accesses made on behalf of multiple client connections do not require any synchronization.

You can find more information there: Redis is single-threaded, then how does it do concurrent I/O?

What is the difference between a plain vanilla implementation of a machine with in memory cache and server that can respond to commands and a Redis box?

There is no difference. Redis is a plain vanilla implementation of a machine with in memory cache and server that can respond to commands. But it is an implementation which is done right:

  • using the single threaded event loop model
  • using simple and minimalistic data structures optimized for their corresponding use cases
  • offering a set of commands carefully chosen to balance minimalism and usefulness
  • constantly targeting the best raw performance
  • well adapted to modern OS mechanisms
  • providing multiple persistence mechanisms because the "one size does fit all" approach is only a dream.
  • providing the building blocks for HA mechanisms (replication system for instance)
  • avoiding stacking up useless abstraction layers like pancakes
  • resulting in a clean and understandable code base that any good C developer can be comfortable with
like image 63
Didier Spezia Avatar answered Mar 23 '23 16:03

Didier Spezia