Redis - Data types
Redis - Modern Data Structures
Section titled “Redis - Modern Data Structures”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.
Core Data Types
Section titled “Core Data Types”1. Strings
Section titled “1. Strings”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 3600OK127.0.0.1:6379> GET cache:user:123"...user profile data..."2. Hashes
Section titled “2. Hashes”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) 3127.0.0.1:6379> HGETALL user:10011) "username"2) "alex"3) "email"4) "alex@example.com"5) "points"6) "1500"3. Lists
Section titled “3. Lists”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) 1127.0.0.1:6379> LPUSH tasks:queue 'generate_report_3.pdf'(integer) 2127.0.0.1:6379> RPOP tasks:queue"process_video_1.mp4"4. Sets
Section titled “4. Sets”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) 3127.0.0.1:6379> SADD article:1:tags 'redis' # This will be ignored(integer) 0127.0.0.1:6379> SISMEMBER article:1:tags 'database'(integer) 15. Sorted Sets (ZSETs)
Section titled “5. Sorted Sets (ZSETs)”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 players127.0.0.1:6379> ZREVRANGE leaderboard:2024 0 1 WITHSCORES1) "player2"2) "2100"3) "player3"4) "1800"Advanced Data Types
Section titled “Advanced Data Types”6. Streams
Section titled “6. Streams”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 stream127.0.0.1:6379> XADD sensor:data * temp 25.5 humidity 60"1672531200000-0"
# Read all events from the beginning of the stream127.0.0.1:6379> XRANGE sensor:data - +1) 1) "1672531200000-0" 2) 1) "temp" 2) "25.5" 3) "humidity" 4) "60"7. Geospatial Indexes
Section titled “7. Geospatial Indexes”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 point127.0.0.1:6379> GEORADIUS locations 13.36 52.52 5 km1) "Brandenburg Gate"8. HyperLogLogs
Section titled “8. HyperLogLogs”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) 1127.0.0.1:6379> PFADD unique:logins:2024-01-01 'user1' # Will not increase count(integer) 0127.0.0.1:6379> PFCOUNT unique:logins:2024-01-01(integer) 39. Bitmaps and Bitfields
Section titled “9. Bitmaps and Bitfields”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) 0127.0.0.1:6379> SETBIT daily:active:2024-01-01 5 1(integer) 0127.0.0.1:6379> SETBIT daily:active:2024-01-01 10 1(integer) 0
# Check if user 5 was active127.0.0.1:6379> GETBIT daily:active:2024-01-01 5(integer) 1
# Count total active users127.0.0.1:6379> BITCOUNT daily:active:2024-01-01(integer) 3