Skip to content

fix(cache): count the error behind a redis write reported as false - #14641

Merged
abnegate merged 2 commits into
mainfrom
fix/cache-swallowed-write-errors
Oct 19, 2026
Merged

abnegate merged 2 commits into
mainfrom
fix/cache-swallowed-write-errors

Conversation

@abnegate

Copy link
Copy Markdown
Member

What

Redis::save()/saveMany(), Redis\Multiplexing::save()/saveMany() and Leasable::saveWithLease() swallow every Throwable and return false. They now pass the cause to a recordWriteFailure() hook on Leasable. Redis\Multiplexing counts it as cache.redis_multiplexing.write.failures, tagged with operation and error (the short exception class, e.g. TimeoutException). The return values are unchanged.

Why

In Appwrite Cloud, a multiplexed HSET that missed its 100ms read deadline reached utopia-php/database as Failed to register document cache owner. Nothing recorded why, so it was first blamed on Redis memory, but Dragonfly showed zero OOM rejections. With this counter, the next slow or refused write says which it was. The database side of the fix is utopia-php/database#1041.

Tests

  • New MultiplexingDeadlineTest::testAWriteThatMissesItsDeadlineCountsItsCause: pauses the server, asserts save() returns false, and asserts the counter recorded ['operation' => 'save', 'error' => 'TimeoutException']. It fails without the change.
  • bin/monorepo check cache --linked passes (Pint, PHPStan max, Rector).
  • The unit suite passes, and so do the Multiplexing, MultiplexingLeasable, MultiplexingReader, MultiplexingDeadline and Redis E2E suites.

🤖 Generated with Claude Code

Redis::save(), saveMany() and saveWithLease() catch every Throwable and
return false, so a write that misses its read deadline looks the same
as one Redis refused. In Appwrite Cloud that false reached
utopia-php/database as "Failed to register document cache owner".
Nothing recorded why, which led to a wrong out-of-memory diagnosis:
Dragonfly reported zero OOM rejections.

The adapters now pass the cause to a recordWriteFailure() hook on
Leasable. Redis\Multiplexing, the adapter Cloud runs and the one that
already reports telemetry, counts it as
cache.redis_multiplexing.write.failures, tagged with the operation and
the short error class. The write still returns false, so callers see
the same behaviour.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@luxor-code-review

luxor-code-review Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Review complete. 🟡 1 medium

💬 Inline comments (1)


This PR extends the Redis cache adapter with a multiplexing mode, a leasable write path, and write-failure telemetry counters, adding an E2E test for read deadlines.

Files Change
packages/cache/src/Adapter/Redis.php Adds telemetry wiring and write-failure accounting to save/saveMany paths
packages/cache/src/Adapter/Redis/Leasable.php New trait implementing Lua-based lease acquire/release around cached writes
packages/cache/src/Adapter/Redis/Multiplexing.php New coroutine-multiplexed Redis adapter with a shared reader and per-command deadlines
packages/cache/tests/E2E/MultiplexingDeadlineTest.php E2E test covering read deadline expiry and recovery

One medium-confidence robustness issue remains in the telemetry instrumentation path, noted as an inline comment.

Reviewed commit: 2bc2913

@ghost

ghost commented Oct 19, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The added failure-path coverage matches the implementation, and no unresolved issues remain in the reviewed changes.

Redis write exceptions that are converted to false are now passed to a failure hook, and the multiplexing adapter records a counter tagged with the operation and short exception class. The deadline integration test verifies timeout reporting for save, saveMany, and saveWithLease while preserving their false results.

Latest changes: The deadline test now exercises saveMany and saveWithLease alongside save, and asserts the expected operation tag for each timeout.

Verdict New comments Fixed Still open
✅ Approved 0 1 0
📂 Walkthrough · 4
File Change
packages/cache/src/Adapter/Redis.php Pass exceptions from failed Redis save and saveMany calls to the shared write-failure hook.
packages/cache/src/Adapter/Redis/Leasable.php Add a default no-op failure hook and report saveWithLease exceptions through it.
packages/cache/src/Adapter/Redis/Multiplexing.php Count failed writes with operation and short exception-class attributes.
packages/cache/tests/E2E/MultiplexingDeadlineTest.php Verify timed-out save, saveMany, and saveWithLease calls return false and record their causes.
✅ Fixed since the last review · 1
  • Cover failure instrumentation for the other write operations · packages/cache/tests/E2E/MultiplexingDeadlineTest.php:197

Reviewed the commits since 2bc2913 · Details · Comment @hansi-codes review to re-run, or mention @hansi-codes with a question.

@github-actions

Copy link
Copy Markdown

Security rules

No new WARNING or ERROR findings from security rules.

19 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (12)
  • php.appwrite.secret-compare-timing (5)
  • php.appwrite.weak-secret-env-default (2)

Posted by Checks / Rules. Re-runs update this comment in place. Rule details and baseline: .semgrep/README.md.

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Tier A · Looks good to merge. Summary

@luxor-code-review luxor-code-review Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds lease-aware and multiplexed Redis cache adapters with write-failure telemetry counters, plus an E2E deadline test; one robustness gap found in the telemetry instrumentation path.

Key findings

Comment on lines +145 to +152
#[\Override]
protected function recordWriteFailure(string $operation, Throwable $error): void
{
$this->getWriteFailures()->add(1, [
'operation' => $operation,
'error' => new \ReflectionClass($error)->getShortName(),
]);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 bug · medium

Guard recordWriteFailure against throwing telemetry

recordWriteFailure runs inside every catch (Throwable) write handler and calls $this->telemetry->createCounter(...) / Counter::add(...) unguarded (packages/cache/src/Adapter/Redis/Multiplexing.php:145-152). If the telemetry adapter throws — a failing exporter, or an error on the lazy createCounter first use — the exception escapes save, saveMany, and saveWithLease, breaking the documented contract of reporting a write failure as false. A telemetry outage would then turn silent cache-write degradation into hard exceptions for callers, since Cache::save does not catch adapter exceptions.

📋 Prompt for AI Agents

In packages/cache/src/Adapter/Redis/Multiplexing.php around lines 145-152, wrap the body of recordWriteFailure (the getWriteFailures()->add(...) call) in a try/catch (Throwable) that ignores telemetry errors. The surrounding save/saveMany/saveWithLease catch blocks must still reliably return false even when the telemetry adapter throws.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving this unguarded on purpose. Every instrument in this package records without a guard, including the cache.operation.duration histogram that Cache::save() records in a finally on this same failure path. The telemetry adapters (OpenTelemetry, None, Test) do not throw on add(). Guarding only this one call would not protect the write path and would be inconsistent with the rest of the package.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The consistency argument is noted, but it cuts both ways: if any telemetry adapter ever throws (network-backed exporters, misconfigured test doubles, future adapters), this method will convert a cache write failure into an unhandled exception on the caller's stack. The cache.operation.duration histogram recorded in finally shares the same risk, but that does not reduce the risk here — it multiplies it. Guarding recordWriteFailure with a try/catch (Throwable) is a one-line change that costs nothing and preserves the documented false-return contract unconditionally. The finding remains open.

@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/cache-swallowed-write-errors (after).

Metric Before After Change
🚀 Requests/sec 199.24 197.53 ⚪ -0.9%
⏱️ Latency P50 85.28 ms 88.97 ms ⚪ +4.3%
⏱️ Latency P95 208.14 ms 202.04 ms ⚪ -2.9%
Per-scenario breakdown & investigation details

Metrics below reflect the current branch (after). Δ P95 compares against the base.

Scenario P50 (ms) P95 (ms) Requests RPS Δ P95 (ms)
API total 88.97 202.04 12,483 197.53 -6.11
Account 161.46 305.06 657 11 -30.36
TablesDB 85.95 157.72 6,789 110.08 +0.35
Storage 84.38 185.65 3,285 54.87 +9.16
Functions 122.2 235.33 1,752 29.89 -41.38

Top API waits (after)

API request Max wait (ms)
account.name.update 590.66
functions.create 400.11
storage.buckets.create 384.28
functions.variables.update 378.49
account.get 340.76

The failure counter test only drove save(). saveMany() and
saveWithLease() go through their own catch blocks and operation labels,
so a missing hook or wrong label there would have passed unnoticed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@ghost ghost left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Tier S · Looks good to merge. Summary

@abnegate
abnegate merged commit da76dba into main Oct 19, 2026
78 checks passed
@abnegate
abnegate deleted the fix/cache-swallowed-write-errors branch October 19, 2026 03:23
@ChiragAgg5k ChiragAgg5k mentioned this pull request Oct 21, 2026
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant