Skip to content

fix(databases): refresh cached lists when document permissions change - #14869

Merged
abnegate merged 10 commits into
mainfrom
fix/list-cache-document-permissions
Oct 27, 2026
Merged

abnegate merged 10 commits into
mainfrom
fix/list-cache-document-permissions

Conversation

@abnegate

Copy link
Copy Markdown
Member

What does this PR do?

On collections with document security, writes that set document permissions now purge the collection's cached lists: single update and upsert, bulk update and upsert carrying $permissions, and committed transaction operations. A cache failure during transaction commit is logged rather than failing the commit.

Test Plan

New e2e testListDocumentsCacheBustedByDocumentPermissionChange covering single update, bulk update and a committed transaction, plus the existing cache, bulk, upsert, permission and transaction suites on Legacy and TablesDB. Lint and PHPStan are clean.

Related PRs and Issues

  • None

Checklist

  • Have you read the Contributing Guidelines on issues?
  • If the PR includes a change to an API's metadata (desc, label, params, etc.), does it also include updated API specs and example docs?

🤖 Generated with Claude Code

On collections with document security, writes that set document permissions now purge the collection's cached lists: single update and upsert, bulk update and upsert carrying $permissions, and committed transaction operations. A cache failure during transaction commit is logged rather than failing the commit.

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

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

Copy link
Copy Markdown

Review complete. No blocking issues — approved ✅; 2 nitpicks below.

🧹 Nitpicks (2) — 🟢 2 low
  • 🟢 Transactional commits purge cache on every update (Update.php:632) — setsPermissions() matches 'update', 'upsert' => isset($data['$permissions']) (Transactions/Update.php:632-640), but the staging paths always write that key: Collections/Documents/Update.php:187 sets $data['$permissions'] = $permissions even when the caller passed none and the value is just the document's existing permissions, and Collections/Documents/Upsert.php:203 does the same.
  • 🟢 Inconsistent handling of failed permission-cache purge (Action.php:280) — The new permission-change cache purge is handled inconsistently across the five write paths: Transactions/Update.php:468-472 catches a purge failure and only logs a warning, while the direct write paths call purgeListCacheForPermissions unguarded (Collections/Documents/Update.php:306-308, Collections/Documents/Upsert.php:334-336, Bulk/Update.php:234-236, Bulk/Upsert.php:218-220), so a failure there throws after the write has already committed.

This PR introduces permission-change-aware list-cache invalidation for DocumentsDB: when a caller supplies $permissions on document writes, the collection's cached list responses are purged so revoked callers stop seeing stale rows. The purge is wired through the shared Databases/Action helper plus the single update/upsert, bulk update/upsert, and transaction commit paths, and is covered by a new e2e test asserting cache bust on permission revocation.

Files Change
src/Appwrite/Platform/Modules/Databases/Http/Databases/Action.php Adds purgeListCacheForPermissions helper that purges the collection list cache when permissions were supplied and the collection is documentSecurity.
.../Collections/Documents/Update.php, .../Upsert.php Purge after single-document writes when the caller supplied permissions.
.../Collections/Documents/Bulk/Update.php, .../Bulk/Upsert.php Purge after bulk writes; runs post-try/catch, so failure paths skip it.
.../Transactions/Update.php Classifies commit actions via setsPermissions and purges during transaction commit; purge failure is logged, not fatal.
tests/e2e/Services/Databases/DatabasesBase.php New testListDocumentsCacheBustedByDocumentPermissionChange covering update, bulk, and transaction revocation with cache hit/miss assertions.

Reviewed commit: e5684f3

@ghost

ghost commented Oct 23, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

This pull request invalidates cached document-list responses when document permissions change through single or bulk writes and committed transactions. List requests capture a cache version before querying and save their results only if that version is still current; version markers now also expire. E2E and unit tests cover invalidation, races, cache failures, and marker expiration.

Latest changes: The newest commit gives each list-cache version marker a 24-hour TTL and adds a unit test that verifies Redis expires the marker.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 11
File Change
src/Appwrite/Databases/ListCache.php Adds version-guarded list-cache saves, purges around permission-changing writes, and a TTL for version markers.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Action.php Provides shared cache lookup and permission-purge handling for database actions.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Update.php Purges list caches around bulk updates that set document permissions.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Upsert.php Purges list caches around bulk upserts that set document permissions.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Update.php Purges list caches around single-document updates that set permissions.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Upsert.php Purges list caches around single-document upserts that set permissions.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/XList.php Captures the list-cache version before querying and saves results only if it remains unchanged.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php Purges list caches for collections affected by committed permission changes.
tests/e2e/Services/Databases/DatabasesBase.php Covers list-cache invalidation after single, bulk, and transaction permission changes.
tests/unit/Databases/ListCacheTest.php Tests version-guarded cache saves, purge behavior, failure handling, and version-marker expiration.
tests/unit/Databases/Outage.php Adds batch cache operations to the outage adapter used by list-cache tests.

Merged main (244bea4); no changes to this PR's code since 21f9811 · 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.

6 existing findings tracked in .semgrep/baseline.json
  • php.appwrite.guest-write-without-abuse-limit (3)
  • php.appwrite.secret-compare-timing (1)
  • 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 previously requested changes Oct 23, 2026

@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 B · 1 blocking finding to address. Summary

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/list-cache-document-permissions (after).

Metric Before After Change
🚀 Requests/sec 217.67 225.91 ⚪ +3.8%
⏱️ Latency P50 76.88 ms 77.35 ms ⚪ +0.6%
⏱️ Latency P95 188.24 ms 175.62 ms 🟢 -6.7%
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 77.35 175.62 13,965 225.91 -12.62
Account 140.59 261.5 735 12.2 -34.72
TablesDB 75.75 141.17 7,595 124.74 +6.53
Storage 69.62 158.98 3,675 61.9 -15.3
Functions 104.38 197.34 1,960 33.75 -55.75

Top API waits (after)

API request Max wait (ms)
account.name.update 431.69
account.prefs.update 374.03
tablesdb.rows.increment 369.65
tablesdb.rows.update 343.98
storage.buckets.create 330.21

abnegate and others added 5 commits October 23, 2026 16:52
…purging after

Purging the cached lists after a permission write committed had two
failure modes. Where the purge was uncaught, a cache outage returned an
error for a write that was already stored and skipped its events. Where
it was caught, the stale entries survived and were served again once
the cache recovered.

A write that sets document permissions on a collection with document
security now runs behind a fence on that collection's list cache:

- Before the write, it registers a token in a sibling hash and then
  purges the list hash, which also advances the hash's generation. If
  either step fails, the write is refused before anything is stored.
- While any token is live, lists are still read but nothing is saved.
- A list saves its result with saveWithLease under the generation it
  read before checking the fences, so a read that raced the purge is
  dropped rather than cached.
- After the write, the token is released. A failed release is logged
  and never fails the stored write. Its token lapses after 300 seconds,
  during which lists are served uncached.

No cache failure can leave a pre-write list servable: the purge either
happened before the write or the write never ran. The one assumption is
that no write runs longer than the fence lapse.

Single and bulk update and upsert, and transaction commits, all fence
the same way. A commit fences every collection one of its operations
sets permissions in before the database transaction starts.

The e2e test now also covers single upsert, bulk upsert and a
transaction that commits a bulk upsert. ListCacheTest covers the fence
against a Redis cache that goes down mid-write.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ons' into fix/list-cache-document-permissions
…ache

A fence that cannot be registered refuses the write before it is stored.
Raise it as Utopia\Cache\Exception\Unavailable so the error hook answers
503 general_cache_unavailable, which clients may retry, instead of a 500.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@ghost
ghost dismissed their stale review October 23, 2026 04:19

The findings that requested changes are resolved. See the summary comment for what is still open.

@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 · See the inline comments. Summary

Comment thread src/Appwrite/Databases/ListCache.php Resolved
Replace the fence, lease and generation machinery with two purges
around the write, which is simpler and adds no Redis calls to the list
read path.

- Before a write that sets document permissions on a collection with
  document security, its cached lists are purged. If the purge fails,
  the write is refused before anything is stored, with 503
  general_cache_unavailable. A cache outage therefore cannot leave a
  list cached before the write.
- After the write, the lists are purged again, best-effort. A failure is
  logged, and the stored write still succeeds and runs its events. This
  purge also clears a list a concurrent reader cached between the first
  purge and the commit.
- A transaction commit purges every affected collection before the
  database transaction starts. If that fails, the commit fails like any
  other commit error.

Accepted residual window: a list cached between the first purge and the
commit survives only if the second purge also fails, and then lives
until its TTL.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ghost
ghost previously requested changes Oct 23, 2026

@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 B · 1 blocking finding to address. Summary

Comment thread src/Appwrite/Databases/ListCache.php Resolved
…took

Purging before and after a permission write does not stop a list
request that missed the cache before or during the write from saving
its pre-write rows after the last purge. The revoked rows would then be
served until the entry's TTL.

Every list cache purge now also writes a new version to a sibling key.
On a cache miss, a list request reads the version before it queries.
After the query, it saves the result only if the version is unchanged.
Otherwise it returns the rows without caching them. If the version
cannot be read, the request is served uncached. Cache hits make no
extra calls.

Accepted leftover: a purge that lands between the version re-check and
the save is not seen, so that save can still survive.

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 A · Looks good to merge. Summary

Comment thread src/Appwrite/Databases/ListCache.php Resolved
@ghost
ghost dismissed their stale review October 23, 2026 21:32

The findings that requested changes are resolved. See the summary comment for what is still open.

@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 dbed8ba into main Oct 27, 2026
39 checks passed
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