Why Redis Can Slow Down Your App and How to Fix It
This article explains common Redis performance pitfalls—including per‑command network latency, expensive commands like SUNION and KEYS, mass key expiration spikes, and big‑key deletions—and provides practical techniques such as MSET batching, randomizing expirations, and using UNLINK to keep your backend fast.
1. Network and Communication Latency
Inserting many key‑value pairs one by one forces a round‑trip for each command, so network time dominates the latency. Using a batch command such as MSET groups the writes into a single request.
Test results on a local machine showed that inserting 20,000 records with a for loop took 5472 ms, while a single MSET call took only 126 ms – more than 40× faster. Similar batch commands (e.g., MGET, MHSET, HMGET) and pipelines can provide comparable gains.
2. Ignoring High‑Complexity Commands
Simple commands like SET or LPUSH are fast (under 10 ms), but commands that require heavy computation—such as SORT, LREM, SUNION —can be orders of magnitude slower. For example, SUNION on a 2 million‑item set took nearly 5000 ms, about 500× slower than a SET.
Redis processes commands in a single thread, so a long‑running command blocks all others, causing severe throughput drops under high concurrency. The KEYS command is especially dangerous because it scans the entire keyspace; testing with 100 million keys showed execution times exceeding 50 seconds, during which even a simple GET is blocked. Production environments should disable KEYS and avoid its use.
3. Concentrated Key Expiration
Redis provides two eviction strategies: lazy eviction (clean up on access) and active eviction (periodic random scans). Active eviction runs every 100 ms, scanning 20 random keys and deleting expired ones; if many keys expire simultaneously, the eviction loop can become a bottleneck, effectively adding pressure to the server.
To smooth expiration spikes, add a random offset to each key’s TTL, e.g.: expire(key, time + random(600)) This spreads expirations over time and reduces the chance of a massive eviction burst.
4. Big‑Key Issues
Keys that hold large amounts of data (big‑keys) make deletion expensive. A DEL on a big‑key can block the event loop for a noticeable duration, especially during traffic peaks.
Since Redis 4.0, the UNLINK command replaces DEL for asynchronous memory release, and Redis 6.0’s lazy‑free feature moves the freeing work to background threads, preventing main‑thread blockage. Nevertheless, it is best to avoid creating big‑keys in the first place and to split existing ones when possible.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
