Skip to main content
← PROBLEMS / ARCHITECTURE
An end-to-end domain learning journey

Preventing Redis Big-Key
Event Loop Freezes.

Deleting or expiring a 480 MB Redis Hash with 2.5M fields via synchronous DEL blocks Redis's single-threaded aeMain event loop for 820ms while jemalloc frees millions of allocations, causing fleet-wide client timeouts.
Start the journey5 stages
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

When a single bottleneck threatens end-to-end availability, replace DEL with async UNLINK, enable lazyfree-lazy-expire/eviction, and shard multi-million-entry collections into 64 hash buckets (< 8 MB each) or delete in 500-field HSCAN+HDEL batches.

TL;DR30-second simplified summary
▾

Core Mechanics: Redis 7 executes all commands sequentially on a single aeMain event-loop thread.

Visual & Structural Flow: Depicts Synchronous DEL on 2.5M-Field Hash Blocks 85k OPS Clients (Refresh Job -> Redis aeMain -> 85k OPS Clients); 64-Bucket Hash Sharding & bio_lazy_free Offload (redis-py Client -> aeMain (O(1) UNLINK) -> bio_lazy_free -> catalog:v2:00..63 (7.5 MB/shard)).

Trade-Offs & Production: Replacing DEL with UNLINK in Python (redis-py) fixes manual deletes, but leaves automatic TTL expiration and maxmemory eviction synchronous unless. Big-key governance is essential for any single-threaded in-memory store: Feature-Flag & Product Catalogs: Versioned, sharded hashes prevent refresh stampedes and event-loop stalls.

Failure Modes & Application: Big-key governance is essential for any single-threaded in-memory store: Feature-Flag & Product Catalogs: Versioned, sharded hashes prevent refresh stampedes and event-loop stalls.

Full walkthrough · 5 stages
01
STAGE 01 · WHY IT MATTERSL4 · FUNDAMENTALS

Why a single 480 MB Redis Hash freezes an 85,000 OPS primary.

Why this matters: The mechanism: understand how Why a single 480 MB Redis Hash freezes an 85,000 OPS primary works from first principles.

Think of Why a single 480 MB Redis Hash freezes an 85,000 OPS primary as the intuition layer for Preventing Redis Big-Key Event Loop Freezes: The mechanism: understand how Why a single 480 MB Redis Hash freezes an 85,000 OPS primary works from first principles.

Redis 7 executes all commands sequentially on a single aeMain event-loop thread. Storing 2.5 million entries inside one Redis Hash (catalog:items, ~480 MB RSS) turns a routine DEL catalog:items or 24-hour TTL expiration into a synchronous traversal of 2.5M dictEntry allocations—blocking aeMain for 820–1,200 ms.

02
STAGE 02 · IMPACTL5 · APPLIED DESIGN

Fleet-wide 200 ms client timeouts and cache-stampede gaps.

Why this matters: The problem: see what breaks without Fleet-wide 200 ms client timeouts and cache-stampede gaps under real concurrency or scale.

When aeMain freezes for 820 ms, every concurrent GET queues behind the DEL:

  1. 85,000 OPS Timeout Storm: Clients with 200 ms SocketTimeoutException limits fail across the entire fleet.
  2. Empty-Cache Stampede: Deleting the entire catalog key at once leaves a window where all catalog reads miss and slam the origin database.
Single-Threaded Event Loop Freeze

Synchronous DEL on 2.5M-Field Hash Blocks 85k OPS Clients

Refresh JobRedis aeMain85k OPS Clients1. Normal GETs (0.8ms p99)2. DEL catalog:items (2.5M fields)3. jemalloc frees 2.5M nodes (820ms)4. GETs queue -> 200msSocketTimeout5. Fix: UNLINK + 64 shards(<0.1ms)

UNLINK detaches the key in O(1) (~0.08ms) and offloads memory deallocation to the bio_lazy_free background thread.

Engineering Decision Framework
Problem
How to implement Preventing Redis Big-Key Event Loop Freezes without hitting unbounded tail latency, state divergence, or operational fragility.
Naive Solution
Use a single unpartitioned path with unbounded retries and synchronous global coordination.
Why It Fails
Global coordination serializes throughput under load, while unbounded retries amplify transient stalls into full outages.
Alternatives
Compare synchronous strict-consistency paths against bounded asynchronous replication, local caching, or partitioned ownership.
Engineering Decision
When a single bottleneck threatens end-to-end availability, replace DEL with async UNLINK, enable lazyfree-lazy-expire/eviction, and shard multi-million-entry collections into 64 hash buckets (< 8 MB each) or delete in 500-field HSCAN+HDEL batches.
Trade-Offs
Gains predictable p99 latency and fault isolation in exchange for explicit bounding, observability, and slightly higher implementation complexity.
Decision Exercise · Make the Call

When Preventing Redis Big-Key Event Loop Freezes experiences a sudden 5x traffic spike alongside a slow downstream dependency, which engineering choice best protects system stability?

03
STAGE 03 · NAIVE SOLUTIONL5 · APPLIED DESIGN

Why calling UNLINK in code alone still freezes on TTL expiration.

Why this matters: The problem: see what breaks without Why calling UNLINK in code alone still freezes on TTL expiration under real concurrency or scale.

Think of Why calling UNLINK in code alone still freezes on TTL expiration as the intuition layer for Preventing Redis Big-Key Event Loop Freezes: The problem: see what breaks without Why calling UNLINK in code alone still freezes on TTL expiration under real concurrency or scale.

Replacing DEL with UNLINK in Python (redis-py) fixes manual deletes, but leaves automatic TTL expiration and maxmemory eviction synchronous unless lazyfree-lazy-expire yes and lazyfree-lazy-eviction yes are enabled in redis.conf. Worse, a single 480 MB key still causes HGETALL or replica full-sync stalls.

Request & State Flow

Why calling UNLINK in code alone still freezes on TTL expiration

App WorkerSingleflight / L1Redis L2Origin DB1. GET key (coalesce duplicates)2. Check L2 distributed cache3. Single origin query on miss4. Populate cache with jitteredTTL
04
STAGE 04 · ENTERPRISE SOLUTIONL6 · SCALE & TRADE-OFFS

Enterprise solution: UNLINK, Lazy-Free Config, and 64-Bucket Sharding.

Why this matters: Operational control: configure bounds, failure handling, and safe production defaults.

Eliminate > 2 ms event-loop blocks and zero-cache refresh gaps with three steps:

  • Async Deallocation (UNLINK + lazyfree-lazy-*): Use UNLINK (0.08 ms O(1) pointer detach onto bio_lazy_free) and enable lazyfree-lazy-expire yes, lazyfree-lazy-eviction yes, and lazyfree-lazy-server-del yes.
  • 64-Bucket Hash Sharding: Partition catalog:items across catalog:items:00..63 via CRC16(item_id) % 64, shrinking each key from 480 MB to 7.5 MB (~39k fields).
  • Versioned Blue-Green Swap: Write the refreshed catalog to catalog:v2:00..63, atomically flip a tiny catalog:active_version pointer, and UNLINK v1 keys asynchronously (or HSCAN+HDEL in 500-field batches).
Sharded & Lazy-Free Redis

64-Bucket Hash Sharding & bio_lazy_free Offload

redis-py ClientCRC16(id) % 64aeMain (O(1) UNLINK)0.08ms detach, <10MB…bio_lazy_freeAsync jemalloc freecatalog:v2:00..63(7.5 MB/shard)Zero-gap versioned…

1.O(1) UNLINK: Detaches key from keyspace in ~0.08ms and frees memory on bio_lazy_free.

2.Lazy-Free Config: Enable lazyfree-lazy-expire yes and lazyfree-lazy-eviction yes in redis.conf.

3.64-Bucket Sharding: Keeps every key <8 MB so even HSCAN/HDEL batches stay sub-millisecond.

Sharding big hashes below 10 MB and enabling lazy-free across deletes, expirations, and evictions keeps p99 at 0.8 ms.

05
STAGE 05 · SYSTEM DESIGN APPLICATIONSL7 · STAFF / PRINCIPAL

Where Redis big-key mitigation applies in production.

Why this matters: Scope boundary: identify where this pattern fits best and where simpler or async alternatives win.

Big-key governance is essential for any single-threaded in-memory store:

  • Feature-Flag & Product Catalogs: Versioned, sharded hashes prevent refresh stampedes and event-loop stalls.
  • Social Feeds & Leaderboards: Cap ZSET / LIST lengths with ZREMRANGEBYRANK / LTRIM and shard by time bucket.
  • Cluster Migrations: Small < 10 MB keys prevent MIGRATE slot-transfer timeouts in Redis Cluster.
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

Why can Redis still freeze for hundreds of milliseconds even after replacing all application `DEL` calls with `UNLINK`?

Recommended Learning Flow