Introduction
Redis is a distributed in-memory NoSQL database supporting persistence. It offers rich data structures including strings, lists, sets, sorted sets, and hashes.
Key Features:
- Memory-based with optional disk persistence
- Distributed architecture support
- Multi-data structure support
- Pub/Sub messaging system
Official Documentation: http://redisdoc.com
Use Cases
- Session Storage: Persistent login sessions
- Rankings/Counters: Real-time leaderboards, article view counters
- Message Queues: Task queue systems (e.g., Celery integration)
- Real-time Metrics: Active user tracking
- Data Caching: Frequently accessed data (e.g., forum sections)
- Social Features: Friend relationships (used by Weibo)
- Pub/Sub Systems: Chat applications
Redis vs Memcached
| Feature | Memcached | Redis |
|---|
| Storage Type | Pure In-Memory | Memory + Disk Sync |
| Data Types | Fixed Value Types | Multiple Structures |
| Virtual Memory | ❌ Not Supported | ✔️ Supported |
| Data Persistence | ❌ No | ✔️ Dump File Backup |
| Disaster Recovery | ❌ No | ✔️ Memory Restoration |
| Pub/Sub | ❌ No | ✔️ Supported |
Installation Guides
Windows Systems
- Download from Microsoft Archive
- Run:
1
| redis-server.exe redis.windows.conf
|
- Connect:
Ubuntu Systems
- Install:
1
| sudo apt-get install redis-server
|
- Start/Stop:
1
2
| sudo service redis-server start
sudo service redis-server stop
|
Remote Access Configuration
Modify redis.conf:
1
| bind 0.0.0.0 # Allow all network interfaces
|
Core Operations
Basic Commands
1
2
3
4
5
| SET key value # Add key-value pair
DEL key # Delete key
EXPIRE key 60 # Set 60-second expiration
TTL key # Check remaining expiration
KEYS * # List all keys
|
List Operations
1
2
3
4
5
| LPUSH list1 "item1" # Add to list head
RPUSH list1 "item2" # Add to list tail
LRANGE list1 0 -1 # Get all elements
LREM list1 2 "value" # Remove two occurrences
LLEN list1 # Get list length
|
Set Operations
1
2
3
4
| SADD set1 "A" "B" # Add elements
SMEMBERS set1 # View all elements
SREM set1 "A" # Remove element
SINTER set1 set2 # Find intersections
|
Hash Operations
1
2
3
4
| HSET user:1001 name "John" # Create hash
HGET user:1001 name # Get value
HGETALL user:1001 # Get all fields
HDEL user:1001 email # Delete field
|
Advanced Features
Transactions
1
2
3
4
5
6
| MULTI # Start transaction
SET balance 100
SET credit 50
EXEC # Commit transaction
DISCARD # Cancel transaction
WATCH key1 # Monitor key changes
|
Pub/Sub System
1
2
| PUBLISH news "Update" # Send message to channel
SUBSCRIBE news # Receive messages
|
Persistence Configuration
Enable in redis.conf:
1
2
| save 900 1 # Save if 1+ changes in 15min
save 300 10 # Save if 10+ changes in 5min
|
Best Practices
- Use connection pooling for high traffic
- Monitor memory usage with
INFO MEMORY - Implement proper key expiration policies
- Use AOF (Append-Only File) for critical data
- Regularly backup RDB snapshots
Command Reference:
1
2
| redis-cli INFO SERVER # View server metrics
BGSAVE # Create background snapshot
|