DEV Community

tanmay kuamr chaurasia
tanmay kuamr chaurasia

Posted on

4 Essential Redis Patterns to Reduce Database Load and Response Times

Redis is often added to a stack as a silver bullet for performance. However, without a clean caching architecture, it frequently becomes a memory hog that serves stale data or crashes when memory fills up.

During architecture audits, the most common anti-pattern I see is unbounded caching: keys written without a Time-To-Live (TTL), sitting in memory until the server hits its memory limit or stale data breaks application logic.

Here is how to fix these underlying issues and implement production-grade caching patterns that protect your primary database.

1. Implement Cache-Aside (Lazy Loading)

The cache-aside pattern is the foundation of read-heavy backend services. Instead of letting your application read directly from the database or relying on complex sync mechanisms, your service controls the cache flow directly:

  1. Check if the key exists in Redis.
  2. If found (Cache Hit), return the data immediately.
  3. If missing (Cache Miss), read from the primary database.
  4. Populate Redis with the database response and return it.

This guarantees that Redis only stores data that is actively requested by your users, keeping your memory footprint lean.

2. Apply TTL Jitter to Prevent Thundering Herds

Setting a flat expiration time (for instance, exactly 1 hour on every user session) creates a hidden bottleneck. If 10,000 keys are generated during a traffic spike, all 10,000 keys will expire simultaneously 60 minutes later.

When they do, your application experiences a thundering herd (or cache stampede): thousands of concurrent requests miss Redis at the same second and hit your primary database all at once.

To prevent this, add random jitter to your TTL calculations. If your target TTL is 3600 seconds, add a random offset of ±30 to 60 seconds. This spreads key expirations evenly over a time window, keeping database load flat.

3. Handle Null Values with Negative Caching

If a user requests a record that does not exist in your database (e.g., querying user_id: 999999), a naive cache-aside implementation will fail to find it in Redis, query the database, receive null, and return.

If an attacker or bot queries non-existent IDs repeatedly, every single request bypasses your cache and hits your database.

The solution is negative caching: store an explicit null or empty marker in Redis with a short TTL (e.g., 30 to 60 seconds). Subsequent requests for the non-existent record will hit Redis and return immediately without touching the database.

Production Implementation Example

Here is a complete Python implementation bringing cache-aside, negative caching, and TTL jitter together:

import json
import random
import redis

r = redis.Redis(host='localhost', port=6379, db=0)

def get_user_profile(user_id: str):
    cache_key = f"user:{user_id}"

    # 1. Check Redis first
    cached_data = r.get(cache_key)
    if cached_data is not None:
        decoded = cached_data.decode('utf-8')
        if decoded == "__NULL__":
            return None  # Negative cache hit
        return json.loads(decoded)

    # 2. Database fallback on cache miss
    user_data = fetch_user_from_db(user_id)

    # 3. Handle non-existent records (Negative Caching)
    if user_data is None:
        r.setex(cache_key, 60, "__NULL__")
        return None

    # 4. Set key with TTL Jitter (base 3600s +/- 30s)
    base_ttl = 3600
    jitter = random.randint(-30, 30)
    ttl = base_ttl + jitter

    r.setex(cache_key, ttl, json.dumps(user_data))
    return user_data

def fetch_user_from_db(user_id: str):
    # Simulated database query
    pass
Enter fullscreen mode Exit fullscreen mode

4. Track Cache Hit Rate and Configure Eviction Policies

A cache is only as good as its hit rate. Monitor your cache metrics using Redis INFO stats.

If your cache hit rate drops below 90%, it usually indicates one of three issues:

  • Your TTLs are too short for your access patterns.
  • You are caching low-value, rarely read data.
  • Redis is running out of memory and evicting keys prematurely.

Additionally, configure an explicit maxmemory-policy in redis.conf. For standard web applications, allkeys-lru (Least Recently Used) or allkeys-lfu (Least Frequently Used) ensures Redis automatically drops obsolete data when memory limits are reached, rather than throwing write errors.

By enforcing strict TTLs, applying jitter, and caching negative responses, backend architectures can reduce database read queries by 80% or more, taking response latencies down from milliseconds to microseconds.

About Me

Tanmay builds AI agents and automation for businesses and is open to freelance projects. Feel free to connect with me on LinkedIn.

Top comments (0)