Skip to content

fix(databases): claim a transaction commit atomically - #14163

Merged
HarshMN2345 merged 3 commits into
mainfrom
fix/tablesdb-concurrent-commit
Oct 6, 2026
Merged

HarshMN2345 merged 3 commits into
mainfrom
fix/tablesdb-concurrent-commit

Conversation

@HarshMN2345

@HarshMN2345 HarshMN2345 commented Oct 6, 2026 •

Copy link
Copy Markdown
Member

Fixes #14142

Committing a transaction checked status === 'pending' on a plain read and later wrote committing with an unconditional updateDocument. Concurrent PATCH {commit: true} requests on the same transaction all passed the check and all ran the staged operations, so an increment was applied once per request. With a create op, the losing requests hit a 409 conflict and their catch blocks then marked the winner's transaction failed, even though its row had been written.

The pending to committing transition now happens inside $dbForProject->withTransaction(), after re-reading the transaction with getDocument(..., forUpdate: true). If the status is no longer pending, the request throws TRANSACTION_NOT_READY. The claim sits before the try block, so a request that loses it can no longer reach the catch blocks that mark the transaction failed. The TablesDB, DocumentsDB and VectorsDB transaction update actions extend this class, so all four APIs are covered. One visible change: for a create op, the losing requests now get 400 transaction_not_ready instead of 409 transaction_conflict, and the winner's transaction stays committed.

Checked against a local API container running this branch on Postgres, with a fresh transaction per round and N parallel commit requests:

Case Before (main) After
Increment, 4 parallel Rounds 2 and 3: four 200s, n=4 (round 1 happened to serialize, n=1) Every round: one 200, three 400 transaction_not_ready, n=1, status committed
Create, 4 parallel One 200, three 409 transaction_conflict; row created but transaction ended failed One 200, three 400 transaction_not_ready, status committed
Increment, 8 parallel, 3 rounds One 200 per round, n=1, committed

testConcurrentCommit in TransactionsBase sends four parallel commits of one staged increment and asserts one 200, three 400 transaction_not_ready, a committed status and a single increment. Against main's Update.php it failed 6 of 6 runs ([200,200,200,200] or [200,200,400,400]); with this change it passed every run of the TablesDB, Legacy and DocumentsDB server, client and console suites.

Only Postgres and a single API node were exercised. The lock is a database row lock, so multiple API nodes should behave the same. Two cases are not covered here: a rollback racing a commit can still overwrite it, because the rollback branch writes failed without re-checking the status; and concurrent commits of an empty transaction (no operations) still all return 200, which applies nothing twice.

The pending to committing transition now runs inside a database transaction that re-reads the transaction row with a lock, so only one concurrent commit request applies the staged operations. Requests that lose the claim fail with TRANSACTION_NOT_READY before they can mark the winner's transaction failed.
@ghost

ghost commented Oct 6, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The added regression test addresses the previous coverage finding, and no new defects were found in the incremental changes.

The transaction commit action now claims pending transactions atomically with a locked re-read, rejecting competing commits before they can enter failure handlers. The shared action covers all four database APIs. An E2E regression test stages an increment, sends four parallel commits, and checks that only one succeeds and the stored counter increases once.

Latest changes: The newest commit adds an E2E test for concurrent commits of the same transaction, asserting one success, three transaction_not_ready errors, a committed response, and a single persisted increment.

Verdict New comments Fixed Still open
✅ Approved 0 1 0
📂 Walkthrough · 2
File Change
src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php Claim pending transactions under a database row lock before executing operations and reject losing requests outside failure handlers.
tests/e2e/Services/Databases/Transactions/TransactionsBase.php Add parallel same-transaction commit coverage checking response outcomes and the persisted increment.
✅ Fixed since the last review · 1
  • Add an API regression test for competing commits · src/Appwrite/Platform/Modules/Databases/Http/Databases/Transactions/Update.php:191

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

@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

Four parallel commits of one staged increment must leave exactly one success, reject the rest with transaction_not_ready and apply the increment once.
@github-actions

github-actions Bot commented Oct 6, 2026 •

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/tablesdb-concurrent-commit (after).

Metric Before After Change
🚀 Requests/sec 194.62 183.64 🔴 -5.6%
⏱️ Latency P50 88.61 ms 94.17 ms 🔴 +6.3%
⏱️ Latency P95 211.92 ms 223.79 ms 🔴 +5.6%
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 94.17 223.79 11,400 183.64 +11.87
Account 184.61 365.33 600 10.41 +36.69
TablesDB 91.31 179.41 6,200 103.12 +15.56
Storage 84.59 190.08 3,000 51.63 +16.15
Functions 132.42 269.09 1,600 28.27 +11.02

Top API waits (after)

API request Max wait (ms)
account.name.update 621.85
functions.variables.update 437.31
functions.create 433.14
functions.delete 419.53
account.prefs.update 405.31

@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

@HarshMN2345
HarshMN2345 merged commit 1966d5e into main Oct 6, 2026
2 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.

🐛 Bug Report: Concurrent commits apply a transaction more than once

2 participants