Stop 2 · Data structures in depth
Every structure a value can hold, taken one at a time: mental model + commands + uses + encoding + interview probes
First, a common misunderstanding: Redis does not only store strings
Many people think Redis is a store that only holds strings. More precisely: a key is always a string, and it is the value that has a type. Redis is called a "data structure server", not just a cache, because a value can be one of several structures, and each structure has its own set of commands. The tabs above take them one at a time. For each one: how to think about it, the commands you will actually use, real uses, the , and the questions an interviewer asks next.
The most basic type, and the one you will use most. One key holds one value. The name is misleading: the value can also be a number, and Redis has INCR and DECR that change it in place, so you do not have to read the value, add one, and write it back. A value can also be a whole JSON document or binary data. GET and SET are both O(1).
SET name "Wayne"store a valueGET nameread by keySET otp 123456 EX 30store with a 30s TTLINCR page:viewsatomic +1 (counter)SET lock v NX EX 10write only if the key is absent (claim / idempotency)MGET k1 k2 k3read several keys in one round trip
- cache one whole result, such as an aggregated JSON document
- counters: page views, rate-limit counts, failed attempts
- flags, feature toggles, one-time codes, session tokens
- distributed locks and idempotency claims (SET with NX and a TTL)
All three Redis uses in our system are Strings: the rate quote stored as a JSON string, the idempotency key, and the balance projection. One type covers a lot of ordinary work.
Three encodings: int (a value that is an integer is stored as an integer, which uses the least memory), embstr (strings of 44 bytes or fewer, allocated in one block together with the object header), raw (longer strings, allocated separately). Redis picks one for you.
Because INCR is . GET then SET is two steps: two clients can read the same old value, each add one, and write back, so one increment is lost. INCR does the read and the write inside a single command, and Redis executes commands one at a time, so no other client can slip in between.
The limit is 512MB. In practice keep values far below that. A command on a very large value holds the execution thread for as long as it runs, and every other request waits behind it (see the "big key" probe).