Architecture Intermediate
Caching strategie — kde a jak cachovat¶
CachingRedisPerformance 3 min read
Cache-aside, write-through, write-behind a invalidace cache. Kompletní průvodce.
Cache-Aside¶
async function getUser(id) {
let user = await redis.get(\`user:\${id}\`);
if (user) return JSON.parse(user); // Cache hit
user = await db.query('SELECT * FROM users WHERE id = $1', [id]);
await redis.setex(\`user:\${id}\`, 3600, JSON.stringify(user));
return user;
}
Strategies¶
- Cache-Aside (Lazy Loading) — čti z cache, při miss načti z DB
- Write-Through — zápis přes cache do DB, cache vždy aktuální
- Write-Behind — zápis do cache, async do DB (rychlejší, rizikovější)
Invalidace¶
- TTL — cache expiruje po čase
- Event-driven — při změně dat publikujte invalidační event
- Version-based — cache key obsahuje verzi
Summary¶
Cache-aside je nejbezpečnější start. Vždy mějte strategii pro invalidaci. Cache je optimalizace, ne datový zdroj.
Need Help with Implementation?¶
Our team has experience designing and implementing modern architectures. We’re happy to help.