Question: Is Memcached multi-threaded?

Answer

Yes, Memcached is multi-threaded. It uses a single process with multiple threads for handling concurrent client requests. The number of threads can be configured through the "-t" option while starting the Memcached instance.

Each thread runs independently and handles a subset of client connections in a non-blocking manner. When a request is received from a client, the thread looks up the requested data in the cache and returns it if found. If the data is not present in the cache, the thread fetches it from the backend storage and stores it in the cache for future access.

Here's an example of starting a Memcached instance with 4 threads:

$ memcached -t 4

By default, Memcached uses as many worker threads as there are CPU cores available on the system. However, it's recommended to limit the number of threads to avoid contention and context switching overheads.

It's also worth noting that Memcached uses a slab allocator for memory management, which allows for efficient allocation and deallocation of small fixed-size chunks of memory. This architecture is well-suited for multi-threaded environments where multiple threads need to allocate and free memory frequently.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book
Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.