TExcelTAD
Home › Redis › Redis Data Modeling

Redis Data Modeling

In a relational database you design tables first and write queries later. In Redis it is the other way round: you start from the questions your application asks and choose keys and data types that answer each question with one or two cheap commands. There are no joins and no secondary indexes unless you build them yourself.

Step 1: list the access patterns

Imagine a small blog. The application needs to:

  1. Show a post (title, author, body, view count).
  2. Show the 10 newest posts.
  3. Show the posts of one author.
  4. Show the posts with a tag.
  5. Count views.

Step 2: map each pattern to a structure

Question Key Type
a post's fields post:{id} hash
newest posts posts:by-date sorted set, score = timestamp
posts by author author:{name}:posts sorted set, score = timestamp
posts by tag tag:{name}:posts set
views field views in post:{id} HINCRBY
next post ID next:post:id string counter (INCR)
redis · try it yourself
INCR next:post:id
HSET post:1 title "Why Redis" author ada created 1760000000 views 0
ZADD posts:by-date 1760000000 post:1
ZADD author:ada:posts 1760000000 post:1
SADD tag:redis:posts post:1
SADD tag:intro:posts post:1
INCR next:post:id
HSET post:2 title "Sorted sets" author linus created 1760090000 views 0
ZADD posts:by-date 1760090000 post:2
ZADD author:linus:posts 1760090000 post:2
SADD tag:redis:posts post:2
ZRANGE posts:by-date 0 9 REV
SISMEMBER tag:intro:posts post:2
SCARD tag:redis:posts
HINCRBY post:1 views 1
HMGET post:1 title views
redis> INCR next:post:id
(integer) 1
redis> HSET post:1 title "Why Redis" author ada created 1760000000 views 0
(integer) 4
redis> ZADD posts:by-date 1760000000 post:1
(integer) 1
redis> ZADD author:ada:posts 1760000000 post:1
(integer) 1
redis> SADD tag:redis:posts post:1
(integer) 1
redis> SADD tag:intro:posts post:1
(integer) 1
redis> INCR next:post:id
(integer) 2
redis> HSET post:2 title "Sorted sets" author linus created 1760090000 views 0
(integer) 4
redis> ZADD posts:by-date 1760090000 post:2
(integer) 1
redis> ZADD author:linus:posts 1760090000 post:2
(integer) 1
redis> SADD tag:redis:posts post:2
(integer) 1
redis> ZRANGE posts:by-date 0 9 REV
1) "post:2"
2) "post:1"
redis> SISMEMBER tag:intro:posts post:2
(integer) 0
redis> SCARD tag:redis:posts
(integer) 2
redis> HINCRBY post:1 views 1
(integer) 1
redis> HMGET post:1 title views
1) "Why Redis"
2) "1"

Every question is now one command. The price: writing a post updates several keys. Use a transaction (MULTI/EXEC) or a Lua script so that a crash in the middle does not leave a post that is missing from an index.

Secondary indexes are just more keys

Redis only finds data by key. To find "users by email" you maintain a second key that points back to the primary one, and you update both together:

redis · try it yourself
MULTI
HSET user:17 name "Grace Hopper" email grace@navy.example
SET user:by-email:grace@navy.example 17
EXEC
GET user:by-email:grace@navy.example
HGET user:17 name
redis> MULTI
OK
redis> HSET user:17 name "Grace Hopper" email grace@navy.example
QUEUED
redis> SET user:by-email:grace@navy.example 17
QUEUED
redis> EXEC
1) (integer) 2
2) OK
redis> GET user:by-email:grace@navy.example
"17"
redis> HGET user:17 name
"Grace Hopper"

For ranges ("users aged 30 to 40") the index is a sorted set with the value as score:

redis · try it yourself
ZADD users:by-age 36 user:1 41 user:2 29 user:3 33 user:4
ZRANGE users:by-age 30 40 BYSCORE
redis> ZADD users:by-age 36 user:1 41 user:2 29 user:3 33 user:4
(integer) 4
redis> ZRANGE users:by-age 30 40 BYSCORE
1) "user:4"
2) "user:1"

One-to-many and many-to-many

Relationship Pattern
one-to-one fields in the same hash, or a key that holds the other ID
one-to-many (ordered) a list or sorted set of child IDs: order:9:items
one-to-many (unordered) a set of child IDs
many-to-many two sets, one per direction: user:1:groups and group:5:members
redis · try it yourself
SADD user:1:groups g:admins g:editors
SADD group:g:editors:members user:1 user:3
SADD user:3:groups g:editors
SADD group:g:admins:members user:1
SINTER user:1:groups user:3:groups
SMEMBERS group:g:admins:members
redis> SADD user:1:groups g:admins g:editors
(integer) 2
redis> SADD group:g:editors:members user:1 user:3
(integer) 2
redis> SADD user:3:groups g:editors
(integer) 1
redis> SADD group:g:admins:members user:1
(integer) 1
redis> SINTER user:1:groups user:3:groups
1) "g:editors"
redis> SMEMBERS group:g:admins:members
1) "user:1"

Denormalise on purpose

Reading is usually far more frequent than writing, so it is normal to copy data where it is read. For example, store the author's display name inside each post hash instead of looking it up in the user hash every time. When the name changes (rarely), update the copies in a background job.

Watch out for big keys

A single key with millions of elements is a "big key": deleting or reading it blocks the server, and in a cluster it cannot be split across nodes. Split by time or by ID range instead:

Instead of Use
events (one stream forever) events:2026-10 per month, trimmed with MINID
followers:celebrity (50 M members) followers:celebrity:{bucket} with bucket = user ID mod 100
all-sessions hash one key per session with its own TTL

Naming summary

  • Use type:id:attribute with colons; keep names readable.
  • Put the variable part where a SCAN MATCH pattern can use it: session:*, tag:redis:*.
  • In a cluster, put related keys under the same hash tag: {user:17}:profile, {user:17}:cart.

Test yourself

What is the first step when designing a Redis data model?

  1. Normalise the data into tables
  2. List the questions (access patterns) the application asks
  3. Choose the number of databases
  4. Create indexes with CREATE INDEX

How do you find a user by email in Redis?

  1. SCAN all user hashes
  2. Maintain an extra key such as user:by-email:{email} holding the user ID
  3. Use HGET with a wildcard
  4. It is impossible

Which structure works best as an index for 'posts between two dates'?

  1. A list
  2. A set
  3. A sorted set scored by timestamp
  4. A HyperLogLog