Skip to content
Caching

Lesson 13 of 14 · 20 min

x
13/14

Lesson position in the course — not completion. Use Mark Complete to track finished lessons (saved in this browser).

Caching in Microservices & Monitoring

In a microservices architecture, each service may have its own cache — but shared data creates invalidation challenges. When the Product Service updates a price, the Cart Service, Search Service, and Recommendation Service may all cache stale product data. Event-driven invalidation via a message bus (Redis Pub/Sub, Kafka) is the standard solution: publish product.updated, every service drops its local and distributed cache entries for that product.

Monitoring separates a healthy cache from a decorative one. Track hit ratio (target: >90% for read-heavy workloads), cache latency (p50, p99), memory usage, eviction rate, and connection count. Alert when hit ratio drops suddenly — it usually means a deployment invalidated keys, TTLs are too short, or access patterns shifted. Tools: Redis INFO command, Prometheus + Grafana dashboards, Datadog cache analytics.

Before
No monitoring — cache might be useless
1// No idea if caching is working2const cached = await redis.get(key);3if (cached) return JSON.parse(cached);4// ... fetch from DB
After
Instrumented cache with metrics
1const cacheHits = new Counter('cache_hits_total');2const cacheMisses = new Counter('cache_misses_total');3const cacheLatency = new Histogram('cache_op_duration_seconds');4 5async function get(key) {6  const start = Date.now();7  const cached = await redis.get(key);8  cacheLatency.observe((Date.now() - start) / 1000);9 10  if (cached) {11    cacheHits.inc();12    return JSON.parse(cached);13  }14  cacheMisses.inc();15  const data = await fetchFromDB(key);16  await redis.setex(key, 300, JSON.stringify(data));17  return data;18}19 20// Alert: hit_ratio < 0.8 for 5 minutes

Check your understanding

  • How do microservices invalidate shared product data?Show answer

    Answer

    Publish an update event; each service drops its local/Redis entries for that id.
  • Which metrics matter most?Show answer

    Answer

    Hit ratio, cache op latency (p50/p99), memory/evictions, and sudden hit-ratio drops after deploys.
  • What does a sudden hit-ratio drop often mean?Show answer

    Answer

    Key namespace change, too-short TTLs, invalidation flood, or traffic mix shift.
Previous

Progress is saved in this browser.

Next Lesson