Quiz on AI Interviews Prep Live Training Corporate Training

Redis Tutorials.

A beginner-to-intermediate guide to Redis.

1. Introduction to Redis

Redis (REmote DIctionary Server) is an in-memory data structure store used as a database, cache, and message broker.

  • Extremely fast (in-memory)
  • Supports multiple data types
  • Persistence options available
  • Commonly used for caching and real-time apps

2. Installing Redis

# Ubuntu / Debian
sudo apt install redis-server

# macOS (Homebrew)
brew install redis

# Start Redis
redis-server

# Redis CLI
redis-cli

3. Redis Data Types

Data Type Description
String Text or binary data
List Ordered collection of strings
Set Unordered unique values
Hash Key-value pairs inside a key
Sorted Set Ordered by score
Stream Log-like data structure

4. Strings

SET username "alice"
GET username
INCR page_views
APPEND username "_123"

5. Lists

LPUSH tasks "task1"
LPUSH tasks "task2"
LRANGE tasks 0 -1
RPOP tasks

6. Sets

SADD tags "redis" "database" "cache"
SMEMBERS tags
SISMEMBER tags "redis"

7. Hashes

HSET user:1 name "Alice" age 25
HGET user:1 name
HGETALL user:1

8. Sorted Sets

ZADD leaderboard 100 "Alice"
ZADD leaderboard 200 "Bob"
ZRANGE leaderboard 0 -1 WITHSCORES

9. Key Expiration (TTL)

SET session:123 "active"
EXPIRE session:123 60
TTL session:123

10. Pub / Sub

# Subscriber
SUBSCRIBE news

# Publisher
PUBLISH news "Hello Redis!"

11. Transactions

MULTI
INCR balance
DECR balance
EXEC

12. Persistence

RDB (Snapshots)

SAVE
BGSAVE

AOF (Append Only File)

CONFIG SET appendonly yes

13. Common Redis Use Cases

  • Caching
  • Session storage
  • Rate limiting
  • Real-time analytics
  • Message queues

14. Best Practices

  • Use TTL to avoid memory leaks
  • Choose the right data type
  • Avoid large keys
  • Monitor memory usage
  • Use persistence wisely