Redis is an in-memory data structure store. Unlike traditional databases, it natively understands Strings, Hashes, Lists, Sets, Sorted Sets — and many operations are O(1). This makes it ideal for caching, session storage, leaderboards, and real-time analytics.
Step 1 — Setup and Connection
dockerrun-d--nameredis-labredis:7sleep2dockerexec-itredis-labredis-cli# Verify connectionPING# PONG# Clear all data (dev only!)FLUSHALL
📸 Verified Output:
PONG
OK
Step 2 — Strings: The Foundation
Strings are the simplest Redis type — a key maps to a byte sequence (binary-safe, up to 512MB).
📸 Verified Output:
💡 Redis strings are binary-safe — they can store JSON, images, serialized objects, anything. The limit is 512MB per value.
Step 3 — Hashes: Object Storage
Hashes map string fields to string values — perfect for storing objects without serializing the whole thing.
📸 Verified Output:
💡 Hashes are more memory-efficient than storing the same data as separate string keys. user:1:name + user:1:email + user:1:age costs 3 key overhead; HSET user:1 name email age costs 1.
Step 4 — Lists: Ordered Sequences
Lists are ordered sequences of strings — insertion order preserved. Support both stack (LIFO) and queue (FIFO) patterns.
📸 Verified Output (LRANGE after pushes):
💡 Lists are ideal for message queues (RPUSH + BLPOP) and activity feeds (LPUSH + LRANGE 0 N). For a task queue, have producers RPUSH and workers BLPOP.
Step 5 — Sets: Unique Collections
Sets are unordered collections of unique strings. O(1) add/remove/check, O(N) set operations.
📸 Verified Output:
Step 6 — Sorted Sets: Ranked Data
Sorted Sets are like Sets but each member has a score (float). Members are always sorted by score. Perfect for leaderboards, priority queues, time-series.
# TYPE: check what type a key is
TYPE user:1:name # string
TYPE product:1001 # hash
TYPE task:queue # list
TYPE online:users # set
TYPE leaderboard # zset
# EXISTS / DEL
EXISTS user:1:name # 1
DEL user:1:name
EXISTS user:1:name # 0
# EXPIRE: set TTL on any key
EXPIRE leaderboard 86400 # expire in 24 hours
TTL leaderboard # seconds remaining
PERSIST leaderboard # remove TTL
# RENAME
RENAME leaderboard game:leaderboard
# SCAN: iterate keys (safe for production, unlike KEYS)
SCAN 0 MATCH "user:*" COUNT 100
# Model a social media app in Redis
# User profile (Hash)
HSET user:1001 username alice name "Alice Smith" bio "Engineer" followers 1250 following 340
# Session (String with TTL)
SET session:tok_abc123 "user:1001" EX 3600
# User's feed (List — most recent first)
LPUSH feed:1001 "post:500" "post:499" "post:498"
LRANGE feed:1001 0 9 # latest 10 posts
# User's interests (Set)
SADD interests:1001 "technology" "python" "databases"
# Global trending tags (Sorted Set — score = mention count)
ZADD trending:tags 1523 "python" 987 "redis" 2341 "databases" 445 "docker"
ZRANGE trending:tags 0 4 WITHSCORES REV # top 5 trending
# User notifications (List)
RPUSH notifications:1001 '{"type":"like","from":"bob","post":"post:500"}'
RPUSH notifications:1001 '{"type":"follow","from":"carol"}'
# Mutual followers (Set intersection)
SADD followers:1001 "bob" "carol" "dave"
SADD followers:1002 "alice" "carol" "eve"
SINTER followers:1001 followers:1002 # mutual: carol
# Daily active users (Set — track by day)
SADD dau:2024-01-15 "user:1001" "user:1002" "user:1003"
SCARD dau:2024-01-15 # 3 DAU
# Verify all keys created
SCAN 0 COUNT 100