Redis Medium technical 0 views 1 min read

What is the cache-aside pattern and how do you keep the cache consistent?

Peer-reviewed by HireXTech Technical Panel Updated for 2025/2026 hiring Editorial standards
Practise this track
Interviewer Expectations for this Question
01
Core Competency

Assesses fundamental understanding of Redis conventions, runtime behavior, and memory/performance considerations.

02
Evaluation Criteria

Hiring managers look for precision, avoidance of ambiguous jargon, and ability to explain trade-offs under real production conditions.

Comprehensive Model Answer Verified Solution

In cache-aside the application checks the cache first. On a miss it reads from the database, writes the value into the cache with a TTL, and returns it. On writes, the common approach is to update the database and then invalidate the cache key.

def get_user(uid):
    cached = redis.get(f"user:{uid}")
    if cached:
        return json.loads(cached)
    user = db.get_user(uid)
    redis.setex(f"user:{uid}", 300, json.dumps(user))
    return user

Consistency pitfalls: updating the cache before the database, or racing concurrent writers, can leave stale data. Delete the cache on write and let the next read repopulate it, using a short TTL as a safety net. For stronger needs use versioned keys or write-through. The first request after invalidation always pays the database cost.

Candidate Response Strategy & Interview Tips

  1. Start with a concise one-sentence summary: Deliver a direct, confident answer first before expanding into nuances.
  2. Demonstrate real-world trade-offs: Discuss where this approach excels and when you would avoid it in production systems.
  3. Discuss complexity & edge cases: Proactively explain time/space complexity or boundary conditions (null values, scale limits).
  4. Prepare for interviewer follow-ups: Technical hiring panels frequently probe deeper into concurrency, backward compatibility, or alternative libraries.
Related Topics & Skills
Spotted an error or have an alternative solution?