|
| 1 | +🗓️ 06022026 1116 |
| 2 | + |
| 3 | +# caffeine-refresh-vs-expire |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +| Feature | `expireAfterWrite` | `refreshAfterWrite` | |
| 8 | +|-----------------|-----------------------------------------------|------------------------------------------------------| |
| 9 | +| **Behavior** | Entry is **removed** after duration | Entry is **refreshed** asynchronously after duration | |
| 10 | +| **Stale reads** | No (returns null or recomputes synchronously) | Yes (returns stale value while refreshing) | |
| 11 | +| **Blocking** | Yes (on cache miss) | No (refresh happens in background) | |
| 12 | +| **Use case** | Data that must not be stale | Data where eventual consistency is acceptable | |
| 13 | + |
| 14 | +## expireAfterWrite |
| 15 | + |
| 16 | +Entries are **evicted** after the specified duration. The next request will block while the value is recomputed. |
| 17 | + |
| 18 | +```java |
| 19 | +LoadingCache<String, User> cache = Caffeine.newBuilder() |
| 20 | + .expireAfterWrite(5, TimeUnit.MINUTES) |
| 21 | + .build(key -> userService.fetchUser(key)); |
| 22 | + |
| 23 | +// After 5 minutes, entry is removed |
| 24 | +// Next get() blocks until fetchUser() completes |
| 25 | +User user = cache.get("user123"); |
| 26 | +``` |
| 27 | + |
| 28 | +**Timeline:** |
| 29 | +``` |
| 30 | +t=0 -> cache.get("A") -> MISS, blocks, fetches, returns fresh value |
| 31 | +t=4min -> cache.get("A") -> HIT, returns cached value |
| 32 | +t=6min -> cache.get("A") -> MISS (expired), blocks, fetches, returns fresh value |
| 33 | +``` |
| 34 | + |
| 35 | +## refreshAfterWrite |
| 36 | + |
| 37 | +Entries are **refreshed asynchronously** after the specified duration. The stale value is returned immediately while refresh happens in the background. |
| 38 | + |
| 39 | +```java |
| 40 | +LoadingCache<String, User> cache = Caffeine.newBuilder() |
| 41 | + .refreshAfterWrite(5, TimeUnit.MINUTES) |
| 42 | + .expireAfterWrite(10, TimeUnit.MINUTES) // recommended: set expiry as upper bound |
| 43 | + .build(key -> userService.fetchUser(key)); |
| 44 | + |
| 45 | +// After 5 minutes, entry is marked for refresh |
| 46 | +// get() returns stale value immediately, triggers async refresh |
| 47 | +User user = cache.get("user123"); |
| 48 | +``` |
| 49 | + |
| 50 | +**Timeline:** |
| 51 | +``` |
| 52 | +t=0 -> cache.get("A") -> MISS, blocks, fetches, returns fresh value |
| 53 | +t=4min -> cache.get("A") -> HIT, returns cached value |
| 54 | +t=6min -> cache.get("A") -> HIT (stale), returns cached value, triggers async refresh |
| 55 | +t=6min+1ms -> refresh completes in background, cache updated |
| 56 | +t=7min -> cache.get("A") -> HIT, returns fresh value |
| 57 | +``` |
| 58 | + |
| 59 | +## Key Differences |
| 60 | + |
| 61 | +### 1. Blocking Behavior |
| 62 | + |
| 63 | +```java |
| 64 | +// expireAfterWrite: blocks on expired entry |
| 65 | +cache.get("key"); // blocks if expired |
| 66 | + |
| 67 | +// refreshAfterWrite: never blocks (returns stale) |
| 68 | +cache.get("key"); // returns immediately, even if stale |
| 69 | +``` |
| 70 | + |
| 71 | +### 2. Combined Usage (Recommended) |
| 72 | + |
| 73 | +Always pair `refreshAfterWrite` with `expireAfterWrite` as a **failsafe**: |
| 74 | + |
| 75 | +```java |
| 76 | +Caffeine.newBuilder() |
| 77 | + .refreshAfterWrite(1, TimeUnit.MINUTES) // refresh after 1 min |
| 78 | + .expireAfterWrite(5, TimeUnit.MINUTES) // hard expiry after 5 min |
| 79 | + .build(loader); |
| 80 | +``` |
| 81 | + |
| 82 | +**Why is expireAfterWrite needed?** |
| 83 | + |
| 84 | +`refreshAfterWrite` only triggers **on access**. The expiry acts as a safety net for edge cases: |
| 85 | + |
| 86 | +| Scenario | Without expireAfterWrite | With expireAfterWrite | |
| 87 | +| ------------------------ | -------------------------------- | ----------------------- | |
| 88 | +| Key never accessed again | Stale data sits in cache forever | Evicted after 5 min | |
| 89 | +| Refresh throws exception | Stale value remains indefinitely | Evicted after 5 min | |
| 90 | +| Refresh keeps failing | Data becomes arbitrarily old | Hard limit on staleness | |
| 91 | + |
| 92 | +**In normal operation** (frequent reads, successful refreshes), expireAfterWrite rarely triggers — the refresh keeps the entry fresh. It's a **worst-case bound**, not for typical flow. |
| 93 | + |
| 94 | +## When to Use What |
| 95 | + |
| 96 | +| Scenario | Recommendation | |
| 97 | +|----------|----------------| |
| 98 | +| User session data | `expireAfterWrite` (security-sensitive) | |
| 99 | +| Config/settings | `refreshAfterWrite` + `expireAfterWrite` | |
| 100 | +| External API responses | `refreshAfterWrite` + `expireAfterWrite` | |
| 101 | +| Rate limit counters | `expireAfterWrite` (must be accurate) | |
| 102 | +| Feature flags | `refreshAfterWrite` (eventual consistency OK) | |
| 103 | + |
| 104 | + |
| 105 | +--- |
| 106 | +## References |
0 commit comments