Skip to content

perf(cache): reuse the decoded envelope while the stored string is unchanged - #279

Closed
loks0n wants to merge 1 commit into
mainfrom
perf/cache-decoded-envelopes
Closed

loks0n wants to merge 1 commit into
mainfrom
perf/cache-decoded-envelopes

Conversation

@loks0n

@loks0n loks0n commented Sep 16, 2026 •

Copy link
Copy Markdown
Contributor

Why

Profiling the cloud API (appwrite-labs/cloud#5861) showed json_decode as the top leaf function at 10.5% of busy CPU. Collection metadata, the project and the user are read from Redis on every request and their JSON decoded every time, although the stored string almost never changes between requests.

What

The Redis and multiplexing adapters keep a small per-process memo of decoded envelopes keyed by cache key (DecodedEnvelopes). Redis stays the source of truth: every load still fetches the string, and the decoded value is reused only while the string is byte-for-byte the one it was decoded from. A purge or a save from any process changes the string and the next load decodes again, so the memo cannot serve anything Redis would not.

  • 128 keys per adapter, oldest evicted first, payloads under 1 KB pass straight through (they are cheap to decode and would only churn the memo).
  • The envelope TTL is re-applied on every hit.
  • Envelope::unwrap() is split out of Envelope::decode() so the memo can keep the envelope time.

Measured

Warm cache, 9 KB collection metadata envelope from a local Redis, PHP 8.5, 5k iterations, CPU µs per operation:

before after
Cache::load (telemetry off) 57.2 14.8
Cache::load (OpenTelemetry on) 72.9 22.8
Database::getCollection (telemetry off) 92.3 56.2
Database::getDocument (OpenTelemetry on) 174.5 121.1

The string compare on a hit is within noise of the bare HGET (10.4 vs 10.3 µs). The OpenTelemetry rows include #280 (breaker gauges observed at collect, no exemplars); the telemetry-off rows isolate this change.

Memory: a memoized 9 KB envelope holds the string plus its decoded array, so the bound is roughly 128 × (payload + decoded size) per adapter instance, per worker process.

🤖 Generated with Claude Code

…changed

Profiling the cloud API showed json_decode as the top leaf function (10.5%
of busy CPU): collection metadata, the project and the user are read from
Redis on every request and decoded every time, although the stored string
almost never changes between requests.

The Redis and multiplexing adapters now keep a small per-process memo of
decoded envelopes keyed by cache key. Redis stays the source of truth: every
load still fetches the string, and the decoded value is reused only while
the string is byte-for-byte the one it was decoded from, so a purge or a
save from any process is seen on the next load. The memo holds 128 keys,
evicts the oldest, skips payloads under 1 KB, and re-applies the envelope
TTL on every hit.

Measured on a 9 KB collection metadata envelope from a local Redis (5k
iterations, PHP 8.5): a cache load costs 57 µs CPU before and 15 µs after
with telemetry off; the string compare on a hit is within noise of the bare
HGET. Through Database::getCollection that is 92 µs to 56 µs.

Refs appwrite-labs/cloud#5861.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@ChiragAgg5k

Copy link
Copy Markdown
Member

packages/cache now lives in appwrite/appwrite; ported as appwrite/appwrite#13915. Thanks!

This branch had an error being deployed

1 failed deployment
vcs — fcfb66f0 Deployed Sep 16, 2026 by loks0n via test (vcs, linked) #972
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.

2 participants