Quick revision
The short answer
A key-value store gives us get, put and delete by key, usually with a time to live and atomic operations such as increment and compare-and-set. It is fast because a lookup goes straight to the key's location through a hash or partition key, with no query planning and no joins, and Redis keeps everything in memory while DynamoDB spreads keys over partitions. Good uses are session storage and caching computed results, and also rate limit counters, feature flags and idempotency keys; bad uses are ad-hoc queries on non-key fields and reporting, and data that needs relational constraints or joins.
What to remember
In this answer, we saw that a key-value store offers get, put and delete by key, with TTLs and atomic operations such as increment and compare-and-set. It is fast because the key leads straight to the value, with no query planning, in memory for Redis and on one partition for DynamoDB. Sessions, cached results, counters, flags and idempotency keys fit it well; ad-hoc queries, reporting and data that needs constraints or joins do not, and Redis data structures extend the model without changing its rule that we must know the key.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
A key-value store gives us get, put and delete by key, usually with a time to live and atomic operations such as increment and compare-and-set. It is fast because a lookup goes straight to the key's location through a hash or partition key, with no query planning and no joins, and Redis keeps everything in memory while DynamoDB spreads keys over partitions. Good uses are session storage and caching computed results, and also rate limit counters, feature flags and idempotency keys; bad uses are ad-hoc queries on non-key fields and reporting, and data that needs relational constraints or joins.
Why this question is asked
Key-value stores are the simplest kind of database, and that simplicity is their strength. Nearly every Java service at scale uses one, often Redis for caching and sessions, and many use DynamoDB as a primary store. Interviewers ask about them to see whether we understand what we give up for the speed, because using a key-value store for the wrong job, such as reporting, is a common and costly mistake. In this answer, we will recap the operations a key-value store offers, see why they are fast, walk through good and bad use cases with examples, and look at how Redis data structures stretch the model.
1A Quick Recap of the Key-Value Model
A key-value store is like a giant HashMap that lives outside our application. Each entry has a key, a string we choose such as session:8f3a or user:42:profile, and a value, which the store treats as opaque bytes or a simple structure. The core operations are put a value under a key, get the value for a key, and delete a key. That is the whole contract: the store does not know what is inside the value, so it cannot search by it.
On top of the core, most stores add three features that make them useful in practice. A TTL, time to live, makes a key expire automatically after a given time. Atomic operations such as increment change a value in one step, so two clients incrementing at once never lose an update. And compare-and-set, or conditional writes, change a value only if it still has the value or version we expect.
Notice that every operation names the key. There is no 'find all sessions for user 42' here, unless we design a key for it.
2Why These Lookups Are Fast
The speed comes from what the store does not have to do. A SQL query is parsed, planned and optimised, may use several indexes, and may join tables. A key-value get skips all of that: the key is hashed, the hash points to where the value lives, and the value is returned. In Redis that is an in-memory hash table lookup, typically well under a millisecond inside the server, so most of the time a client sees is the network round trip. Redis also runs commands one at a time on a single main thread, which avoids locking and makes each command atomic.
DynamoDB works the same way at a larger scale. The partition key is hashed to choose a partition, a slice of the table on its own storage nodes, and the item is read from there, with single-digit millisecond latency at almost any table size. Because every request goes to one partition, adding partitions adds capacity, which is why these stores scale horizontally so easily.
6 messages in order. Play them, or step through them one at a time.
The key-value store skips planning because the key says where the value is.
3Good Use Cases
Session storage is the classic example. A session is always read by its id, it is small, and it should disappear after a period of inactivity, which a TTL handles for us. Spring Session can store HTTP sessions in Redis with one dependency, so any instance of the service can serve any request.
Caching computed results is the second. A product page that takes 80 milliseconds to assemble from several services can be stored under product:123:page for five minutes and served in a millisecond. Similar patterns work for rate limit counters, where an atomic increment on a key per user per minute with a TTL of 60 seconds counts requests, for feature flags read on every request, and for idempotency keys, where setIfAbsent records that payment request pay-7781 was processed, so a retry is recognised and not charged twice.
4Bad Use Cases
Ad-hoc queries by fields other than the key are the first bad fit. If product managers ask for all sessions created from mobile devices in France, a key-value store can only answer by scanning every key and decoding every value, which in Redis blocks other commands if done carelessly and in DynamoDB reads and pays for the whole table. Reporting and analytics belong in a relational database or a warehouse.
Data that needs relational constraints or joins is the second. Orders that must reference existing customers, balances that must never go negative across several accounts, or invoices that must be unique per order rely on constraints and transactions across entities. A key-value store can enforce a condition on one key, but not a rule that spans many.
| Use | Fit | Why |
|---|---|---|
| Sessions | Good | read by id, small, expires |
| Cached results | Good | read by key, rebuildable |
| Rate limit counters | Good | atomic increment with TTL |
| Idempotency keys | Good | set if absent |
| Ad-hoc search by fields | Bad | needs a full scan |
| Reports and joins | Bad | no query planner, no joins |
5Redis Data Structures Blur the Line
Redis is more than strings. Its values can be hashes, which hold fields such as name and email under one key; lists; sets; sorted sets, which keep members ordered by a score and make leaderboards and time-ordered queues easy; and streams, append-only logs with consumer groups. With these, Redis can answer questions like 'top 10 players' with ZREVRANGE, which goes beyond pure key-value. DynamoDB similarly adds sort keys and secondary indexes. So the label key-value describes the core access pattern, and each product adds its own extras; we still design around knowing the key before we read.
6Follow-up questions
Is Redis durable enough to be a primary database?
Redis can persist data with RDB snapshots and the append-only file, and with replicas it survives a node failure, but with default settings some recent writes can be lost on a crash, because persistence is asynchronous. For caches and sessions that is fine. For data we cannot lose, we either configure stronger persistence and accept the cost, or keep the source of truth in a durable database.
How do you implement compare-and-set in Redis?
With WATCH on the key, then MULTI and EXEC: if another client changes the watched key before EXEC, the transaction is aborted and we retry. A Lua script that checks and sets in one step is another common way, since Redis runs a script atomically.
Why should cache keys have a TTL even if we invalidate them?
Invalidation can fail or be missed, for example when a message is lost, so a TTL puts an upper bound on how long stale data can live. It also stops the cache filling up with entries nobody reads anymore.
7Conclusion
In this answer, we saw that a key-value store offers get, put and delete by key, with TTLs and atomic operations such as increment and compare-and-set. It is fast because the key leads straight to the value, with no query planning, in memory for Redis and on one partition for DynamoDB. Sessions, cached results, counters, flags and idempotency keys fit it well; ad-hoc queries, reporting and data that needs constraints or joins do not, and Redis data structures extend the model without changing its rule that we must know the key.
Open with
A key-value store gives you a small set of operations: put a value under a key, get it, delete it, usually with a TTL so keys expire, and atomic operations like increment and compare-and-set, such as Redis SET with NX or DynamoDB conditional writes.
Then, if they want more
It is fast because there is no query to plan: the key is hashed and points directly to the value. Redis does that in memory on a single thread, so commands take microseconds and are atomic, and DynamoDB hashes the partition key to one partition, which keeps latency low at any size and lets it scale out.
Two good uses are session storage, since a session is read by id and expires with a TTL, and caching computed results such as an assembled product page; counters for rate limiting and idempotency keys are also good.
Two bad uses are ad-hoc queries on fields that are not the key, which need a full scan, and anything that needs relational constraints, reports or joins, such as orders that must reference customers. Redis blurs the line a bit with hashes, sorted sets and streams, but I would still design around knowing the key before every read.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Redis documentation, MongoDB manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What operations does a key-value store like Redis or DynamoDB (used as key-value) give...": https://scrivio-interview-library.netlify.app/databases/nosql-fundamentals/a9aaf2bd29/