Pub/Sub PatternsAdvanced
1Question 1 of 1
javascriptWhat are common architectural patterns for Pub/Sub in distributed systems?
Explanation & Concept
Common Redis Pub/Sub patterns include: • Event Broadcasting: Notifying thousands of connected WebSocket servers to broadcast an update to end-user clients. • Cache Invalidation: Publishing an eviction notice whenever an entity updates in the database so all microservices invalidate their local in-memory caches. • Configuration Reload: Notifying worker pods to refresh feature flags dynamically.
Code Example
javascript
// Cache Invalidation Publisher
async function updateProductPrice(productId, newPrice) {
await db.products.update(productId, { price: newPrice });
// Publish invalidation event across the cluster
await redis.publish("cache:invalidate", JSON.stringify({ entity: "product", id: productId }));
}
// Subscriber on all app nodes
redisSubscriber.subscribe("cache:invalidate", (message) => {
const { entity, id } = JSON.parse(message);
localMemoryCache.del(`${entity}:${id}`);
});How It Works & Key Takeaways
Broadcasting cache invalidation through Redis Pub/Sub ensures that multi-server local memory caches stay synchronized with the primary database.
Mock Test · 1 Questions · Optional
Ready for Memory Optimization?
Take a quick test to evaluate yourself, or move straight to the next module.
Advanced1 questions