Repository navigation
Description
👟 Reproduction steps
- Table with a string column
status, create a row withstatus = "open". - Create a transaction and stage
{"action": "update", "rowId": <row>, "data": {"status": "done"}}. - Start a few clients that keep calling
GET /v1/tablesdb/{db}/tables/{table}/rows/{row}(any API node). - Commit the transaction (
PATCH /v1/tablesdb/transactions/{id},commit: true) →200. - Stop the readers and call
GET …/rows/{row}again.
Script used: 6 readers over two API nodes, commit after 0.3 s, then 20 reads 0.2 s after the commit returned; 30–40 rounds with a fresh row each.
👍 Expected behavior
After the commit returned 200, getRow returns status = "done".
👎 Actual Behavior
In some rounds every following getRow keeps returning the old row, while the database already has the new value:
| rounds | rounds with stale getRow |
stale after 5 s / 300 s | |
|---|---|---|---|
| transaction commit (run 1) | 30 | 2 (20/20 reads stale) | 2 / – |
| transaction commit (run 2, with DB check) | 40 | 5 (20/20 reads stale) | 5 / 5 – in all 5 rounds listRows with equal $id returned done while getRow still returned open after 300 s |
plain updateRow (same setup) |
30 | 0 | 0 |
The stale entry stays until the next write to that row or until the document cache TTL (24 h, Database::TTL) expires.
In a real workload this broke a "status change + counter in one transaction" pattern: clients that re-read the row after staging (to verify it is still open) got the cached pre-commit row and counted twice.
Cause (as far as I can see):
Database::updateDocument()purges the document cache inside its transaction and again after commit "so readers cannot re-cache the pre-commit version" (Database.phpL6551).- The transaction commit endpoint runs all staged operations inside one outer
$dbForDatabases->withTransaction(...)(Transactions/Update.phpL222). The innerwithTransactionofupdateDocumentis nested (aSAVEPOINT), so its "after commit" purge runs before the realCOMMIT. - A
getRowarriving between that purge and the outerCOMMITmisses the cache, reads the still-committed old row from Postgres and stores it (presumably the lease/generation check ofsaveWithLeasepasses because the read started after the purge; not verified in detail). - Nothing purges the touched rows after the outer commit, so the old version stays cached.
Plain updateRow is not affected because there the inner transaction is the outermost one.
🎲 Appwrite version
2.3.0 (utopia-php/database 7.3.11). utopia-php/database 7.4.0 / main (used by Appwrite main) have the same code.
💻 Operating system
Linux (Debian 12), Docker
🧱 Your Environment
Self-hosted Appwrite 2.3.0, Postgres adapter (PostgreSQL 18), Redis cache, two API nodes.
💡 Suggested fix
Purge the cache of every document touched by the commit after the outer withTransaction() returned (Appwrite already loops over the operations right after the commit for events), or let utopia defer "purge after commit" until the outermost transaction commits (e.g. collect keys while inTransaction > 1 and purge them in commitTransaction() at depth 0).
👀 Have you spent some time to check if this issue has been raised before?
- I checked and didn't find a similar issue
🏢 Have you read the Code of Conduct?
- I have read the Code of Conduct
Activity
fix(databases): purge related rows after a transaction commit
fix(databases): purge rows removed by a cascaded delete