Repository navigation
fix(cache): fail cache purges the breaker can't confirm - #14642
Conversation
When the circuit was open or the adapter failed, CircuitBreaker::purge() returned false. That is the same value as purging a key that is not there, so utopia-php/database read it as nothing to invalidate. It committed the write and served the old cached document, permissions included, once Redis recovered. purge() now throws Exception\Unavailable in both cases, with the adapter's error as the previous exception. A false from the adapter itself is still returned. Reads and saves keep their fallbacks. A failed save leaves a cache miss, which is not stale, so throwing there would only cost availability. In Appwrite Cloud a breaker trip opens the circuit for 30 seconds per worker. That only works if no adapter reports an error as false, so the purges now tell the two apart: - Redis: php-redis reports a lease script's error reply as false with getLastError(). A lease script always replies with an integer, so false now throws. - RedisCluster: HDEL's error reply was coerced to 0, and now throws. - Memcached and Hazelcast: a delete that fails with anything other than RES_NOTFOUND throws. - Filesystem: an unlink that leaves the file in place throws. Redis\Multiplexing already threw on error replies, and a test now pins that. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Review complete. 🟡 2 medium 💬 Inline comments (1)
📍 Findings outside the diff (1) — 🟡 1 medium — defects on lines GitHub can't attach comments to🟡 Medium — // packages/cache/src/Cache.php
291 public function purge(string $key, string $hash = ''): bool
292 {
293 $key = $this->caseSensitive ? $key : strtolower($key);
294 $hash = $this->caseSensitive ? $hash : strtolower($hash);
295
296 foreach (\array_keys($this->recordedPurges) as $recording) {
297 $this->recordedPurges[$recording][$key][$hash] = true;
298 }
299
300 $start = microtime(true);
301 $result = $this->adapter->purge($key, $hash);
302 $duration = microtime(true) - $start;
The change makes every cache adapter's
Reviewed commit: 7a3dfbb |
🟢 Tier S · Ready to mergeAdapter changes distinguish failed deletes from missing keys across filesystem, Redis, Memcached, Hazelcast. Redis Cluster, with regression coverage for breaker and backend behavior. Latest changes: The latest commit allows purging from a not-yet-created filesystem cache directory to return
📂 Walkthrough · 16
✅ Fixed since the last review · 1
🔇 Filtered out · 1Findings Hansi considered but did not post.
Reviewed the commits since |
Security rulesNo new WARNING or ERROR findings from security rules. 19 existing findings tracked in
|
There was a problem hiding this comment.
This PR hardens the packages/cache purge path so backend failures surface as exceptions (backed by a new CircuitBreaker adapter and Unavailable exception) instead of silent false returns, with E2E and unit coverage across Redis, RedisCluster, Memcached, Hazelcast, and Filesystem adapters.
Key findings
- 🟡
delpurge failure coerced to 0, misses the throw — RedisCluster.php:217 - 🟡
purgethrow contract breaks uncaught production callers — Cache.php:291
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
RedisCluster's DEL branch still declared an int return. Without strict types, a false error reply was coerced to 0 and read as nothing to purge. Filesystem's file_exists() also returns false when the cache directory cannot be searched, so a purge that never reached the file reported a missing key. Both now throw. A Hazelcast E2E case pins the same RES_NOTFOUND distinction that Memcached has. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
On the finding outside the diff ( |
A Filesystem cache whose directory was never created has nothing in it. Only a directory that exists and cannot be searched leaves the purge unconfirmed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Retires cached documents before a write the cache cannot confirm commits (utopia-php/database#1042), paired with the cache purge fix in appwrite#14642. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Since appwrite#14642, a purge the circuit breaker cannot confirm throws Utopia\Cache\Exception\Unavailable. The error hook had no case for it, so it reached clients as a 500 general_unknown that SDKs do not retry. Map it to a new general_cache_unavailable (503). It is not described as "not applied": a purge after the commit can fail too, so the client is told to read the affected resources back before retrying. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Root cause
Utopia\Cache\Adapter\CircuitBreakerreturns its fallback for every call when the circuit is open or the inner adapter throws. Forpurge()that fallback wasfalse, which is the same value as purging a key that is not there. utopia-php/database readsfalsefrom a purge as "nothing to invalidate". So a write made during a short Redis failure (one missed 0.1sreadTimeout, or a 30s breaker-open window in Cloud) committed, and the old cached document,$permissionsincluded, was served for up to 24h once Redis recovered.A probe against utopia-php/database 8.3.9 confirmed this for single updates (inside and outside transactions), deletes, batch updates and
purgeCachedCollection().Fix
CircuitBreaker::purge()throwsUtopia\Cache\Exception\Unavailablewhen the circuit is open and when the adapter fails the purge. The adapter's error is kept asprevious, and the failure still counts towards opening the circuit.falsereturned by the adapter itself, such as purging a missing key, is still returned.The breaker can only tell the two cases apart if no adapter reports an error as
false. These adapters now separate them:Redis(php-redis)false, sopurge()returnedfalseRedisExceptionwithgetLastError(). A lease script always replies with an integerRedisCluster0, sofalseRedisClusterExceptionMemcached,HazelcastfalseRES_NOTFOUNDFilesystemunlink()returnedfalseRedis\MultiplexingBefore / after
falseUnavailable, adapter not calledTimeoutException)falseUnavailable(previous: the cause), counted as a failurefalsefalseTests
These fail on
mainand pass here:CircuitBreakerTest::testAFailedPurgeThrowsInsteadOfReportingNothingToPurgeCircuitBreakerTest::testAPurgeWhileTheCircuitIsOpenThrowsWithoutReachingTheAdapterE2E\RedisTest::testAPurgeTheServerRejectsThrowsInsteadOfReportingNothingToPurge(WRONGTYPE)E2E\MemcachedTest::testAPurgeTheServerRejects…(key longer than 250 bytes)FilesystemTest::testAPurgeThatCannotRemoveTheFileThrows…New guards, which also pass on
main:testAFailedPurgeCountsTowardsOpeningTheCircuit,testOtherOperationsKeepTheirFallbacksWhileTheCircuitIsOpen,testPurgingAMissingKeyStillReturnsFalse, and theRedisCluster/MultiplexingWRONGTYPE purge tests.I could not run the
RedisClusterandMultiplexingE2E tests locally (amd64-only cluster image on arm64, and a local Swoole build that segfaults); CI runs them.bin/monorepo check cachepasses: Pint, Rector and PHPStan. The PHPStan baseline count drops by one now thatRedisCluster::purge()is typed.The database side is utopia-php/database#1042: batch writes and schema changes are retired before they commit, so the throw rolls them back.
🤖 Generated with Claude Code