Redis Data Types and Commands
Learning Objectives: By the end of this lesson, you will be able to:
- Master all Redis data types: Strings, Hashes, Lists, Sets, Sorted Sets, and specialized types
- Choose the optimal data type for different use cases and requirements
- Execute advanced commands for each data type with proper syntax and options
- Implement complex operations using Redis data structures effectively
- Understand time complexity and performance characteristics of different operations
1. Welcome and Introduction (5 minutes)
Icebreaker Activity
Think about your favorite social media platform. How do you think they store your profile information, friends list, timeline posts, and trending topics? Each of these requires different data organization strategies. Redis provides exactly the right data structures for each of these scenarios!
Lesson Overview
- Duration: 90 minutes
- Prerequisites: Completion of Lesson 1 (Redis Fundamentals) and Redis CLI familiarity
- Materials Needed: Running Redis instance, Redis CLI access, text editor for taking notes
2. Learning Objectives and Session Outline (5 minutes)
What We’ll Cover Today:
- Strings - The foundation data type with advanced operations
- Hashes - Object-like structures for complex data
- Lists - Ordered collections for queues and timelines
- Sets - Unique collections for tags and relationships
- Sorted Sets - Ranked data for leaderboards and scoring
- Specialized Types - Bitmaps, HyperLogLog, Geospatial, and Streams
- Advanced Commands - Transactions, expiration, and pattern matching
Success Criteria:
By the end of this session, you should confidently choose and implement the appropriate Redis data type for any given use case, and execute complex operations efficiently.
3. Foundation Knowledge (15-20 minutes)
Core Concepts
Redis Data Type Overview
Redis is often called a “data structure server” because it supports multiple data types, each optimized for specific use cases. Understanding when and how to use each type is crucial for building efficient applications.
Real-World Analogy: Think of Redis data types like different containers in your kitchen:
- Strings are like labels on jars - simple key-value storage
- Hashes are like recipe cards with multiple ingredients - structured objects
- Lists are like a shopping list - ordered items you can add to either end
- Sets are like a spice rack - unique items with no duplicates
- Sorted Sets are like a leaderboard - ranked items with scores
Why This Matters: Choosing the right data type affects performance, memory usage, and the complexity of your application code. A well-designed Redis schema can make operations 10x faster and use 50% less memory.
Key Terminology
- Atomic Operations: Commands that execute completely or not at all
- Time Complexity: Big O notation describing operation performance (O(1), O(log N), O(N))
- Lexicographical Order: Alphabetical sorting based on string comparison
- Cardinality: The number of elements in a set
- Score: Numeric value used for ranking in sorted sets
- Field: Individual attributes within a hash
4. Detailed Explanation and Examples (25-30 minutes)
1. Strings - The Universal Data Type
Strings are the most basic Redis data type, but they’re incredibly versatile. They can store text, numbers, serialized objects, or even binary data up to 512MB.
Basic String Operations
# Simple string operations
SET username "john_doe"
GET username
STRLEN username
# Numeric operations
SET counter 10
INCR counter
INCRBY counter 5
DECR counter
DECRBY counter 3
# String manipulation
SET message "Hello"
APPEND message " World"
GETRANGE message 0 4
SETRANGE message 6 "Redis"
Advanced String Features
# Conditional setting
SET config:feature "enabled" NX # Only if key doesn't exist
SET config:feature "disabled" XX # Only if key exists
# Atomic get and set
GETSET counter 0 # Returns old value and sets new value
# Multiple operations
MSET user:1:name "Alice" user:1:age "30" user:1:city "NYC"
MGET user:1:name user:1:age user:1:city
# Bit operations
SET flags 0
SETBIT flags 0 1
SETBIT flags 2 1
GETBIT flags 0
BITCOUNT flags
Use Cases: Caching, configuration values, counters, feature flags, simple locks
2. Hashes - Structured Objects
Hashes map field-value pairs, similar to objects in programming languages or rows in a database table.
Hash Operations
# Creating and accessing hashes
HSET user:1000 name "John Doe" email "john@example.com" age 30
HGET user:1000 name
HGETALL user:1000
HMGET user:1000 name email
# Hash manipulation
HINCRBY user:1000 age 1
HLEN user:1000
HEXISTS user:1000 phone
HDEL user:1000 age
# Scanning large hashes
HSCAN user:1000 0 MATCH "*email*"
Use Cases: User profiles, product information, configuration objects, session data
3. Lists - Ordered Collections
Lists are ordered collections of strings, perfect for implementing queues, stacks, or activity feeds.
List Operations
# Adding elements
LPUSH notifications "New message from Alice"
RPUSH notifications "Friend request from Bob"
LPUSH notifications "System maintenance at 3AM"
# Accessing elements
LRANGE notifications 0 -1 # All elements
LINDEX notifications 0 # First element
LLEN notifications # List length
# Removing elements
LPOP notifications # Remove from left (FIFO queue)
RPOP notifications # Remove from right (LIFO stack)
LREM notifications 1 "System maintenance at 3AM" # Remove specific value
# List modification
LSET notifications 0 "Updated notification"
LTRIM notifications 0 9 # Keep only first 10 elements
Advanced List Features
# Blocking operations (useful for job queues)
BLPOP task_queue 10 # Block for 10 seconds waiting for element
# List as a circular buffer
LPUSH recent_searches "redis tutorial"
LTRIM recent_searches 0 4 # Keep only 5 most recent searches
# Moving elements between lists
RPOPLPUSH source_list dest_list
Use Cases: Activity feeds, job queues, recently viewed items, message queues
4. Sets - Unique Collections
Sets are unordered collections of unique strings, excellent for representing relationships and performing set operations.
Set Operations
# Basic set operations
SADD tags:article:1 "redis" "database" "tutorial"
SADD tags:article:2 "redis" "performance" "scaling"
SMEMBERS tags:article:1
SCARD tags:article:1 # Count members
# Set membership
SISMEMBER tags:article:1 "redis"
SRANDMEMBER tags:article:1 # Random member
# Set operations
SINTER tags:article:1 tags:article:2 # Intersection
SUNION tags:article:1 tags:article:2 # Union
SDIFF tags:article:1 tags:article:2 # Difference
# Moving elements
SMOVE tags:article:1 tags:article:2 "tutorial"
SPOP tags:article:1 # Remove random element
Use Cases: Tags, categories, followers/following relationships, unique visitors
5. Sorted Sets - Ranked Collections
Sorted sets combine sets and lists, maintaining unique elements in sorted order by score.
Sorted Set Operations
# Adding scored elements
ZADD leaderboard 1500 "Alice" 1200 "Bob" 1800 "Charlie"
ZADD game:scores 95.5 "player1" 87.2 "player2" 92.1 "player3"
# Accessing by rank
ZRANGE leaderboard 0 -1 WITHSCORES # All elements with scores
ZREVRANGE leaderboard 0 2 WITHSCORES # Top 3 players
ZRANK leaderboard "Alice" # Alice's rank (0-based)
# Accessing by score
ZRANGEBYSCORE game:scores 90 100 # Players with scores 90-100
ZCOUNT game:scores 85 95 # Count players in score range
# Score operations
ZINCRBY leaderboard 100 "Alice" # Increase Alice's score
ZSCORE leaderboard "Alice" # Get Alice's score
# Set operations on sorted sets
ZUNIONSTORE combined_scores 2 leaderboard game:scores
ZINTERSTORE common_players 2 leaderboard game:scores
Use Cases: Leaderboards, priority queues, time-series data, ranking systems
5. Live Coding Demonstration (20-25 minutes)
Example 1: Building a Social Media Profile System
# User profile with hash
HSET user:alice name "Alice Johnson" email "alice@example.com" age 28
HSET user:alice bio "Software Developer" location "San Francisco"
HSET user:alice created_at "2024-01-01T10:00:00Z" posts_count 42
# User's followers and following (sets for uniqueness)
SADD followers:alice "bob" "charlie" "diana" "eve"
SADD following:alice "bob" "diana" "frank" "grace"
# Find mutual connections
SINTER followers:alice following:alice
# User's recent posts (list for chronological order)
LPUSH posts:alice "Just learned Redis! Amazing data structures 🚀"
LPUSH posts:alice "Working on a new project with Go and Redis"
LPUSH posts:alice "Beautiful sunset today 🌅"
# Get recent posts
LRANGE posts:alice 0 4
# Post engagement scores (sorted set)
ZADD engagement:alice 156 "post:1" 89 "post:2" 234 "post:3"
ZREVRANGE engagement:alice 0 -1 WITHSCORES
Explanation:
- Hash for structured profile data
- Sets for relationships (no duplicate followers)
- List for chronological posts
- Sorted set for ranked content by engagement
Example 2: E-commerce Product Catalog
# Product information
HSET product:laptop:1 name "Gaming Laptop" price 1299.99
HSET product:laptop:1 brand "TechBrand" category "Electronics"
HSET product:laptop:1 stock 15 rating 4.7 description "High-performance gaming laptop"
# Product categories and tags
SADD category:electronics "laptop:1" "phone:1" "tablet:1"
SADD tags:laptop:1 "gaming" "high-performance" "portable" "rgb-lighting"
# Price range index (sorted set by price)
ZADD products:by:price 1299.99 "laptop:1" 899.99 "phone:1" 599.99 "tablet:1"
# Popular products (sorted by sales)
ZADD products:popular 450 "laptop:1" 1200 "phone:1" 300 "tablet:1"
# Recent customer reviews
LPUSH reviews:laptop:1 "Amazing performance!" "Great for gaming" "Fast delivery"
# Search products by price range
ZRANGEBYSCORE products:by:price 1000 1500 WITHSCORES
# Get top 3 popular products
ZREVRANGE products:popular 0 2 WITHSCORES
Example 3: Real-time Analytics Dashboard
# Page view counters
INCR pageviews:home
INCR pageviews:products
INCR pageviews:about
# Unique visitors per day (using sets)
SADD visitors:2024-01-01 "user:123" "user:456" "user:789"
SCARD visitors:2024-01-01
# Top referrers (sorted set)
ZINCRBY referrers 1 "google.com"
ZINCRBY referrers 1 "twitter.com"
ZINCRBY referrers 3 "direct"
# Recent error logs (list with trimming)
LPUSH error_logs "404: /missing-page"
LPUSH error_logs "500: database connection failed"
LTRIM error_logs 0 99 # Keep only last 100 errors
# Performance metrics by endpoint
HSET metrics:api endpoint "/users" avg_response_time 45.2 total_requests 1500
HSET metrics:api error_rate 0.02 uptime "99.9%"
Debugging Demonstration
# Examine key types and memory usage
TYPE user:alice
MEMORY USAGE user:alice
# Check key expiration
TTL session:xyz789
PTTL session:xyz789 # In milliseconds
# Scan keys with patterns
SCAN 0 MATCH user:* COUNT 10
SSCAN followers:alice 0 MATCH "*bob*"
# Monitor operations in real-time
MONITOR # Be careful - shows all operations!
# Check slow operations
SLOWLOG GET 5
6. Hands-On Practice (30-40 minutes)
Exercise 1: Blog Application Data Modeling (15 minutes)
Objective: Design Redis data structures for a blog platform
Requirements:
- Store blog posts with title, content, author, and publish date
- Track post views and likes
- Manage post categories and tags
- Implement a comment system
- Create author profiles
Starter Template:
# TODO: Create a blog post
# TODO: Add categories and tags
# TODO: Track engagement metrics
# TODO: Add comments
# TODO: Create author profile
Solution:
# Blog post as hash
HSET post:1 title "Getting Started with Redis" content "Redis is amazing..."
HSET post:1 author "john_doe" published_at "2024-01-01T10:00:00Z"
HSET post:1 views 0 likes 0
# Categories and tags as sets
SADD categories:database "post:1" "post:2"
SADD tags:post:1 "redis" "tutorial" "beginner" "database"
# Engagement tracking
ZINCRBY posts:by:views 1 "post:1"
ZINCRBY posts:by:likes 1 "post:1"
# Comments as list (chronological)
LPUSH comments:post:1 "Great tutorial!" "Very helpful, thanks!"
# Author profile
HSET author:john_doe name "John Doe" bio "Redis enthusiast"
SADD author:john_doe:posts "post:1" "post:3" "post:5"
Exercise 2: Gaming Leaderboard System (15 minutes)
Objective: Implement a comprehensive gaming leaderboard
Requirements:
- Track player scores across different games
- Maintain global and weekly leaderboards
- Store player profiles and achievements
- Track game sessions and statistics
Challenge:
# Players: Alice(1500), Bob(1200), Charlie(1800), Diana(1600)
# Games: "space_shooter", "puzzle_master", "racing_game"
# Achievements: "first_win", "high_score", "speed_demon"
Solution:
# Global leaderboard
ZADD global:leaderboard 1500 "alice" 1200 "bob" 1800 "charlie" 1600 "diana"
# Game-specific leaderboards
ZADD game:space_shooter 850 "alice" 920 "charlie" 780 "diana"
ZADD game:puzzle_master 650 "alice" 480 "bob" 880 "charlie"
# Player profiles
HSET player:alice username "alice" level 15 total_games 45
SADD achievements:alice "first_win" "high_score"
# Weekly leaderboard (with expiration)
ZADD weekly:leaderboard 300 "alice" 250 "diana"
EXPIRE weekly:leaderboard 604800 # 7 days
# Game session tracking
LPUSH sessions:alice "space_shooter:850:2024-01-01T10:00:00Z"
LTRIM sessions:alice 0 9 # Keep last 10 sessions
# Top performers
ZREVRANGE global:leaderboard 0 2 WITHSCORES
Exercise 3: E-commerce Shopping Cart (10 minutes)
Objective: Build a shopping cart system with Redis
Requirements:
- Add/remove items from cart
- Track item quantities
- Calculate total price
- Handle cart expiration
- Store product information
Implementation:
# Shopping cart as hash (item_id -> quantity)
HSET cart:user123 "laptop:1" 1 "mouse:5" 2 "keyboard:3" 1
# Product prices for calculation
HSET products:prices "laptop:1" 1299.99 "mouse:5" 29.99 "keyboard:3" 89.99
# Cart operations
HINCRBY cart:user123 "mouse:5" 1 # Add one more mouse
HDEL cart:user123 "keyboard:3" # Remove keyboard
# Set cart expiration (24 hours)
EXPIRE cart:user123 86400
# Calculate total (would need application logic)
HMGET products:prices "laptop:1" "mouse:5"
HGETALL cart:user123
7. Common Pitfalls and Troubleshooting (10 minutes)
Frequent Mistakes
-
Problem: Using wrong data type for use case Example: Using a list for unique items instead of a set Solution: Understand the characteristics of each data type
-
Problem: Not considering time complexity Example: Using
KEYS *orLRANGE 0 -1on large datasets Solution: UseSCAN,SSCAN,HSCANfor large datasets; limit range operations -
Problem: Memory inefficiency with small hashes Example: Creating many small hashes instead of using strings Solution: Use Redis hash-max-ziplist settings or consider string serialization
-
Problem: Forgetting to set expiration on temporary data Example: Session data, cache entries without TTL Solution: Always set appropriate expiration times
Debugging Tips
# Check data type and encoding
TYPE mykey
OBJECT ENCODING mykey
# Monitor memory usage
MEMORY USAGE mykey
INFO memory
# Analyze slow operations
CONFIG SET slowlog-log-slower-than 10000 # 10ms threshold
SLOWLOG GET 10
# Check key distribution
INFO keyspace
SCAN 0 MATCH pattern:* COUNT 100
Performance Considerations
- O(1) operations: GET, SET, HGET, HSET, SADD, ZADD
- O(log N) operations: ZRANGE, ZRANK, sorted set operations
- O(N) operations: KEYS, SMEMBERS, HGETALL (avoid on large datasets)
- Memory optimization: Use appropriate data types, consider compression
8. Best Practices and Professional Tips (5 minutes)
Industry Standards
- Key Naming Convention: Use consistent, hierarchical naming (e.g.,
user:123:profile) - Data Type Selection: Choose based on access patterns, not just data structure
- Memory Management: Monitor memory usage and set appropriate limits
- Expiration Strategy: Set TTL on temporary data, use appropriate eviction policies
Advanced Patterns
# Implementing counters with expiration
SET counter:api:user123:minute 1 EX 60
INCR counter:api:user123:minute
# Using sets for time-based data
SADD active:users:$(date +%H) "user123"
EXPIRE active:users:$(date +%H) 3600
# Sorted sets for time series
ZADD temperatures $(date +%s) 22.5
ZREMRANGEBYSCORE temperatures 0 $(date -d '1 hour ago' +%s)
Next Steps in Your Learning Journey
- Learn Redis with programming languages (Go, Python, Node.js)
- Explore Redis modules (RedisJSON, RedisGraph, RedisTimeSeries)
- Study Redis persistence and replication
- Master Redis Lua scripting for complex operations
- Understand Redis clustering and sharding strategies
9. Summary and Review (5 minutes)
Key Takeaways
- Choose the right data type for your use case to optimize performance and memory usage
- Understand time complexity of operations to avoid performance bottlenecks
- Use appropriate commands for each data type to leverage Redis’s full potential
- Consider memory implications when designing your Redis schema
- Set expiration times on temporary data to prevent memory leaks
Data Type Selection Guide
| Use Case | Best Data Type | Alternative |
|---|---|---|
| Simple values, counters | String | - |
| Structured objects | Hash | Serialized String |
| Ordered items, queues | List | - |
| Unique items, tags | Set | - |
| Ranked data, leaderboards | Sorted Set | - |
| Time series | Sorted Set | Redis Streams |
| Caching | String/Hash | - |
Quick Review Questions
- Which data type would you use for a user’s followers list and why?
- What’s the time complexity of getting an element from a sorted set by rank?
- How would you implement a “recently viewed products” feature?
- What’s the difference between LPUSH and RPUSH?
- When would you choose a hash over a serialized string?
For Instructors
Teaching Tips
- Use visual diagrams to show how each data type organizes data
- Provide real-world examples for each data type to make concepts concrete
- Demonstrate the performance difference between optimal and suboptimal data type choices
- Encourage hands-on experimentation with different commands
- Show memory usage differences between data types for the same data
Common Student Questions
-
Q: “When should I use a hash vs. multiple string keys?” A: Use hashes for related data that you often access together; use strings for simple key-value relationships
-
Q: “Can I store JSON in Redis?” A: Yes, as strings, but consider using hashes for better memory efficiency and field-level operations
-
Q: “How do I handle large datasets with sets and sorted sets?” A: Use SCAN-based commands (SSCAN, ZSCAN) and consider Redis Cluster for horizontal scaling
Assessment Ideas
- Design exercises where students choose appropriate data types for given scenarios
- Performance comparison tasks showing the impact of data type selection
- Debugging exercises with intentionally suboptimal data type choices
- Real-world case studies requiring multiple data types working together
Adaptation Notes
- Beginners: Focus on the three main types (strings, hashes, lists) before introducing sets and sorted sets
- Advanced students: Dive deeper into memory optimization and advanced command options
- Time constraints: Prioritize strings, hashes, and lists; cover others in overview format