Skip to content

Redis - Data types

Redis is more than a simple key-value store; it’s a data structure server. Understanding its rich set of data types is key to building powerful and efficient applications. Modern Redis supports several advanced data structures beyond the original five.

The most basic Redis type. They are binary-safe and can hold any kind of data, from text to a serialized object or a JPEG file, up to 512MB.

Use Cases: Caching HTML fragments, storing session data, counting with INCR/DECR.

127.0.0.1:6379> SET cache:user:123 "...user profile data..." EX 3600
OK
127.0.0.1:6379> GET cache:user:123
"...user profile data..."

Maps between string fields and string values, ideal for representing objects. They are very memory-efficient for storing many objects.

Note: The HMSET command is deprecated since Redis 4.0.0. Use HSET with multiple field/value pairs instead.

Use Cases: Storing user profiles, product catalogs.

127.0.0.1:6379> HSET user:1001 username "alex" email "alex@example.com" points 1500
(integer) 3
127.0.0.1:6379> HGETALL user:1001
1) "username"
2) "alex"
3) "email"
4) "alex@example.com"
5) "points"
6) "1500"

A list of strings, sorted by insertion order. You can push/pop elements from either the head (left) or tail (right).

Use Cases: Implementing message queues, storing timelines or activity feeds.

127.0.0.1:6379> LPUSH tasks:queue 'process_video_1.mp4'
(integer) 1
127.0.0.1:6379> LPUSH tasks:queue 'generate_report_3.pdf'
(integer) 2
127.0.0.1:6379> RPOP tasks:queue
"process_video_1.mp4"

An unordered collection of unique strings. They are highly optimized for testing membership, finding unions, and intersections of different sets.

Use Cases: Storing tags for an article, tracking unique visitors, implementing access control lists.

127.0.0.1:6379> SADD article:1:tags 'nosql' 'database' 'redis'
(integer) 3
127.0.0.1:6379> SADD article:1:tags 'redis' # This will be ignored
(integer) 0
127.0.0.1:6379> SISMEMBER article:1:tags 'database'
(integer) 1

Similar to Sets, they are collections of unique strings. However, each member is associated with a floating-point score. Members are always sorted by this score.

Use Cases: Leaderboards, rate limiters, secondary indexing.

127.0.0.1:6379> ZADD leaderboard:2024 1550 'player1' 2100 'player2' 1800 'player3'
(integer) 3
# Get top 2 players
127.0.0.1:6379> ZREVRANGE leaderboard:2024 0 1 WITHSCORES
1) "player2"
2) "2100"
3) "player3"
4) "1800"

A powerful, append-only log data structure. It allows you to record events in chronological order and process them with consumer groups, similar to Apache Kafka.

Use Cases: Real-time messaging, event sourcing, sensor data collection.

# Add an event to a stream
127.0.0.1:6379> XADD sensor:data * temp 25.5 humidity 60
"1672531200000-0"
# Read all events from the beginning of the stream
127.0.0.1:6379> XRANGE sensor:data - +
1) 1) "1672531200000-0"
2) 1) "temp"
2) "25.5"
3) "humidity"
4) "60"

Stores longitude/latitude pairs and allows you to perform radius queries to find points within a certain distance of each other.

Use Cases: Finding nearby points of interest, location-based services.

127.0.0.1:6379> GEOADD locations 13.361389 52.521944 "Brandenburg Gate"
(integer) 1
# Find locations within a 5km radius of a point
127.0.0.1:6379> GEORADIUS locations 13.36 52.52 5 km
1) "Brandenburg Gate"

A probabilistic data structure used to estimate the number of unique items in a set (cardinality) using very little memory. The result is an approximation with a standard error of less than 1%.

Use Cases: Counting unique daily active users, counting unique search queries.

127.0.0.1:6379> PFADD unique:logins:2024-01-01 'user1' 'user2' 'user3'
(integer) 1
127.0.0.1:6379> PFADD unique:logins:2024-01-01 'user1' # Will not increase count
(integer) 0
127.0.0.1:6379> PFCOUNT unique:logins:2024-01-01
(integer) 3

Not a distinct data type, but a set of bit-level operations on the String type. Allows you to treat a string as a bitmap, setting and getting individual bits.

Use Cases: Tracking daily user activity (logged in or not), feature flags, real-time analytics.

# Mark user 1, 5, and 10 as active today (user ID is the bit offset)
127.0.0.1:6379> SETBIT daily:active:2024-01-01 1 1
(integer) 0
127.0.0.1:6379> SETBIT daily:active:2024-01-01 5 1
(integer) 0
127.0.0.1:6379> SETBIT daily:active:2024-01-01 10 1
(integer) 0
# Check if user 5 was active
127.0.0.1:6379> GETBIT daily:active:2024-01-01 5
(integer) 1
# Count total active users
127.0.0.1:6379> BITCOUNT daily:active:2024-01-01
(integer) 3