What is Redis?
Redis (REmote DIctionary Server) is an open-source, in-memory data structure store used as:- Database: Persistent key-value storage
- Cache: High-performance caching layer
- Message Broker: Pub/sub messaging
Key Features
In-Memory Storage
All data stored in RAM for microsecond latency
Rich Data Types
Strings, Lists, Sets, Hashes, Sorted Sets, and more
Persistence
RDB snapshots and AOF logs for durability
High Performance
100,000+ operations per second capability
Installation & Setup
Linux Installation
Basic Configuration
Data Structures & Commands
String (字符串)
Binary-safe strings up to 512MB.Hash (哈希)
Field-value pairs, ideal for objects.Why Hash over String?
- More memory efficient for objects
- Update individual fields without fetching entire object
- Better semantic representation
List (列表)
Doubly-linked list, supports both ends operations.- Message queues
- Timeline feeds
- Latest items list
- Task scheduling
Set (集合)
Unordered collection of unique strings.- Tags
- Unique visitors
- Social relationships (followers, following)
- Lottery/raffle systems
Sorted Set (有序集合)
Set with score for each member, auto-sorted by score.- Leaderboards
- Priority queues
- Time-series data
- Rating systems
Advanced Data Types
Bitmaps
Bit-level operations on strings.HyperLogLog
Cardinality estimation with minimal memory.HyperLogLog Features:
- Uses only 12KB per key
- 0.81% standard error
- Estimates up to 2^64 unique elements
- Cannot retrieve individual elements
Caching Strategies
Cache-Aside Pattern
Cache Invalidation
Common Caching Issues
Cache Penetration (缓存穿透)
Problem: Queries for non-existent data bypass cache and hit database repeatedly. Solutions:- Bloom Filter
- Cache Null Values
Cache Avalanche (缓存雪崩)
Problem: Mass key expiration causes traffic spike to database. Solutions:Cache Breakdown (缓存击穿)
Problem: Hot key expires, causing concurrent requests to hit database. Solution: Mutex lockPersistence Mechanisms
RDB (Redis Database)
Point-in-time snapshots.AOF (Append-Only File)
Logs every write operation.Mixed Persistence (Redis 4.0+)
RDB format with AOF incremental changes.Eviction Policies
Whenmaxmemory limit is reached:
| Policy | Description |
|---|---|
| noeviction | Return error when memory limit reached |
| allkeys-lru | Evict least recently used keys (recommended) |
| allkeys-lfu | Evict least frequently used keys |
| allkeys-random | Evict random keys |
| volatile-lru | LRU among keys with TTL |
| volatile-lfu | LFU among keys with TTL |
| volatile-ttl | Evict keys with shortest TTL |
| volatile-random | Random among keys with TTL |
Choosing Eviction Policy:
- allkeys-lru: General purpose caching (most common)
- allkeys-lfu: Frequency-based access patterns
- volatile-ttl: When you set explicit TTLs
- noeviction: When cache misses are acceptable
Transaction Support
Optimistic Locking with WATCH
Java Client Example
Using Jedis
Performance Best Practices
Key Design
Key Design
- Use namespaces:
user:1000:profile - Keep keys short but descriptive
- Use consistent naming conventions
- Avoid very long keys (> 1KB)
Memory Optimization
Memory Optimization
- Use appropriate data structures
- Set TTL on all keys
- Use
SCANinstead ofKEYS * - Enable compression for large values
- Monitor memory usage regularly
Connection Management
Connection Management
- Use connection pooling (JedisPool)
- Reuse connections
- Set appropriate timeout values
- Close connections in finally blocks
Command Optimization
Command Optimization
- Use pipelining for bulk operations
- Prefer MGET/MSET over multiple GET/SET
- Avoid expensive commands (KEYS, FLUSHALL)
- Use Lua scripts for complex operations
Monitoring Commands
Next Steps
MySQL Basics
Learn relational database fundamentals
MySQL Advanced
Master indexes and optimization