Common Beginner Mistakes
19. Common Beginner Mistakes
Section titled “19. Common Beginner Mistakes”Bad vs Good: A Quick Reference
Section titled “Bad vs Good: A Quick Reference”flowchart TB Mistakes[Common Redis Mistakes ❌] --> M1[Mistake 1: No TTL on Cache] Mistakes --> M2[Mistake 2: Using KEYS * in Production] Mistakes --> M3[Mistake 3: Storing Huge Values] Mistakes --> M4[Mistake 4: Bad Key Naming] Mistakes --> M5[Mistake 5: Not Handling Cache Errors] Mistakes --> M6[Mistake 6: Redis as Primary DB] Mistakes --> M7[Mistake 7: No maxmemory Set]
M1 --> F1[Fix: SET key value EX 300] M2 --> F2[Fix: Use SCAN instead] M3 --> F3[Fix: Store individual items] M4 --> F4[Fix: Use user:id:field naming] M5 --> F5[Fix: try/catch + DB fallback] M6 --> F6[Fix: Pair Redis with durable DB] M7 --> F7[Fix: Set maxmemory + eviction policy]
style Mistakes fill:#ef4444,color:#fff style M1 fill:#ef4444,color:#fff style M2 fill:#ef4444,color:#fff style M3 fill:#ef4444,color:#fff style M4 fill:#ef4444,color:#fff style M5 fill:#ef4444,color:#fff style M6 fill:#ef4444,color:#fff style M7 fill:#ef4444,color:#fff style F1 fill:#059669,color:#fff style F2 fill:#059669,color:#fff style F3 fill:#059669,color:#fff style F4 fill:#059669,color:#fff style F5 fill:#059669,color:#fff style F6 fill:#059669,color:#fff style F7 fill:#059669,color:#fffThemes: Every mistake comes from treating Redis like a traditional database or forgetting it’s an in-memory cache with finite RAM.
Mistake 1: No Expiration on Cache Keys
Section titled “Mistake 1: No Expiration on Cache Keys”# ❌ BAD — key lives foreverSET cache:products '{"items": [...]}'
# ✅ GOOD — expires in 5 minutesSET cache:products '{"items": [...]}' EX 300Why it matters: Without TTL, cache entries pile up indefinitely. Redis will eventually hit its memory limit and start evicting random keys or refusing writes.
Mistake 2: Using KEYS * in Production
Section titled “Mistake 2: Using KEYS * in Production”# ❌ NEVER do this in productionKEYS * # Scans ALL keys — blocks Redis!KEYS user:*
# ✅ Use SCAN instead (non-blocking, cursor-based)SCAN 0 MATCH user:* COUNT 100Why it matters: KEYS is O(n) and blocks all other commands while running. On a database with millions of keys, this can freeze Redis for seconds.
Mistake 3: Storing Huge Values
Section titled “Mistake 3: Storing Huge Values”# ❌ BAD — storing entire catalog as one keySET catalog:all '{...10MB of data...}'
# ✅ GOOD — store individual itemsHSET product:1001 name "Laptop" price "45000"HSET product:1002 name "Mouse" price "1500"Why it matters: Large values slow down serialization, network transfer, and consume more memory per key.
Mistake 4: Bad Key Naming
Section titled “Mistake 4: Bad Key Naming”# ❌ BAD — unclear, may conflictSET u42 "Prathamesh"SET p9001 "Laptop"SET x "temp"
# ✅ GOOD — structured, readableSET user:42:name "Prathamesh"SET product:9001:name "Laptop"SET cache:homepage:v2 "..."Mistake 5: Not Handling Cache Errors Gracefully
Section titled “Mistake 5: Not Handling Cache Errors Gracefully”// ❌ BAD — crashes the app if Redis is downconst data = JSON.parse(await redis.get('cache:key'));return res.json(data);
// ✅ GOOD — fallback to database if Redis failstry { const cached = await redis.get('cache:key'); if (cached) return res.json(JSON.parse(cached));} catch (err) { console.warn('Redis unavailable, using DB fallback:', err.message);}const data = await db.query('...');return res.json(data);Mistake 6: Using Redis as a Primary Database
Section titled “Mistake 6: Using Redis as a Primary Database”Redis is not designed to replace your primary relational or document database. It is a complementary tool for:
- Caching
- Session management
- Rate limiting
- Real-time data
Always keep your primary data in a durable database (MySQL, PostgreSQL, MongoDB).
Mistake 7: Not Setting maxmemory
Section titled “Mistake 7: Not Setting maxmemory”Without a memory limit, Redis will consume all available RAM and potentially crash your server.
# In redis.confmaxmemory 512mbmaxmemory-policy allkeys-lru
# Or via CLICONFIG SET maxmemory 512mbCONFIG SET maxmemory-policy allkeys-lruEviction policies:
| Policy | Behavior |
|---|---|
noeviction | Return error when memory full |
allkeys-lru | Evict least recently used keys |
volatile-lru | Evict LRU keys with TTL only |
allkeys-random | Evict random keys |
volatile-ttl | Evict keys with shortest TTL |