What is the difference between MongoDB and a key-value store like Redis?
Compare MongoDB and Redis: document database with rich queries vs in-memory key-value store for caching, and why they are often used together.
Expected Interview Answer
MongoDB is a document database that stores rich, queryable JSON-like documents on disk and supports secondary indexes, ad-hoc queries, and aggregations, whereas Redis is an in-memory key-value store optimized for extremely fast lookups by key and simple data structures, typically used as a cache or for real-time data.
MongoDB persists data durably and lets you query by any field, run range and aggregation queries, and model relationships, making it a primary system of record. Redis keeps data in RAM for microsecond access and exposes values through keys plus specialized structures (strings, hashes, lists, sets, sorted sets), but you generally cannot query arbitrary fields inside a value. In practice they are complementary: MongoDB as the durable store, Redis as the caching and low-latency layer in front of it.
- MongoDB gives rich secondary-index queries; Redis gives microsecond key lookups
- MongoDB is disk-durable by default; Redis is memory-first with optional persistence
- Redis excels at caching, sessions, rate limiting, and real-time counters
- MongoDB excels as a queryable system of record for complex documents
- They are often used together rather than as substitutes
AI Mentor Explanation
MongoDB is the full match scorecard: you can query it any way — every ball a left-hander faced in the death overs, run rates by bowler — because every detail is indexed and searchable. Redis is the giant boundary scoreboard that flashes the current total the instant a run is scored; it answers 'what's the score by key' in a blink but can't tell you the history behind the number.
Step-by-Step Explanation
Step 1
Classify the data models
MongoDB stores structured JSON-like documents; Redis stores values addressed by a key, using structures like strings, hashes, lists, sets, and sorted sets.
Step 2
Compare query capability
MongoDB supports secondary indexes, ad-hoc field queries, ranges, and aggregation; Redis primarily retrieves by key with structure-specific commands.
Step 3
Compare storage and durability
MongoDB persists to disk by default; Redis keeps data in RAM with optional persistence (RDB snapshots or AOF).
Step 4
Compare latency profile
Redis delivers microsecond in-memory reads; MongoDB delivers millisecond disk/index-backed reads with far richer querying.
Step 5
Decide the role
Use MongoDB as the durable system of record; use Redis as a cache, session store, queue, or real-time layer in front of it.
What Interviewer Expects
- Document store vs key-value store distinction
- MongoDB's secondary indexes and ad-hoc queries vs Redis key access
- In-memory (Redis) vs disk-durable (MongoDB) storage model
- Understanding latency vs query-richness trade-offs
- Recognizing they are complementary, not strictly competing
Common Mistakes
- Claiming Redis can run arbitrary field queries like MongoDB
- Saying Redis has no persistence at all (it has RDB/AOF)
- Treating them as interchangeable rather than complementary
- Ignoring that Redis is memory-bound while MongoDB scales on disk
- Assuming MongoDB cannot cache or be fast without Redis
Best Answer (HR Friendly)
“MongoDB is a database that stores detailed records you can search by any field, so it's great as your main store of data. Redis keeps data in memory and answers simple lookups by a key extremely fast, so it's usually used as a speed layer or cache in front of a database like MongoDB.”
Code Example
// Find active users in a region, sorted by signup date
await db.collection('users').createIndex({ region: 1, createdAt: -1 });
const users = await db.collection('users')
.find({ region: 'EU', status: 'active' })
.sort({ createdAt: -1 })
.limit(20)
.toArray();// Cache a computed value by key
await redis.set('user:42:profile', JSON.stringify(profile), 'EX', 300);
const cached = await redis.get('user:42:profile');
// Real-time counter and a sorted-set leaderboard
await redis.incr('signups:2026-07-21');
await redis.zadd('leaderboard', 950, 'player:42');
const top = await redis.zrevrange('leaderboard', 0, 9, 'WITHSCORES');Follow-up Questions
- When would you put Redis in front of MongoDB, and what do you cache?
- How does Redis persist data with RDB versus AOF?
- What are the trade-offs of storing sessions in Redis vs MongoDB?
- How would you keep a Redis cache consistent with MongoDB writes?
- Which Redis data structures map well to a real-time leaderboard?
MCQ Practice
1. Which statement best distinguishes MongoDB from Redis?
MongoDB stores documents you can query by any indexed field, while Redis is optimized for fast key-based access to in-memory values and structures.
2. What is the most common architectural relationship between them?
They are complementary: MongoDB is the durable system of record and Redis serves as a low-latency cache, session store, or real-time layer.
3. Which is true about Redis persistence?
Redis is memory-first but offers optional durability through RDB point-in-time snapshots and append-only file (AOF) logging.
Flash Cards
Data model: MongoDB vs Redis? — MongoDB stores queryable JSON-like documents; Redis stores values by key using strings, hashes, lists, sets, and sorted sets.
Query capability difference? — MongoDB supports secondary indexes and ad-hoc field queries; Redis mainly retrieves by key with structure-specific commands.
Storage model difference? — MongoDB is disk-durable by default; Redis is in-memory first with optional RDB/AOF persistence.
Typical roles? — MongoDB is the durable system of record; Redis is the cache, session store, queue, and real-time layer in front of it.
Are they competitors? — Usually complementary — used together — not direct substitutes.