Databases 44 min read

Redis Multithreaded I/O and BIO System Evolution: Architecture, Code Analysis, and Lazyfree

This article examines Redis's transition from a single‑threaded reactor to a multi‑threaded I/O model, details the underlying I/O multiplexing and BIO subsystems, and explains the lazyfree mechanism for asynchronous large‑key deletions, providing extensive code analysis and performance insights.

Top Architect
Top Architect
Top Architect
Redis Multithreaded I/O and BIO System Evolution: Architecture, Code Analysis, and Lazyfree

Redis has long been considered single‑threaded for its core network model, but the article explains the precise definition of “single‑threaded” and shows how Redis used a single‑threaded reactor before version 6.0.

It then describes the I/O multiplexing layer (epoll, kqueue, select) and the Reactor pattern that processes client connections, and details the transition to a multi‑threaded I/O model introduced in Redis 6.0, where a main thread distributes read and write tasks to several I/O worker threads using a round‑robin scheduler.

Key source functions such as initThreadedIO , postponeClientRead , handleClientsWithPendingReadsUsingThreads and IOThreadMain are examined to illustrate task queuing, lock‑free counters, and busy‑waiting synchronization.

The article also revisits the older BIO subsystem, showing its task‑queue implementation with pthread_mutex , pthread_cond , and the bioCreateBackgroundJob function used for AOF fsync and file closing.

Finally, it covers the “lazyfree” extension added in Redis 4.0 that offloads large‑key deletions to background threads, presenting the dbAsyncDelete and unlinkCommand logic and the threshold‑based decision to use the BIO lazy‑free thread.

performancedatabaseRedisI/OmultithreadingLazyfree
Top Architect
Written by

Top Architect

Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.