
Caching Patterns
Redis caching strategies, cache invalidation, write-through/write-behind, TTL...
Install
npx skills add vibeeval/vibecosystem --skill caching-patterns -g -a claude-code -y- Repository
- vibeeval/vibecosystem541
- Category
- Writing
- Updated
- 3mo ago
- License
- MIT
- Context cost
- Mediumabout 2,108 tokens
- Runs scripts
- No, instructions only
What it does
Redis caching strategies, cache invalidation, write-through/write-behind, TTL management, and cache stampede protection.
Caching Patterns
Redis-based caching strategies for reducing latency and database load.
Cache Key Design
// Namespace:entity:id format
const CacheKeys = {
market: (id: string) => `market:v1:${id}`,
marketList: (filters: string) => `market:list:${filters}`,
user: (id: string) => `user:v1:${id}`,
userMarkets: (userId: string, page: number) => `user:${userId}:markets:${page}`,
leaderboard: () => 'leaderboard:v1:global'
}
// Version prefix allows instant cache bust on schema change:
// bump v1 → v2 to invalidate all market keys without scanning
Cache-Aside (Lazy Loading)
import Redis from 'ioredis'
const redis = new Redis(process.env.REDIS_URL!)
const DEFAULT_TTL = 300 // 5 minutes
async function getOrSet<T>(
key: string,
loader: () => Promise<T>,
ttl = DEFAULT_TTL
): Promise<T> {
const cached = await redis.get(key)
if (cached) return JSON.parse(cached) as T
const value = await loader()
await redis.setex(key, ttl, JSON.stringify(value))
return value
}
// Usage
async function getMarket(id: string): Promise<Market> {
return getOrSet(
CacheKeys.market(id),
() => db.market.findUniqueOrThrow({ where: { id } }),
300
)
}
Write-Through Pattern
// Write to cache AND database together - cache is always fresh
async function updateMarket(id: string, data: UpdateMarketDto): Promise<Market> {
const updated = await db.market.update({ where: { id }, data })
// Synchronously update cache so next read is fresh
await redis.setex(CacheKeys.market(id), DEFAULT_TTL, JSON.stringify(updated))
return updated
}
async function deleteMarket(id: string): Promise<void> {
await db.market.delete({ where: { id } })
await redis.del(CacheKeys.market(id))
}
Write-Behind (Write-Back) Pattern
// Write to cache immediately, flush to DB asynchronously (higher throughput)
// Risk: data loss on crash if queue not durable
