Back to results for “Caching Patterns”
Redis › Keyspace Notifications

How would you build a reliable cache invalidation system using keyspace notifications, while accounting for their delivery limitations?

Advanced

Answer

You would use keyspace notifications as a fast, best effort mechanism to quickly invalidate related cache entries, but combine it with a periodic reconciliation process that independently verifies cached data against the source of truth, ensuring that even if a notification is occasionally missed, stale data does not persist indefinitely.
SUBSCRIBE __keyevent@0__:set
-- On notification, invalidate related cache entries
-- A separate periodic job also double checks cache freshness
Real-world example An e-commerce platform uses keyspace notifications for fast, immediate cache invalidation when product data changes, while also running an hourly reconciliation job that catches any rare cases where a notification might have been missed.

Common follow-ups: How often should a reconciliation job run to catch missed notifications without adding excessive overhead?;What other guaranteed delivery mechanisms could replace Pub/Sub for this purpose?