venkatesh
№'011' · MAY 2026 · 2 MIN READ

Redis Keys vs Scan — Which to Use?

redis.keys() vs redis.scan() — blocking vs non-blocking

We use Redis to make our service faster…

But Redis increased our API latency. 💀

The culprit? redis.keys("user:*") — running at runtime to find keys.

Looked fine. Worked totally fine in local.

But in production…

👉 Redis is single-threaded 👉 KEYS blocks the entire server while searching 👉 Every request — reads, writes, everything — just waits

At small scale? Milliseconds. Nobody notices. At production scale? 2 second freeze. Every. Single. Call.

Our API latency spiked. No DB issue. No slow query. Infra looked clean. It took eternity to suspect Redis.

The fix was simple — SCAN instead of KEYS

SCAN iterates using a cursor: → small batches → non-blocking → other Redis operations continue normally

-- ❌ Blocks everything
KEYS user:*

-- ✅ Iterates in small batches, non-blocking
SCAN 0 MATCH user:* COUNT 100

Not instant. Takes multiple round trips. But Redis stays unblocked the whole time.

One command change. Latency back to normal.

Redis is incredibly fast… until it freezes your production 😄

copied!