September 4th, 2024

Why is single threaded Redis so fast

Redis is a high-performance, in-memory key-value database that achieves around 100,000 queries per second, utilizing a single-threaded architecture, efficient data structures, and non-blocking I/O for optimal performance.

Read original articleLink Icon
Why is single threaded Redis so fast

Redis is a high-performance, in-memory key-value database known for its impressive speed, achieving around 100,000 queries per second on a single machine despite its single-threaded architecture. The reasons for its efficiency include data storage in memory, efficient data structures, a single-threaded design, and non-blocking I/O. By storing data in memory, Redis eliminates the overhead associated with disk I/O. Its five data types—String, List, Hash, Set, and SortedSet—are optimized for speed through various underlying data structures. The single-threaded model reduces context switching and CPU consumption, avoiding race conditions and locking issues, which can hinder performance. Although Redis can block if a command takes too long, its design allows for efficient handling of multiple client connections through non-blocking I/O multiplexing. This model enables Redis to manage multiple network connections effectively, ensuring high performance. Redis 6.0 introduced multi-threading to address network I/O bottlenecks, allowing for concurrent handling of network data while maintaining single-threaded command execution to avoid concurrency issues. Multi-threading can be enabled in the configuration file, with recommendations for the number of threads based on the machine's core count.

- Redis achieves high performance through a single-threaded architecture.

- Data is stored in memory, eliminating disk I/O overhead.

- Non-blocking I/O multiplexing allows efficient handling of multiple connections.

- Redis 6.0 introduced multi-threading to improve network I/O performance.

- Command execution remains single-threaded to avoid concurrency issues.

Link Icon 6 comments
By @mcdeltat - 8 months
I feel like this article doesn't address the crux of why multithreading can't be faster. Kinda smells like LLM-style writing too - a lot of text that sounds like it's explaining something but skips too many details to be a great argument.
By @marklubi - 8 months
Has this author not been paying attention for the last 15 years?

antirez (Salvatore Sanfilippo) has been very clear about the reasons and benefits.

Article feels like the author was tasked with writing an article that was a certain number of words long and they phoned it in.

By @KingOfCoders - 8 months
Wasn't there an article on how Valkey rewrote Redis to multithreaded with large performance gains?
By @anonzzzies - 8 months
I don't know if it's the multithreading, but keydb is a multithreaded fork of redis and in our benchmark it's very fast; often faster than the advertised 5x.