Stale cache values after tag flush with file driver in Lumen
0 reputation · 21 Jun 2020, 12:21 UTC
0 reputation · 21 Jun 2020, 12:21 UTC
Goal: verify that after executing Cache::tags('posts')->flush() the associated cached values are no longer returned, ensuring that stale reads do not occur.
Constraints: the file driver may delay actual deletion due to OS page caching, while the Redis driver performs atomic tag removal but still depends on the timing of the flush relative to data updates. It is unclear how long a stale‑read window can persist and whether any configuration can reduce it without changing drivers.
Questions: What is the maximum observable stale‑read window after a tag flush with the file driver? Does the Redis driver guarantee immediate consistency after a tag flush, or can a race condition still serve stale data?
29775 reputation · 21 Jun 2020, 13:14 UTC
After Cache::tags('posts')->flush() the file driver may still return a value that was cached under that tag for a short period. On a typical local disk the window is on the order of a few milliseconds to about one second, depending on the operating‑system page‑cache eviction policy. On networked file systems (NFS, SMB, etc.) the delay can grow to several seconds or more.
The Redis driver deletes all keys belonging to the tag in a single transaction. Once the flush completes, any subsequent read will see the new value (or a miss) – the driver guarantees immediate consistency. A race can only occur if a write to the same key happens concurrently with the flush; otherwise Redis guarantees that the old value cannot be served.
Cache a value with a tag:
Cache::tags('posts')->put('posts.123', ['title' => 'Hello'], 3600);
Flush the tag:
Cache::tags('posts')->flush();
Immediately read the key and note the result:
$value = Cache::get('posts.123');
var_dump($value);
Measure the time between the flush call and the read. If $value is not null, the stale window is at least that many milliseconds.
Repeat the test on a networked filesystem to compare delays.
Cache a value with a tag as above, using the Redis driver.
Flush the tag.
Immediately read the key. It should return null.
Insert a new value to the same key after the flush and read again – the new value should be returned, confirming that the flush erased the old entry.
To refine the recommendation, please let us know whether your cache directory lives on a local disk or a networked filesystem (e.g., NFS, SMB). The type of storage can significantly affect the stale‑read window for the file driver.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.