Skip to content

perf(databases): read transaction logs and tenant database once per request - #14749

Merged
abnegate merged 1 commit into
mainfrom
fix/transaction-operations-latency
Oct 22, 2026
Merged

abnegate merged 1 commit into
mainfrom
fix/transaction-operations-latency

Conversation

@abnegate

@abnegate abnegate commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

What

Staging operations (POST /v1/tablesdb/transactions/:transactionId/operations, same action for /v1/databases/...) read the same data again for every operation in the request. This PR reads it once per request instead:

  • TransactionState keeps, for the length of the request, the transaction's stored logs and one tenant database per database. Before, each getDocument(), stagedView(), listDocuments() and countDocuments() call fetched the logs again and built a new tenant database through getDatabasesDB.
  • Operations\Create builds one tenant database per database, not one per operation.

Read-your-own-writes inside a request still works the same way: operations staged earlier in the request are still replayed as pending after the stored logs. Every endpoint that stages writes does so with dbForProject after its last TransactionState read, so caching the stored logs for one request never serves stale data.

Why (prod)

In fra1, the mean latency of this route stepped up from ~100–115 ms to ~141–159 ms (+45%) in the 2026-10-16 06:00–12:00 UTC window and has stayed there. sfo3 stepped from ~0.22 s to 0.27 s at the same time. The step lines up with the cloud deploy of 4a9d3f258c (server-ce 939fa5e → 216111d, utopia-php/database 7.4.2 → 8.3.0). Prod requests are ~1.2 KB, which means a few small operations each. Other TablesDB routes did not step.

Root cause (measured, not assumed)

For each update, upsert, increment or decrement operation, Operations/Create.php:168 calls TransactionState::getDocument(..., pending: $staged). On main that call:

  1. builds a new tenant Database (TransactionState.php:70 → getDatabasesDB). Since this range, app/init/resources/request.php:1082 attaches a Metadata hook to every new database. The hook's memo of public collection IDs belongs to that one instance. The first decorated read therefore resolves the collection's public ID with Metadata::resolvePublicId() (Metadata.php:76). That call is an uncached findOne on database_N, and the collection document's subquery filters add two more queries, on attributes and indexes. Result: 3 extra SQL statements per operation, for API keys too.
  2. fetches all of the transaction's transactionLogs again (TransactionState.php:735, limit PHP_INT_MAX). This step also existed before the upgrade.

Session and JWT callers also run stagedView() per operation (Create.php:229). That does a second full transactionLogs fetch for each update-type operation, and Create.php:218 builds another tenant database for each operation.

Measurements

Local A/B setup: MariaDB 10.11 with Redis cache, one stack at a time. old = 939fa5e (database 7.4.2), new = main 7ef921e (database 8.3.12), fixed = this branch on the main image. For each cell, 30 sequential samples after 3 warm-ups, every sample in a fresh transaction holding the given number of stored operations. SQL counts come from the MariaDB general log for one request (application user only). p50 / p95 in ms:

caller action ops/request staged old new fixed SQL old / new / fixed
API key update 1 0 5.7 / 7.2 6.9 / 8.2 7.2 / 8.2 12 / 16 / 16
API key update 10 0 12.9 / 15.7 20.1 / 22.1 10.8 / 13.9 21 / 52 / 16
API key update 10 100 33.2 / 37.6 30.9 / 33.6 12.1 / 13.5 21 / 51 / 15
API key update 50 0 42.1 / 44.1 77.9 / 93.4 21.5 / 24.3 61 / 212 / 16
API key update 50 500 527.4 / 556.4 363.6 / 378.0 40.9 / 54.0 61 / 212 / 16
API key increment 10 0 12.6 / 14.1 20.0 / 22.7 9.8 / 12.0 21 / 52 / 16
API key create 10 0 6.0 / 7.1 6.9 / 9.4 7.2 / 10.0 11 / 13 / 13
session update 10 0 12.9 / 13.7 24.3 / 27.9 10.8 / 11.6 21 / 62 / 16
session update 10 500 119.1 / 140.7 143.7 / 166.1 22.7 / 24.0 21 / 62 / 16
session upsert 50 100 146.4 / 160.4 212.7 / 222.9 36.1 / 43.3 61 / 261 / 15
session create 10 0 6.2 / 8.7 8.1 / 9.1 7.6 / 9.9 11 / 13 / 13
session, relationship table create 10 0 12.6 / 77.7 25.1 / 26.2 12.7 / 15.6 11 / 62 / 26
session, relationship table update 10 0 21.4 / 24.3 51.6 / 57.0 21.2 / 24.4 41 / 152 / 52

On main, an update-type operation costs about 4 SQL statements (old: 1). After this PR the whole request costs a constant 16, however many operations it carries. The full matrix covers caller {key, session} × action {create, update, upsert, increment} × ops {1, 10, 50} × staged {0, 100, 500}, plus a relationship-table variant. Across it, fixed is at or below old in every multi-operation update-type cell. Creates without relationships were never affected.

Still above old after this PR: a request with a single update-type operation (12 → 16 SQL, ~+1.5 ms locally). That comes from the one remaining Metadata public-ID resolution per request, plus one uncached _metadata read that utopia-php/database 8 makes for every write (it also shows up on creates). Both are left out of scope here.

Tests

  • New TransactionStateTest::testARequestReadsTheStoredOperationsAndItsDatabaseOnce. It replays what the endpoint does for 6 operations (getDocument(pending) + stagedView(pending) for each) and asserts that transactionLogs is read once and the tenant database is built once. Without the fix it fails with 13 reads. It also asserts that earlier operations of the same request are still visible.
  • All existing TransactionStateTest cases pass, including testDocumentAnEarlierOperationOfTheSameRequestWritesExists.

🤖 Generated with Claude Code

…equest

Staging operations got ~45% slower in fra1 after the database 8 upgrade.
Every update, upsert, increment and decrement operation read the
transaction's stored logs again, and session callers read them a second
time for the staged permission view. Each read also built a new tenant
database through getDatabasesDB, so the Metadata hook resolved the
collection's public ID with an uncached catalog query (plus its
attributes and indexes subqueries) once per operation, for API keys too.

TransactionState now keeps the stored logs and the tenant database per
request, and the operations endpoint reuses one tenant database per
database. Operations of the same request are still replayed as pending,
so later operations keep seeing earlier ones. Endpoints only stage their
writes after their last read, so a request never needs a fresher copy.

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

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

Copy link
Copy Markdown

Review complete. No issues found — approved ✅.


This PR caches the first transactionLogs query result on each TransactionState instance and concatenates later-pending operations onto that snapshot, cutting repeated database reads while replaying staged operations in the transactions Create endpoint. A unit test pins down that repeated reads reflect staged operations added between reads.

Files Change
src/Appwrite/Databases/TransactionState.php Memoizes the stored-logs query per instance and replays the cached snapshot plus pending operations.
.../Transactions/Operations/Create.php Consumes the memoized state path when validating and applying new staged operations.
tests/unit/Databases/TransactionStateTest.php Adds coverage asserting repeated reads see newly staged pending operations.

One low-severity, sub-threshold note: the memoization makes each request see a snapshot, so operations committed concurrently to the same transaction by a sibling request are no longer visible on later reads — worth a docblock note, but safe for single-client usage.

Reviewed commit: 72111f1

@ghost

ghost commented Oct 22, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The caches are request-scoped and pending operations remain replayed after stored logs; I found no correctness defects in the changes.

The PR caches stored transaction logs and tenant database instances for the lifetime of a request, while continuing to replay pending operations for read-your-own-writes. It also reuses one tenant database per database in transaction-operation staging and adds a regression test for the request-scoped caches.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 3
File Change
src/Appwrite/Databases/TransactionState.php Caches stored transaction logs and tenant database instances by transaction/database sequence.
src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Operations/Create.php Reuses a tenant database for operations targeting the same database.
tests/unit/Databases/TransactionStateTest.php Adds coverage for single-fetch request caching and visibility of earlier pending operations.

Reviewed 72111f1 · 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 S · Looks good to merge. Summary

@github-actions

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/transaction-operations-latency (after).

Metric Before After Change
🚀 Requests/sec 205.21 207.12 ⚪ +0.9%
⏱️ Latency P50 84 ms 82.96 ms ⚪ -1.2%
⏱️ Latency P95 198.95 ms 196.55 ms ⚪ -1.2%
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 82.96 196.55 12,882 207.12 -2.4
Account 155.7 290.28 678 11.22 +9.3
TablesDB 80.1 153.79 7,006 114.83 -12.59
Storage 76.72 180.64 3,390 57.74 -8.48
Functions 116.52 222.23 1,808 31.37 +6.63

Top API waits (after)

API request Max wait (ms)
storage.buckets.create 494.77
functions.variables.update 460.9
account.name.update 460.65
storage.files.preview 430.9
tablesdb.rows.create 419.65

@abnegate
abnegate merged commit f181a41 into main Oct 22, 2026
39 checks passed
@abnegate
abnegate deleted the fix/transaction-operations-latency branch October 22, 2026 05:35
Tapraj-Pathak pushed a commit to Tapraj-Pathak/appwrite that referenced this pull request Oct 24, 2026
…tions-latency

perf(databases): read transaction logs and tenant database once per request

(cherry picked from commit f181a41)
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