How RocksCache prevents common cache issues
mainRocksCache includes built-in protections against several common distributed caching problems:
Anti-Breakdown (Cache Breakdown)
Prevents multiple processes from hammering the database when a hot key expires. It uses singleflight within a single process and distributed locks in Redis to ensure only one request reaches the database for a specific key. Unlike standard solutions, it can return data immediately to waiting requests.
Anti-Penetration
Prevents requests for non-existent data from hitting the database repeatedly. If the fetch function fn returns an empty string, RocksCache caches this empty result for a duration defined by EmptyExpire (default: 60s). Set EmptyExpire to 0 to disable this.
Anti-Avalanche
Prevents mass cache expiration (thundering herd) by adding jitter to expiration times. The RandomExpireAdjustment option (default: 0.1) adjusts the requested expiration time by a random amount within a range to spread out expirations.