What is the difference between Redis as a cache and as a primary datastore?
Understand Redis as a cache versus a primary datastore: eviction policies, durability with AOF, replication and failover, and when each role fits.
Expected Interview Answer
As a cache, Redis holds a fast, disposable copy of data that can be rebuilt from a slower source of truth, often with TTLs and an eviction policy; as a primary datastore, Redis is the authoritative home of the data and must be configured for durability and high availability so nothing is lost.
In caching mode you accept that keys can be evicted under memory pressure (via maxmemory policies like allkeys-lru) or expire, because the canonical data lives in a database behind Redis. In primary-datastore mode Redis is the source of truth, so you enable strong persistence (AOF, often with RDB), replication, and failover with Redis Sentinel or Cluster, and you avoid eviction policies that discard live data. The functional API is the same; the difference is in durability guarantees, eviction configuration, and operational rigor.
- Cache: dramatically lowers latency and load on the backing store
- Cache: safe to evict or expire because data is rebuildable
- Primary: single fast source of truth with rich data structures
- Primary: durability via AOF plus replication and failover
- Clear choice of maxmemory policy based on the role
AI Mentor Explanation
A cache is like the scoreboard operator's quick notepad: handy for instant lookups but re-derivable from the official scorer's book if it is lost. Using Redis as a primary store is like making that notepad the official record itself, so it must be backed up and witnessed, because there is no other book to rebuild it from.
Step-by-Step Explanation
Step 1
Decide the source of truth
If a backing database owns the data, Redis is a cache; if Redis owns it, it is the primary store.
Step 2
Set the maxmemory policy
Caches use eviction like allkeys-lru; a primary store uses noeviction so live data is never dropped.
Step 3
Configure durability
A primary store enables AOF (often with RDB) so data survives restarts and crashes.
Step 4
Add high availability
Use Redis Sentinel or Cluster with replicas and automatic failover for a primary datastore.
Step 5
Handle cache invalidation
For caching, apply TTLs and an update strategy (write-through, write-behind, or cache-aside).
What Interviewer Expects
- Understanding of source-of-truth versus derived data
- Correct maxmemory and eviction choices per role
- Durability configuration for primary use (AOF, replication)
- Knowledge of caching strategies and invalidation
- Awareness of Sentinel or Cluster for high availability
Common Mistakes
- Running a primary datastore with an eviction policy that drops live keys
- Assuming a cache needs the same durability as a primary store
- Ignoring cache invalidation and serving stale data
- Using Redis as a primary store with no replication or failover
- Confusing persistence with high availability
Best Answer (HR Friendly)
“As a cache, Redis keeps a fast copy of data that can be rebuilt from a main database, so it is fine if some of it is dropped to save memory. As a primary datastore, Redis is the main home of the data, so it must be backed up and replicated because there is no other copy to fall back on.”
Code Example
# Cache role: cap memory and evict least-recently-used keys
maxmemory 2gb
maxmemory-policy allkeys-lru
appendonly no
# Primary datastore role: never evict, durable, replicated
maxmemory-policy noeviction
appendonly yes
appendfsync everysec
# plus replicaof / Sentinel / Cluster for failoverFollow-up Questions
- Which maxmemory-policy is safe for a primary datastore and why?
- Explain cache-aside versus write-through caching.
- How do Redis Sentinel and Cluster provide high availability?
- How do you prevent serving stale cached data?
- When would you choose Redis over a traditional database as the source of truth?
MCQ Practice
1. Which maxmemory-policy is appropriate when Redis is the primary datastore?
noeviction refuses writes rather than dropping live keys, protecting authoritative data that has no other source to rebuild it.
2. Why is eviction acceptable when Redis is used as a cache?
A cache holds derived data that a backing database can regenerate, so evicting or expiring keys is safe.
3. Which provides automatic failover for a Redis primary datastore?
Redis Sentinel and Cluster monitor nodes and promote a replica automatically, giving high availability for authoritative data.
Flash Cards
Cache role: source of truth? — A backing database owns the data; Redis holds a rebuildable copy.
Primary role: source of truth? — Redis itself owns the data, so it needs durability and replication.
Eviction policy for a primary store? — noeviction — never drop live data.
Eviction policy for a cache? — Commonly allkeys-lru to drop least-recently-used keys under pressure.
High availability for a primary store? — Redis Sentinel or Cluster with replicas and automatic failover.
Continue Learning
Related Interview Questions
What is the difference between Redis persistence with RDB and AOF?
medium
How does Redis handle key expiration and TTL?
medium
What happens between starting a Redis process and it serving traffic, and how do you make restarts safe?
hard
What is Redis and what makes it different from a traditional relational database?
easy