Redis Data Structures Overview

Redis is powerful because it supports five main data structures, each optimized for different use cases. Unlike a relational database where everything is a table, Redis lets you choose the right structure for your problem.

Key Concept: Redis is a key-value store, but values can be much more than simple strings. They can be lists, sets, hashes (dictionaries), sorted sets (scored data), and more. The structure you choose determines which commands you can use and how efficiently Redis can serve your queries.

In this lesson, you'll learn 46 essential commands across all five types using real ShopStream e-commerce scenarios: user sessions, product inventory, search queues, product tags, and seller leaderboards.

Strings — Simple Key-Value Storage

Strings are the most basic type. A string key stores a single value: text, numbers, or serialized data. Strings are ideal for user names, settings, counts, and flags.

SET and GET — Store and Retrieve

shell
(integer) 1
"Alice Johnson"

SET key value stores a value. GET key retrieves it. Returns nil if the key doesn't exist.

MSET and MGET — Multiple Keys

shell
OK
1) "alice@shopstream.com"
2) "Austin"
3) "28"

MSET sets multiple keys at once. MGET retrieves multiple values in order. More efficient than separate SET/GET calls.

INCR and INCRBY — Increment Counters

shell
(integer) 1
(integer) 6

INCR key increments by 1. INCRBY key amount increments by a specific amount. Perfect for view counts, sales metrics, and scores.

DECR and DECRBY — Decrement Counters

shell
(integer) 99
(integer) 96

DECR decrements by 1. DECRBY decrements by amount. Use for inventory management and countdown timers.

APPEND and STRLEN

shell
OK
(integer) 30
(integer) 30

APPEND key value adds to the end of a string. STRLEN key returns its length. Useful for comments and descriptions.

Hashes — Dictionary-Like Objects

A hash is like a dictionary or object. One key stores multiple field-value pairs. Perfect for user sessions, product metadata, and structured data.

HSET and HGET — Store and Retrieve Fields

shell
(integer) 3
"1"

HSET key field value [field value ...] stores one or more field-value pairs. HGET key field retrieves a single field.

HGETALL — Get All Fields

shell
1) "user_id"
2) "1"
3) "cart_count"
4) "3"
5) "last_viewed"
6) "product:789"

HGETALL key returns all field-value pairs as a flat list (alternating fields and values).

HKEYS and HVALS

shell
1) "user_id"
2) "cart_count"
3) "last_viewed"

1) "1"
2) "3"
3) "product:789"

HKEYS returns only field names. HVALS returns only values.

HEXISTS and HDEL

shell
(integer) 1
(integer) 0
(integer) 1

HEXISTS key field returns 1 if the field exists, 0 if not. HDEL key field deletes a field and returns how many were deleted.

HINCRBY

shell
(integer) 5

HINCRBY key field increment increments a numeric field. Great for counter updates within a hash.

Lists — Ordered Queues and Stacks

A list is an ordered collection of strings. Elements can be added to the head (left) or tail (right). Perfect for queues, stacks, and activity logs.

LPUSH and RPUSH — Add to Left or Right

shell
(integer) 3
(integer) 2

LPUSH adds elements to the left (head). RPUSH adds to the right (tail). Most recent searches go to the left.

LRANGE — Get Elements by Range

shell
1) "wireless headphones"
2) "usb cable"
3) "laptop stand"

LRANGE key start stop returns elements from index start to stop. 0 -1 means all elements (right-to-left order).

LPOP and RPOP — Remove from Left or Right

shell
"wireless headphones"
"order:002"

LPOP removes and returns the leftmost element. RPOP removes and returns the rightmost. Useful for processing queues.

LLEN — Get List Length

shell
(integer) 2

LLEN key returns the number of elements in the list.

LINDEX — Get Element by Position

shell
"usb cable"

LINDEX key index returns the element at a specific position. Index 0 is the head, -1 is the tail.

LTRIM — Keep Only a Range

shell
OK

LTRIM key start stop deletes all elements outside the range. Useful for keeping only the last N items.

Sets — Unique Collections (No Order)

A set is an unordered collection of unique strings. Perfect for tags, unique visitors, followers, and membership checks.

SADD and SMEMBERS — Add and Get Members

shell
(integer) 4
1) "sale"
2) "featured"
3) "electronics"
4) "new"

SADD key member [member ...] adds one or more members. Duplicates are ignored. SMEMBERS key returns all members (unordered).

SISMEMBER — Check Membership

shell
(integer) 1
(integer) 0

SISMEMBER key member returns 1 if the member is in the set, 0 if not. Very fast for membership checks.

SCARD — Count Members

shell
(integer) 4

SCARD key returns the number of unique members.

SREM — Remove Members

shell
(integer) 1

SREM key member [member ...] removes one or more members and returns how many were removed.

SINTER and SUNION — Set Operations

shell
(integer) 3
(integer) 2
1) "electronics"

1) "sports"
2) "travel"
3) "electronics"
4) "new"

SINTER key1 key2 returns members in both sets (intersection). SUNION key1 key2 returns all unique members from both sets (union).

Sorted Sets — Scored Elements (Ranking)

A sorted set is like a set, but each member has an associated score (numeric). Elements are ordered by score. Perfect for leaderboards, ratings, and time-series data.

ZADD and ZRANGE — Add and Retrieve by Rank

shell
(integer) 4
1) "seller:001"
2) "seller:004"
3) "seller:003"
4) "seller:002"

ZADD key score member [score member ...] adds members with scores. ZRANGE key start stop returns members ordered by score (lowest to highest).

ZREVRANGE — Reverse Order (Highest First)

shell
1) "seller:002"
2) "seller:003"
3) "seller:001"
4) "seller:004"

ZREVRANGE returns members in reverse order (highest score first). Perfect for leaderboards showing top sellers first.

ZSCORE — Get a Member's Score

shell
"2500"

ZSCORE key member returns the score for a specific member.

ZRANK and ZREVRANK — Get Position

shell
(integer) 3
(integer) 0

ZRANK returns position starting from 0 (lowest score). ZREVRANK returns position from top (highest score). seller:002 is 4th overall but 1st (index 0) in reverse rank.

ZINCRBY — Increase Score

shell
"2300"

ZINCRBY key increment member increases a member's score. Great for real-time score updates.

ZCARD and ZCOUNT

shell
(integer) 4
(integer) 2

ZCARD key returns total members. ZCOUNT key min max counts members with scores in a range.

ZREM — Remove Member

shell
(integer) 1

ZREM key member removes a member and returns 1 if successful.

Key Expiration — Auto-Cleanup

Redis can automatically delete keys after a specified time. Perfect for sessions, caches, and temporary data.

EXPIRE and TTL

shell
OK
(integer) 1
(integer) 3599

EXPIRE key seconds sets a key to expire after N seconds. TTL key returns seconds remaining. Returns -1 for no expiration, -2 for non-existent key.

PTTL and PEXPIRE

shell
(integer) 3450000

PTTL key returns TTL in milliseconds. PEXPIRE key milliseconds sets expiration in milliseconds for sub-second precision.

PERSIST — Remove Expiration

shell
(integer) 1
(integer) -1

PERSIST key removes the expiration time. Returns 1 if successful, 0 if key doesn't exist or has no expiration.

Key Management — Finding and Scanning Keys

KEYS — Find Keys by Pattern

shell
1) "user:1:name"
2) "user:2:name"

1) "product:123"
2) "product:456"
3) "product:789"

KEYS pattern returns all keys matching a glob pattern. Useful for debugging, but don't use on large production databases (it blocks Redis).

SCAN — Safely Iterate Large Keyspaces

shell
1) "0"
2) 1) "product:123"
2) "user:1:name"

1) "cursor"
2) 1) "user:1:name"

SCAN cursor MATCH pattern COUNT count iterates keys without blocking Redis. Start with cursor 0, and continue until cursor returns to 0.

Test Your Knowledge

Question 1 of 5
Which Redis data structure is best for a leaderboard of top sellers?
Question 2 of 5
What is the difference between LPUSH and RPUSH?
Question 3 of 5
How do you add an item to a set and ensure no duplicates?
Question 4 of 5
Explain the difference between HSET and HGET.
Question 5 of 5
How do you set a key to expire in 1 hour?
0
/5
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

You're mastering Redis. Go deeper with caching and Lua scripts.

Join the Best AI Program: The AI-Era Data Engineer — 5 months, fully online, with placement assistance. Week 1 is free.

Explore The AI-Era Data Engineer →