Redis data structures
Структуры данных Redis
Redis can store more than a plain value under a key: it can hold a list, a set, a ranked collection, or other structured data. Each type gives you commands for working with the data without fetching and rewriting the whole value.
What a Redis data type gives you
Think of a Redis key as a name, such as cart:42. The value under that name has a type, and that type determines what Redis can do with it. If you cache a JSON response as a String, GET returns the whole response. If you model a cart as a Hash with one field per product, HINCRBY can increase a product's quantity on the server without a separate read and write.
Choose a type based on what you need to do
- String stores bytes, including text or a serialized payload. Use it for cached responses or counters:
INCRincrements a value that represents an integer. - Hash stores field–value pairs under one key. Use it for records whose fields you need to read or update independently, such as user preferences.
HGETreads one field;HSETwrites it. - List keeps elements in order and allows duplicates. Push with
RPUSHand pop withLPOPfor a simple first-in, first-out queue. - Set keeps distinct members without a defined order. It's a good fit for membership checks with
SISMEMBERor for finding shared members withSINTER. - Sorted Set associates each unique member with a numeric score. Redis keeps members ordered by score, so it's a natural fit for leaderboards and other ranked collections.
- Stream is an append-only log with ordered entry IDs. Readers can pick up from an earlier ID, and consumer groups can distribute entries among workers and track acknowledgments.
Redis also supports specialized operations. Bitmaps let you use individual bits as flags, and HyperLogLog estimates the number of distinct items without keeping a list of those items.
Example: a job scheduler
A sorted set can rank things by time as well as by score. Use the job ID as the member and its scheduled Unix timestamp as the score:
ZADD jobs:scheduled 1767225600 job:17 1767225660 job:18
ZRANGE jobs:scheduled -inf 1767225630 BYSCORE LIMIT 0 10
The range query returns up to ten jobs due by the cutoff, in ascending score order. Here it returns only job:17. This query reads jobs; it doesn't claim them. If several workers run it at once, they can see the same job, so a real scheduler needs a separate claim mechanism.
Common mistakes and interview questions
Don't assume every operation is fast just because Redis keeps data in memory. HGET reads one field, whereas HGETALL reads every field. Consider both the size of the collection and how much data a command returns. Also, a command that expects a Hash won't interpret a String containing JSON as one: it returns WRONGTYPE.
A common interview question is when to replace a List-based queue with a Stream. With a plain LPOP, the item is gone as soon as a worker reads it. With a Stream consumer group, unacknowledged entries can be claimed by another worker. That helps recover from crashes, but it doesn't guarantee exactly-once processing. Explain what happens if a worker finishes the job and crashes before acknowledging it.