Repository navigation
Conversation
The breaker turned every cache error into a fallback value, so a failed save or purge came back as false. utopia-php/database reads false from a purge as "nothing was there" and false from a load as "no epoch to invalidate", so a write made while Redis timed out or the breaker was open committed and left the old document cached for up to 24 hours. The Redis adapters also report a save that misses its deadline as false rather than throwing, so the breaker never counted those as failures. save, saveMany, purge and flush now throw Exception\Unavailable when the adapter fails or the breaker is open, with the cause chained. A save or saveMany reported as false counts as a failure too. Input every adapter refuses (an empty key or empty data) still returns false and does not count against the breaker; a purge that removed nothing still returns false. Reads keep falling back: load, loadMany, list, getGeneration, ping and getSize. So do saveWithLease and touch. Losing either only skips a fill or a refresh and never leaves stale data behind. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A write that cannot invalidate the cache now fails with Utopia\Cache\Exception\Unavailable. Map it to general_cache_unavailable (503) so clients see a retryable error instead of a bare 500. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Review complete. No issues found — approved ✅. This PR introduces a circuit-breaker-wrapped cache adapter so failed cache writes surface as an explicit 503 instead of being silently mistaken for success, plus the error-catalog and exception wiring to translate that to API clients.
Reviewed commit: 22713e9 |
🔵 Tier A · Mergeable after minor fixes
This change makes circuit-breaker cache writes surface
Note Not approving while a bug finding is open: Preserve adapter refusals for reserved Redis fields
Fix with agent prompt### Issue 1
packages/cache/src/Adapter/CircuitBreaker.php:72-75
**Preserve adapter refusals for reserved Redis fields**
Redis adapters deliberately return `false` for reserved fields such as `__utopia_gen__`; `saveMany` can also return `false` when all fields are reserved. These are input refusals, not cache outages, but this branch now counts them as breaker failures and turns them into `Unavailable` (and an API 503), despite the stated behavior of preserving refusals.
### Issue 2
packages/cache/src/Adapter/CircuitBreaker.php:199
**Treat a false flush result as a failed write**
With `stored: false`, a `false` result passes through without counting against the breaker or throwing. For example, Hazelcast explicitly returns `false` because it cannot flush, and `Sharding::flush()` returns `false` when a shard fails; callers therefore still receive an ordinary failure value instead of the promised `Unavailable`.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.📂 Walkthrough · 7
Reviewed |
| if ($stored && $result === false) { | ||
| $attempt->failure = new Unavailable("Cache {$method} of '{$key}' was not stored"); | ||
|
|
||
| throw $attempt->failure; |
There was a problem hiding this comment.
Preserve adapter refusals for reserved Redis fields
Redis adapters deliberately return false for reserved fields such as __utopia_gen__; saveMany can also return false when all fields are reserved. These are input refusals, not cache outages, but this branch now counts them as breaker failures and turns them into Unavailable (and an API 503), despite the stated behavior of preserving refusals.
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/cache/src/Adapter/CircuitBreaker.php
Line: 72-75
Comment:
**Preserve adapter refusals for reserved Redis fields**
Redis adapters deliberately return `false` for reserved fields such as `__utopia_gen__`; `saveMany` can also return `false` when all fields are reserved. These are input refusals, not cache outages, but this branch now counts them as breaker failures and turns them into `Unavailable` (and an API 503), despite the stated behavior of preserving refusals.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.🟡 Minor · bug · Reply if this doesn't apply.
| { | ||
| /** @var bool $result */ | ||
| $result = $this->delegate(__FUNCTION__, \func_get_args(), false); | ||
| $result = $this->write(__FUNCTION__, [], '', stored: false); |
There was a problem hiding this comment.
Treat a false flush result as a failed write
With stored: false, a false result passes through without counting against the breaker or throwing. For example, Hazelcast explicitly returns false because it cannot flush, and Sharding::flush() returns false when a shard fails; callers therefore still receive an ordinary failure value instead of the promised Unavailable.
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/cache/src/Adapter/CircuitBreaker.php
Line: 199
Comment:
**Treat a false flush result as a failed write**
With `stored: false`, a `false` result passes through without counting against the breaker or throwing. For example, Hazelcast explicitly returns `false` because it cannot flush, and `Sharding::flush()` returns `false` when a shard fails; callers therefore still receive an ordinary failure value instead of the promised `Unavailable`.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.🟡 Minor · bug · Reply if this doesn't apply.
Security rulesNo new WARNING or ERROR findings from security rules. 19 existing findings tracked in
|
✨ Benchmark resultsComparing
Per-scenario breakdown & investigation detailsMetrics below reflect the current branch (after). Δ P95 compares against the base.
Top API waits (after)
|
What
Utopia\Cache\Adapter\CircuitBreakernow throwsUtopia\Cache\Exception\Unavailablefor writes that change what the cache holds:save,saveMany,purgeandflush. It throws when the adapter fails and when the breaker is open, and chains the cause. Reads keep their fallbacks.save,saveMany(also when the adapter reportsfalse)Unavailable(wasfalse)purge,flushUnavailable(wasfalse)load,loadMany,list,getGeneration,ping,getSizesaveWithLease,touchfalse, unchanged: losing one only skips a fill or a refreshA save the adapter reports as
falsenow counts as a breaker failure. The Redis adapters returnfalsefor a write that misses its deadline, so before this the breaker never counted those. Input every adapter refuses (an empty key, or empty data such as'0') still returnsfalseand does not count. A purge that removed nothing still returnsfalse.The API maps
Unavailableto a newgeneral_cache_unavailableerror (503) instead of a bare 500.Why
Only Appwrite Cloud puts the breaker in front of the database cache. With every Redis error turned into
false, utopia-php/database read a failed purge as done and a failed epoch load as "nothing to invalidate". A write made during one 250ms timeout, or while the breaker was open, committed. Once Redis recovered, the old document (with its old$permissions) was served for up to 24h. With this change the library's existing rollback paths see the failure and roll the write back. The utopia-php/database follow-up covers registration retries and schema changes.Availability trade-off: while the cache is down, writes that must invalidate it fail with a 503 instead of committing stale. Reads still fall back to the database.
Tests
packages/cache/tests/CircuitBreakerTest.php:testWritesThrowWhenCacheOperationsFailfalsereturned)testWriteReportedAsFalseThrowsAndOpensTheBreakertestSaveManyReportedAsFalseThrowstestOpenBreakerThrowsOnWritesWithoutCallingTheAdaptertestCacheSurfacesWriteFailurestestReadsFallBackWhenCacheOperationsFailtestSavesEveryAdapterRefusesAreNotFailurestestPurgeOfAMissingKeyIsNotAFailureBefore = the same tests run against
main'sCircuitBreaker.php. The cache unit suite passes (121 tests), andbin/monorepo check cache(Rector, Pint, PHPStan) is clean.🤖 Generated with Claude Code