Catalog My learning Blog
Redis Tutorial: Caching, Persistence and Scaling · Phase 1 · Lesson 2 · 5 min read

Redis Data Types: Strings, Hashes and Lists

On this page
  1. How to choose a Redis data type
  2. Redis strings and atomic counters with INCR
  3. Redis hashes vs JSON strings: updating one field
  4. Redis lists: queues and capped feeds
  5. WRONGTYPE errors: each key holds one type
  6. Check yourself

How to choose a Redis data type

Every Redis key holds exactly one typed value, and the type decides which commands you can run on it. So "which type?" is really "which operations do I need to be atomic and cheap?". Most teams start by storing everything as JSON strings. That works until two requests update the same object at once.

Today covers the three types you'll use most. Day 3 covers sets, sorted sets and HyperLogLog.

TypeGood forCore commandsCost
StringSingle values, cache blobs, countersGET, SET, INCR, DECRO(1)
HashAn object whose fields are read or updated separatelyHGET, HSET, HINCRBY, HGETALLO(1) per field
ListQueues, capped "recent items" feedsLPUSH, RPUSH, LPOP, BLPOP, LTRIMO(1) at either end

Redis strings and atomic counters with INCR

A string holds up to 512 MB of any bytes: text, a number, serialized JSON, an image. INCR treats a string as an integer and adds one in a single step:

Redis
SET page:views 10
INCR page:views          # (integer) 11

Because commands run one at a time (Day 1), two clients calling INCR can't both read 10 and write 11. The same counter done in application code, read the value then write value + 1, loses updates under concurrent traffic.

Redis hashes vs JSON strings: updating one field

Here's the case that bites teams. A session stores a user id, an IP and a login count that goes up on every request. As a JSON string, each update is read, parse, change, serialize, write: two round trips, and if two requests overlap, one increment is lost.

As a hash, each field is addressable on its own:

Redis
HSET session:abc123 user_id 42 login_count 14 ip 10.0.0.7
HINCRBY session:abc123 login_count 1       # (integer) 15, one atomic step
HGET session:abc123 user_id                # "42", only the field you need

No read before the write, no parsing, no race. Small hashes are also stored compactly. Redis uses an encoding called listpack until a hash grows past a size threshold (Day 4), which makes hashes memory-efficient for objects with a handful of fields.

When is a JSON string still the better choice? When the object is always read and written as a whole and rarely changes, such as a cached API response. Then one GET is simpler than HGETALL plus reassembly.

Redis 8 also includes a native JSON type that stores a document and updates parts of it in place, for example JSON.NUMINCRBY session:abc123 $.login_count 1. It's the middle ground for nested objects that still need atomic field updates. Before Redis 8 it was a separate module, so check what your server or managed service supports.

Redis lists: queues and capped feeds

A list keeps insertion order and is cheap to push and pop at both ends. Two patterns cover most uses.

A capped feed. Show a user's five latest events: push to the front, then trim the rest.

Redis
LPUSH recent:user:42 "event-7"
LTRIM recent:user:42 0 4                   # keep only the 5 newest
LRANGE recent:user:42 0 -1                 # event-7 event-6 event-5 event-4 event-3

Run both commands on every new event and the list never grows past five, with no cleanup job.

A simple work queue. Producers RPUSH, workers pop from the other end. BLPOP blocks until an item arrives, so workers don't poll in a loop:

Redis
RPUSH jobs job-1 job-2
BLPOP jobs 5                               # waits up to 5 seconds, returns "jobs" "job-1"

The weakness: once a worker pops a job, it's gone from Redis. If the worker crashes mid-job, the job is lost. BLMOVE closes most of that gap. It pops the job and pushes it onto a per-worker processing list in one atomic step, so a crash leaves the job visible there instead of lost:

Redis
BLMOVE jobs processing:worker-1 LEFT RIGHT 5     # take the next job, keep a copy; waits up to 5 s
# ... do the work ...
LREM processing:worker-1 1 job-1                 # done: remove the copy

A recovery job moves anything left in a dead worker's processing list back onto jobs. That's the whole reliable-queue pattern, and you write and operate every piece of it yourself. If you need acknowledgments, retries and consumer groups built in, use Streams instead (Day 11).

A string holds a value or an atomic counter, a hash holds an object whose fields change independently, and a list holds an ordered queue or capped feed. Choosing the type is choosing the atomic operations you get.
A string holds a value or an atomic counter, a hash holds an object whose fields change independently, and a list holds an ordered queue or capped feed. Choosing the type is choosing the atomic operations you get.

WRONGTYPE errors: each key holds one type

Commands for the wrong type fail. INCR on a key holding JSON returns ERR value is not an integer or out of range, and HGET on a string key returns a WRONGTYPE error. Mixed types under similar key names are a common source of bugs after a refactor. Use a clear naming scheme (Day 4) so the type is obvious from the key.

Check yourself

Both servers read the same old value and each write back +1, so one update is lost. Store the session as a hash and use HINCRBY, which is a single atomic step.

LPUSH the new action, then LTRIM key 0 49. The trim runs on every insert, so the list can never exceed 50 items.

Move jobs to a processing list with LMOVE and clean up on success, or switch to Redis Streams with consumer groups, which track unacknowledged messages for you.

When you read or update individual fields, such as incrementing login_count or changing one setting. A hash lets you HINCRBY or HSET a single field atomically without rewriting the whole object. If you always read and write the whole object at once and need nested structure, a JSON string is simpler.

LRANGE is O(N) in the number of returned items, so fetching 2 million entries blocks Redis's single thread and sends a huge response over the network. Every other client waits meanwhile. Fetch only the page you need, cap the list's length with LTRIM, or use a different structure.