Real-World Production Deep Dives: In-Memory Cache (Redis & Twemproxy)

โš ๏ธ Not yet source-checked. These case studies summarize public engineering posts and talks, and some details may be simplified or wrong. Before you repeat a specific number or design in an interview, check it against the company's own write-up. Found an error, or a source that confirms a section? Add it to the tracking issue. Sourced fixes are the most welcome contribution here.

Deep-dive architectural dissections of how Twitter / X and Netflix implement massive in-memory caching fleets, prevent slab fragmentation, manage connection pooling, and sustain hundreds of millions of operations per second.


๐Ÿฆ Deep Dive 1: How Twitter / X Uses In-Memory Caching (Twemproxy & Pelikan)

1. Production Context & Scale

2. The Core Problem at Twitter Scale

  1. TCP Connection Exhaustion:
    • Thousands of frontend backend services connecting directly to hundreds of Redis/Memcached instances created millions of active TCP sockets, exhausting server file descriptors and causing CPU thrashing due to kernel context switching.
  2. Memory Fragmentation & The "Out of Memory" Paradox:
    • Standard memory allocators (like malloc) suffer from memory fragmentation when handling millions of variable-sized tweet payloads. A cache node could report 90% memory utilization yet fail an allocation due to lack of contiguous free memory.

3. Twitter's Architecture: Twemproxy & Pelikan

[ Frontend API Services (Thousands of Clients) ]
                        โ”‚
                        โ–ผ
         [ Twemproxy (nutcracker) Layer ]
  โ”œโ”€โ”€ Client Connection Multiplexing (1000s -> 10 persistent links)
  โ”œโ”€โ”€ Request Pipelining (Batches queries into single TCP packets)
  โ””โ”€โ”€ Consistent Hashing (Ketama ring with Virtual Nodes)
                        โ”‚
      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
      โ–ผ                 โ–ผ                 โ–ผ
[ Cache Node 1 ]  [ Cache Node 2 ]  [ Cache Node 3 ]
  (Pelikan Segregated Slab Allocator Engine)

A. Connection Multiplexing via Twemproxy

B. Pelikan & The Segregated Slab Allocator

To eliminate memory fragmentation permanently, Twitter designed Pelikan Cache:


๐ŸŽฌ Deep Dive 2: How Netflix Uses EVCache at Scale (Multi-Tier In-Memory Architecture)

1. Production Context & Scale

2. The Multi-Region Replication Mechanics

[ us-east-1 Microservice ]                   [ eu-west-1 Microservice ]
            โ”‚                                            โ”‚
            โ–ผ                                            โ–ผ
   [ EVCache Client East ]                      [ EVCache Client EU ]
            โ”‚                                            โ”‚
   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                          โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
   โ–ผ                 โ–ผ                          โ–ผ                 โ–ผ
[ Node East-A ] [ Node East-B ]              [ Node EU-A ]   [ Node EU-B ]
   โ””โ”€โ”€ Rend Proxy   โ””โ”€โ”€ Rend Proxy              โ””โ”€โ”€ Rend Proxy   โ””โ”€โ”€ Rend Proxy
         โ”‚                 โ”‚
         โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                  โ”‚ (Async Replication via Kafka)
                  โ–ผ
         [ Cross-Region Kafka Stream ] โ”€โ”€> Updates EVCache in EU & West

A. Asynchronous Multi-Region Replication

B. The Rend Proxy Hybrid Storage (RAM + NVMe Flash)


โš–๏ธ Comparative Architecture Matrix

Architectural Dimension Twitter Pelikan Cache Netflix EVCache
Core Storage Engine Custom Segregated Slab Allocator in C Hybrid Memcached (RAM) + RocksDB (NVMe)
Proxy Layer Centralized Twemproxy / Embedded Pelikan Rend proxy co-located on each cache node
Replication Model Master-Replica with Twemproxy failover Cross-AZ synchronous + Cross-Region async via Kafka
Connection Strategy Aggressive client multiplexing & pipelining Client-side connection pooling in Java EVCache SDK
Memory Management Slab classes; zero runtime OS malloc Slab-based Memcached memory model

๐ŸŽฏ System Design Interview Takeaways

  1. Never Allow Direct Connection Flooding: In large-scale caching designs, explain that having 10,000 microservice pods connect directly to Redis exhausts file descriptors and CPU. Recommend a connection proxy layer (like Twemproxy or Envoy) to multiplex client connections.
  2. Slab Allocation vs Malloc: Impress interviewers by explaining how in-memory caches avoid memory fragmentation: Slab Allocation pre-allocates memory into fixed chunk classes, eliminating the external fragmentation caused by standard malloc/free.
  3. RAM + NVMe Tiering: For multi-terabyte cache designs where RAM is cost-prohibitive, propose a hybrid storage tier (RAM for hot data + NVMe RocksDB for warm data) inspired by Netflix's Rend architecture.

๐Ÿข Deep Dive 3: How Google Scales In-Memory Caching (Web Search Serving Tier)

1. Production Context & Scale

2. Architecture: Inverted Index In-Memory Partitioning