How does Redis achieve high performance and performance?

I know this is a very general question. But I wanted to understand what is the main architectural solution that allows Redis (or caches like MemCached, Cassandra) to work with amazing performance limits.

  • How are connections maintained?
  • Are the connections TCP or HTTP?
  • I know it is written entirely in C. How is memory managed?
  • What are the synchronization methods used to achieve high concurrent read / write throughput?

Basically, what's the difference between a simple vanilla machine implementation with a memory cache and a server that can respond to commands and Redis field? I also understand that the answer must be very large and must contain very complex details to complete. But I'm looking for some general methods, not all the nuances.

+3


source to share


1 answer


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

1) How are connections supported?

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

2) Are the connections TCP or HTTP?

Connections are TCP using the Redis protocol, which is a simple telnet compliant textual reference protocol that supports binary data. This protocol is usually more efficient than HTTP.

3) How is 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 was chosen as it strikes a good balance between CPU consumption, concurrency support, fragmentation, and memory footprint. The jemalloc source code is part of the Redis distribution.

Unlike other products (such as memcached), there is no slab allocator implementation in Redis.



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

4) What are the synchronization techniques used to achieve high throughput during concurrent read / write operations?

Redis is a single threaded event loop, so no synchronization is required as all commands are serialized. Now some threads also run in the background for internal purposes. In rare cases, they access data controlled by the main thread, using classic pthread synchronization primitives (such as mutexes). But 100% of data access done on behalf of multiple client connections does not require synchronization.

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

What is the difference between a simple vanilla machine implementation with a memory cache and a server that can respond to commands and the Redis field?

There is no difference. Redis is a simple vanilla machine implementation with a memory cache and a server that can respond to commands. But this is the implementation that runs correctly:

  • using single threaded event loop model
  • using simple and minimalistic data structures optimized for their respective use cases
  • offers a set of commands carefully chosen to balance minimalism and usefulness
  • constantly aiming for the best raw performance
  • well adapted to modern OS mechanisms
  • Providing multiple persistence mechanisms as a one-size-fits-all approach is a dream come true.
  • providing building blocks for HA mechanisms (e.g. replication system)
  • avoiding stacking useless layers of abstraction like pancakes.
  • which leads to a clean and understandable code base that any good C developer can be comfortable with
+6


source







All Articles