Redis Data Types Overview
Redis is not a dumb string cache.
Introduction
Redis is not a dumb string cache. It offers strings, lists, sets, sorted sets, hashes, streams, bitmaps, HyperLogLog, and geospatial indexes — each optimized for specific access patterns.
Picking the wrong type wastes memory and forces O(N) commands where O(1) was available. A JSON string for a session object prevents partial field updates; a set when you need ranking misses ZREVRANGE.
This overview maps business problems to Redis types so you choose correctly before writing code.
Understanding the topic
Key concepts
- String — binary-safe blob, counters (INCR), cache entries.
- Hash — field map for objects (session, profile).
- List — ordered sequence, queue (LPUSH/BRPOP).
- Set — unique unordered members, tags, graph edges.
- Sorted set — member + score, leaderboards, delayed queues.
- Stream — append-only log with consumer groups (Kafka-lite).
- Bitmap / HLL / GEO — specialized analytics and location.
flowchart LRCache[String] --> APISession[Hash] --> AuthQueue[List] --> WorkerTags[Set] --> FilterRank[ZSet] --> BoardEvents[Stream] --> Consumer
Step-by-step explanation
- Every key has exactly one type; TYPE command returns it.
- Wrong-type commands error (WRONGTYPE Operation against a key).
- Redis converts encodings internally as collections grow/shrink.
- OBJECT ENCODING reveals internal representation.
- MEMORY USAGE estimates bytes for capacity planning.
Syntax reference
Common commands
- TYPE before operating on unknown keys.
- MEMORY USAGE — plan shard sizing.
- WRONGTYPE means you picked the wrong API for that key.
TYPE mykeyOBJECT ENCODING mykeyMEMORY USAGE mykey# Quick reference# String: SET GET INCR# Hash: HSET HGET# List: LPUSH BRPOP# Set: SADD SISMEMBER# ZSet: ZADD ZRANGE# Stream: XADD XREADGROUP
Informative example
Same user modeled three ways — only hash fits partial updates efficiently:
# String blob — rewrite entire JSON on one field changeSET user:1 '{"name":"Ada","plan":"pro"}'# Hash — update one fieldHSET user:1 plan enterprise lastLogin "2026-07-01"# Wrong: Set loses field semanticsSADD user:1 name:Ada plan:pro
Use hash when you read/update individual fields. String JSON is OK for read-mostly cache blobs.
Real-world use
Real-world use cases
- String — API response cache, feature flag value.
- Hash — shopping cart line attributes, user session.
- List — lightweight job queue between services.
- Set — unique visitors per article, mutual followers.
- Sorted set — game leaderboard, priority task queue.
- Stream — order events, audit trail with consumer groups.
Best practices
- Match type to read/write pattern, not ORM habit.
- Run TYPE in runbooks when debugging legacy keys.
- Prefer hash over JSON string for mutable objects.
- Use streams over pub/sub when you need retention.
- Document type conventions in team key naming guide.
- Sample MEMORY USAGE on representative keys before launch.
Common mistakes
- Everything stored as JSON strings.
- Using list as set (duplicates allowed).
- Sorted set when set suffices (extra memory for scores).
- Pub/sub when streams needed for replay.
Advanced interview questions
Q1BeginnerName five Redis data types.
Q2BeginnerWhen use hash vs string?
Q3IntermediateHow check a key's type?
Q4IntermediateList vs stream for messaging?
Q5AdvancedDesign data model for e-commerce cart?
Summary
Redis types are purpose-built — not interchangeable.