13. Cheat Sheet
13.1 Data Type Commands Quick Reference
| Type | Command | Description |
|---|---|---|
| String | SET key val | Set value |
| GET key | Get value | |
| MSET / MGET | Batch set/get | |
| INCR / DECR | Increment/decrement | |
| INCRBY key n | Add n | |
| SETNX key val | Set if not exists | |
| List | LPUSH / RPUSH | Push to left/right |
| LPOP / RPOP | Pop from left/right | |
| LRANGE start stop | Range query | |
| LLEN key | List length | |
| BRPOP key timeout | Blocking pop | |
| Hash | HSET key field val | Set field |
| HGET key field | Get field | |
| HGETALL key | Get all fields | |
| HDEL key field | Delete field | |
| HINCRBY key field n | Increment field by n | |
| Set | SADD key member | Add member |
| SMEMBERS key | All members | |
| SISMEMBER key m | Check membership | |
| SINTER key1 key2 | Intersection | |
| SUNION key1 key2 | Union | |
| ZSet | ZADD key score m | Add scored member |
| ZRANGE key 0 -1 | Ascending range | |
| ZREVRANGE key 0 2 | Top N descending | |
| ZSCORE key m | Get score | |
| ZINCRBY key n m | Add n to score |
13.2 Key Management Quick Reference
| Command | Description | Time Complexity |
|---|---|---|
| EXISTS key | Check if key exists | O(1) |
| DEL key | Delete key | O(N) N=field count |
| UNLINK key | Async delete key | O(1) |
| EXPIRE key seconds | Set expiration | O(1) |
| TTL key | Remaining TTL | O(1) |
| PERSIST key | Remove expiration | O(1) |
| TYPE key | Key data type | O(1) |
| RENAME old new | Rename key | O(1) |
| SCAN cursor | Iterate key space | O(1) per call |
13.3 Server Management Quick Reference
| Command | Description |
|---|---|
| INFO | Server info (ALL/MEMORY/CPU/REPLICATION/...) |
| CONFIG GET parameter | Get configuration |
| CONFIG SET parameter value | Modify configuration |
| CONFIG REWRITE | Persist config to file |
| DBSIZE | Key count in current DB |
| FLUSHDB | Flush current DB |
| FLUSHALL | Flush all DBs (use with caution!) |
| MONITOR | Real-time monitor all commands |
| CLIENT LIST | List client connections |
| CLIENT KILL | Disconnect client |
| SLOWLOG GET n | View slow queries |
| BGREWRITEAOF | Trigger AOF rewrite |
| BGSAVE | Background RDB snapshot |
| LASTSAVE | Last snapshot timestamp |
13.4 Development Guidelines Quick Reference
- 🔑 Key naming convention:
business:object:ID:attribute, e.g.user:1001:profile - 📏 Key length: Try to keep under 44 bytes (jemalloc memory alignment)
- 📦 Value size: String no more than 10KB, Hash/Set/ZSet no more than 5000 elements
- ⏱️ Expiration: All cache keys should have expiration times set to prevent memory leaks
- 🚫 Disabled commands:
KEYS,FLUSHALL,FLUSHDBshould be disabled in production - 🔌 Connection pool: Use connection pooling to reuse connections, avoid frequent create/destroy
- 🔄 Pipeline: Prefer Pipeline for batch operations
- 🧵 Serialization: Use MessagePack/Protobuf instead of JSON for complex objects to save space
13.5 Deployment Architecture Selection
| Scenario | Architecture | Node Count |
|---|---|---|
| Development/testing | Single node | 1 |
| Small production (read-heavy) | Replication + Sentinel | 1 master + 2 replicas + 3 sentinels |
| Medium production (high availability) | Replication + Sentinel | 1 master + 2 replicas + 3 sentinels (per master) |
| Large production (high data volume) | Redis Cluster | At least 6 nodes (3 masters + 3 replicas) |
| Ultra-large scale | Redis Cluster + Proxy | Multi-cluster + Proxy (e.g. Twemproxy) |