Forget Redis: Why the world's biggest tech companies still rely on a "dumb" caching server from 2003.
By The Coding Gopher
Published: 2026-05-02
URL: https://thecodinggopher.substack.com/p/a-deep-dive-into-memcacheds-architecture
A technical deep dive into slab allocation, multi-threaded execution, and the philosophy of the “dumb server.”
Before Redis became the default caching solution for modern applications, there was Memcached. Originally developed in 2003 to speed up LiveJournal by alleviating database load, Memcached defined the concept of the distributed, in-memory key-value store.

While newer tools have introduced complex data structures and persistence, Memcached remains a foundational piece of infrastructure for giants like Facebook, Netflix, and Twitter. It survives because it does exactly one thing, and it does it with mathematically predictable, blistering speed: it stores chunks of data in RAM.
To understand why Memcached is still deeply relevant, we have to look past its simple API and examine its highly opinionated internal architecture.
Memcached is a free, open-source, high-performance distributed memory object caching system designed to speed up dynamic web applications. It works by caching frequently accessed data—such as database results, API calls, or page rendering—in RAM, reducing the load on slower backend databases and decreasing response times to microseconds.
1. The Philosophy: Dumb Servers, Smart Clients
If you deploy a cluster of ten Redis nodes, the nodes gossip with each other, elect leaders, and manage data routing.
\TheRedis gossip protocolis a decentralized, peer-to-peer communication mechanism used by nodes in a Redis cluster to share information and maintain a consistent view of the cluster's state without a central coordinator.\
If you deploy a cluster of ten Memcached nodes, they have absolutely no idea the other nodes exist.
Memcached operates on a “shared-nothing” architecture. There is no clustering protocol, no replication, and no internal communication. The servers are entirely dumb; they just listen on a port and execute SET and GET commands.
\*Memcached uses ashared-nothing architecture.
In a typical deployment, multiple Memcached servers operate independently of each other, possessing their own dedicated RAM and CPU resources. The servers do not communicate, share memory, or share disk storage with each other.\*
All the distributed logic lives in the Memcached Client (the library running in your application).
When your app wants to store a user session, the client library hashes the key, looks at the list of available Memcached IPs, and uses an algorithm (usually Consistent Hashing) to decide which specific server gets the data.
This extreme decoupling means you can scale a Memcached deployment infinitely. Adding 100 new cache nodes adds exactly zero network overhead to the existing cluster because there is no cluster state to synchronize.
2. Defeating Fragmentation: The Slab Allocator
When an application constantly allocates and frees random sizes of memory (like a 12-byte string, then a 500-byte JSON object, then a 2MB image), the RAM quickly becomes a slice of Swiss cheese. This is called memory fragmentation, and it eventually causes the system to crash or aggressively swap to disk.

Memcached solves this using a strict memory management system called the Slab Allocator. Instead of using the operating system’s standard malloc and free for every item, Memcached pre-allocates your RAM into highly structured blocks.

Here is the exact hierarchy of how it stores your data:
- Pages: Memcached grabs memory from the OS in massive, default 1-Megabyte chunks called Pages.
- Slab Classes: It divides these Pages into different “Classes” based on size.
- Chunks: Inside each Page, the memory is sliced into perfectly equal, fixed-size Chunks.
For example, Slab Class 1 might cut its 1MB page into 96-byte chunks. Slab Class 2 might cut its page into 120-byte chunks.
When you ask Memcached to store an 80-byte JSON string, it doesn’t just put it anywhere. It finds the smallest available chunk that fits the data (in this case, the 96-byte chunk in Slab Class 1).
The Trade-off: This eliminates memory fragmentation completely, ensuring predictable performance forever. However, it introduces internal wasted space. In the example above, storing 80 bytes in a 96-byte chunk wastes 16 bytes of RAM.
\Memcached increases the logically usable cache size primarily by aggregating memory from multiple, separate servers into a single, unified pool. Rather than relying on the limited RAM available on a single web server, application developers can distribute cache data across a cluster, enabling massive amounts of data to be stored in-memory (e.g., merging 50 servers with small RAM into a 3.2GB shared cache).\
Memcached divides memory into slabs, which are further broken down into chunks of fixed sizes (e.g., 64 bytes, 128 bytes, 1MB). When storing an item, Memcached fits it into the smallest slab class that can hold it. If you store a 50-byte item in a 64-byte chunk, 14 bytes are wasted. This wasted space is called internal fragmentation. Slabs are pre-allocated in 1MB pages. If a slab class is created but not fully used, that memory is locked to that size class, even if you need it for another size.

When Memcached starts, it often pre-allocates a large portion of its assigned memory (-m limit) to the slab allocator, rather than reserving it gradually as items are added. This makes the memory usage appear high immediately, regardless of how much actual data is stored.
𝐋𝐞𝐚𝐫𝐧 𝐭𝐨 𝐛𝐮𝐢𝐥𝐝 𝐆𝐢𝐭, 𝐃𝐨𝐜𝐤𝐞𝐫, 𝐑𝐞𝐝𝐢𝐬, 𝐇𝐓𝐓𝐏 𝐬𝐞𝐫𝐯𝐞𝐫𝐬, 𝐚𝐧𝐝 𝐜𝐨𝐦𝐩𝐢𝐥𝐞𝐫𝐬, 𝐟𝐫𝐨𝐦 𝐬𝐜𝐫𝐚𝐭𝐜𝐡. Get 40% OFF CodeCrafters: <https://app.codecrafters.io/join?via=the-coding-gopher>
This Substack is reader-supported. To receive new posts and support my work, consider becoming a free or paid subscriber.
3. Multi-Threaded by Design
This is where Memcached fundamentally diverges from its biggest rival, Redis. While Redis processes commands using a single thread, Memcached is natively multi-threaded.
Under the hood, Memcached utilizes libevent, a highly optimized asynchronous event notification library.
- The Main Thread: A single dispatcher thread listens for incoming network connections.
- Worker Threads: When a connection is established, the dispatcher hands it off to a pool of worker threads.
Because Memcached only handles simple key-value operations (it doesn’t have to manage complex operations like intersecting two massive sets), it can safely use granular locking mechanisms. Multiple threads can read and write to the cache simultaneously across multiple CPU cores.
If you provision a massive, 64-core AWS EC2 instance, a single Memcached process will effortlessly scale to utilize every single core, achieving throughputs of millions of operations per second on a single machine.
4. Eviction
Like all in-memory stores, Memcached eventually runs out of RAM. When this happens, it relies on an LRU (Least Recently Used) algorithm to delete old data.
LRU (Least Recently Used) caching is the primary mechanism for deciding which data to remove (evict) when the cache's memory capacity is reached. The principle is that items accessed less recently are less likely to be needed again and are thus the first to be removed to make room for new data.
However, because of the Slab Allocator, it doesn’t evaluate the entire cache at once. LRU eviction in Memcached is Slab-Specific.

If Slab Class 1 (the tiny chunks) is completely full, but Slab Class 5 (the large chunks) is mostly empty, Memcached will not evict a large chunk to make room for a tiny one. It will strictly evict the oldest item within Slab Class 1.
The Segmented LRU
The Segmented LRU (Least Recently Used) algorithm automatically divides memory into HOT, WARM, and COLD sub-LRUs (run by background threads) to manage data based on its access frequency (temperature). This ensures that a sudden, massive burst of new data doesn’t accidentally flush out your most critical, frequently accessed cache hits.

HOTLRU: Acts as a “probationary” queue for new items or recently updated data. Items are never bumped (moved to the head) withinHOT; once an item reaches the tail, it is moved toWARMif active (accessed frequently), orCOLDif inactive.
WARMLRU: Serves as a buffer for active data or items that are scanned frequently (e.g., via a web crawler). Items that have not been accessed at least twice generally do not enter theWARMtier. If aWARMitem is accessed, it is bumped back to the head; otherwise, it moves toCOLD.
COLDLRU: Holds the least active or frequently accessed items. Inactive items from bothHOTandWARMflow here. When Memcached runs out of memory, it evicts items from the tail of the COLD queue first.
How Items Move (Data Lifecycle)
- New Item: Enters the
HOTqueue.
- Inactive Item: Flows
HOT → COLDorWARM → COLD.
- Active Item (Re-accessed): If a
COLDitem is accessed again, it is queued to move asynchronously back toWARM.
Benefits of Segmented LRU
- Reduced Lock Contention: By using sub-LRUs, Memcached reduces the need for expensive global locks, improving performance on multi-threaded systems.
- Faster Scans: The LRU crawler can identify that “
HOT” items don’t need frequent scanning, saving CPU cycles.
- Better Retention: It prevents a large volume of one-time data (e.g., a batch job) from flushing out popular data, as the one-time data settles in the
COLDqueue and is evicted quickly.
Tuning and Monitoring
- Tuning: Memcached allows setting “percent hot” and “percent warm” to adjust the sizes of these queues dynamically on a running instance.
- Monitoring: Using
stats itemsorstats sizes, you can monitor if theWARMregion is larger than theCOLDregion, which is ideal, or if too many items are moving quickly toCOLD, signaling a need for policy adjustments.
In addition, a TEMP LRU exists for new items with very short TTLs (time-to-live), preventing them from polluting the main LRUs, though it is not enabled by default.
The Verdict: Memcached vs. Redis
Today, engineers inevitably face the choice between Memcached and Redis.

When to choose Memcached: If you are building an ultra-high-throughput system that only needs simple string or object caching, and you want to scale vertically across massive multi-core servers with zero cluster-management overhead, Memcached is the purest, most efficient tool for the job.
It is the C++ of databases: it gives you bare-metal speed, assumes you know what you are doing, and offers absolutely zero extra features to slow you down.
This Substack is reader-supported. To receive new posts and support my work, consider becoming a free or paid subscriber.
Thanks for reading! This post is public so feel free to share it.