Repository navigation
perf(databases): read transaction logs and tenant database once per request - #14749
Conversation
…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]>
|
Review complete. No issues found — approved ✅. This PR caches the first
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 |
🟢 Tier S · Ready to merge
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.
📂 Walkthrough · 3
Reviewed |
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)
|
…tions-latency perf(databases): read transaction logs and tenant database once per request (cherry picked from commit f181a41)
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:TransactionStatekeeps, for the length of the request, the transaction's stored logs and one tenant database per database. Before, eachgetDocument(),stagedView(),listDocuments()andcountDocuments()call fetched the logs again and built a new tenant database throughgetDatabasesDB.Operations\Createbuilds 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
pendingafter the stored logs. Every endpoint that stages writes does so withdbForProjectafter its lastTransactionStateread, 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:168callsTransactionState::getDocument(..., pending: $staged). Onmainthat call:Database(TransactionState.php:70→getDatabasesDB). Since this range,app/init/resources/request.php:1082attaches aMetadatahook 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 withMetadata::resolvePublicId()(Metadata.php:76). That call is an uncachedfindOneondatabase_N, and the collection document's subquery filters add two more queries, onattributesandindexes. Result: 3 extra SQL statements per operation, for API keys too.transactionLogsagain (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 fulltransactionLogsfetch for each update-type operation, andCreate.php:218builds 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 =
main7ef921e (database 8.3.12), fixed = this branch on themainimage. 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: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
Metadatapublic-ID resolution per request, plus one uncached_metadataread that utopia-php/database 8 makes for every write (it also shows up on creates). Both are left out of scope here.Tests
TransactionStateTest::testARequestReadsTheStoredOperationsAndItsDatabaseOnce. It replays what the endpoint does for 6 operations (getDocument(pending)+stagedView(pending)for each) and asserts thattransactionLogsis 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.TransactionStateTestcases pass, includingtestDocumentAnEarlierOperationOfTheSameRequestWritesExists.🤖 Generated with Claude Code