Skip to content

fix(executions): write execution snapshots without ClickHouse async insert - #14652

Merged
levivannoort merged 1 commit into
mainfrom
fix/executions-sync-insert
Oct 19, 2026
Merged

levivannoort merged 1 commit into
mainfrom
fix/executions-sync-insert

Conversation

@ChiragAgg5k

@ChiragAgg5k ChiragAgg5k commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

What does this PR do?

Writes execution snapshots to ClickHouse with async_insert=0, so each insert returns as soon as its part is written instead of waiting for ClickHouse's async insert buffer to flush.

Why

POST /v1/functions/{functionId}/executions returns the execution $id before the execution is stored. The snapshot goes ExecutionScheduled / ExecutionCompleted → Log listener → v1-executions queue → executions worker → Store::upsert(), while GET /v1/functions/{functionId}/executions/{executionId} reads only from the store. Until the worker writes the row, polling the returned id gives 404 execution_not_found. This was reported by a self-hosted 2.5.x user who runs long AI jobs async and polls getExecution for completion.

The write itself is the bottleneck. ClickHouse 26.x ships with async_insert=1 and wait_for_async_insert=1 as server defaults, and Store never overrode them. Every snapshot is one row, so each insert waited out the adaptive busy timeout (50–200 ms) for a flush that had almost nothing to batch:

  • Server-side insert duration: p50 175 ms, of which writing the part took ~2 ms.
  • The executions store pool defaults to 2 connections (_APP_POOL_SIZE_EXECUTIONS), so the worker caps at 2 / 0.175 s ≈ 11 writes/s. Measured drain rate: 11/s.
  • Every execution writes at least two snapshots (pending + final), so above ~5 executions/s the queue grows without bound, and so does the window in which getExecution 404s.

With async_insert=0 the same insert takes 3–5 ms.

Alternatives considered and rejected

  • Writing the pending snapshot synchronously on the API path (closes the window completely). With the default async insert it added ~180 ms to every create call and cut create throughput from ~620 to ~100 req/s. With async_insert=0 it still halved throughput under concurrency. Not worth it for a read-after-write guarantee on one endpoint.
  • Raising the pool size only. This helps throughput but keeps the ~175 ms floor on every snapshot.

Test Plan

Unit

tests/unit/Execution/StoreTest.php::testWritesBypassAsyncInsertBuffer asserts every insert (upsert and delete snapshots) sends async_insert=0. It fails on main (Failed asserting that null is identical to '0') and passes with this change. The full StoreTest passes (21 tests). Pint, PHPStan and Rector are clean.

Benchmark

Setup: stock appwrite/appwrite:2.5.0 docker compose stack (ClickHouse 26.4.3.37, default .env, combined worker) on a 12-CPU / 20 GB Docker host, with a Node 22 function that returns immediately. The patched variant swaps only Store.php inside the same containers. Each phase waits for the functions and executions queues to drain before starting. Rounds were interleaved to limit drift: A was measured twice, D once. The "queued writes" columns show how far the executions worker fell behind by the end of each phase, and how long it took to catch up. That catch-up time is the worst-case window in which getExecution returns 404.

Phase Before: create p50 Before: queued writes → catch-up After: create p50 After: queued writes → catch-up
300 async creates, 1 at a time 5.3 ms 611 → 54 s 6.7 ms 0 → 1 s
1000 async creates, 20 concurrent 25 ms 1,216 → 178 s 34 ms 1,009 → 9 s
150 sync creates, 1 at a time 14 ms 134 → 12 s 14 ms 0 → 0 s
400 sync creates, 10 concurrent 21–60 ms 389 → 35 s 31 ms 58 → 2 s

ClickHouse system.query_log for the same runs (3,270 execution inserts per round):

avg p50 p95 max active parts after the run
Before 172 ms 175 ms 202 ms 684 ms 5
After 4.9 ms 3 ms 11 ms 171 ms 3

Create latency is unchanged within noise: the API path never touched the insert. Merges kept up easily without the buffer, and active parts stayed in single digits.

Create → immediate GET on the same stack, with the race window measured until GET returns 200:

Load Before: first GET 404 Before: visible after p50 / max After: first GET 404 After: visible after p50 / max
1 at a time 20/20 68 / 100 ms 6/30 11 / 50 ms
10 concurrent 40/40 256 / 546 ms 85/100 73 / 179 ms
50 concurrent 72/100 631 / 1,744 ms not run not run

A short window remains, because the snapshot still goes through the queue. But the delay now stays in the tens of milliseconds instead of growing with load to minutes.

Related PRs and Issues

  • Reported by a self-hosted user on Discord: async createExecution returns 202 with an id that getExecution 404s until ClickHouse persistence.

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?

Closes #14654

…nsert

ClickHouse 26.x enables async_insert with wait_for_async_insert by default, so
every one-row execution snapshot waited up to 200ms for a buffer flush. With
the default pool of two connections the executions worker topped out near 11
writes per second, and the queue backed up far enough that getExecution kept
returning 404 for executions the API had already accepted.
@luxor-code-review

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

Copy link
Copy Markdown

Review complete. No issues found — approved ✅.


This PR adds async_insert=0 to the ClickHouse insert URL so snapshot writes skip the ~200ms async flush wait, and adds a unit test verifying both insert requests carry the parameter. All writes flow through insertRows, so deletes and queries are unaffected.

Files Change
src/Appwrite/Execution/Store.php Appends async_insert=0 to the bulk-insert query string to bypass the async insert buffer.
tests/unit/Execution/StoreTest.php Adds testWritesBypassAsyncInsertBuffer asserting async_insert is 0 on captured write requests.

Reviewed commit: e3c8894

@ghost

ghost commented Oct 19, 2026 •

Copy link
Copy Markdown

🟢 Tier S · Ready to merge

The ClickHouse request setting is applied to the snapshot insert path and the regression test covers both snapshot write forms.

Execution snapshot writes now set ClickHouse async_insert=0 so inserts do not wait for the async insert buffer. A unit test checks that both bulk upserts and delete snapshots include this setting.

Verdict New comments Fixed Still open
✅ Approved 0 0 0
📂 Walkthrough · 2
File Change
src/Appwrite/Execution/Store.php Adds async_insert=0 to the ClickHouse snapshot insert request.
tests/unit/Execution/StoreTest.php Adds coverage for the setting on upsert and delete snapshot requests.

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

@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.

@ChiragAgg5k ChiragAgg5k self-assigned this Oct 19, 2026
@github-actions

Copy link
Copy Markdown

✨ Benchmark results

Comparing main (before) → fix/executions-sync-insert (after).

Metric Before After Change
🚀 Requests/sec 193.29 197.06 ⚪ +2%
⏱️ Latency P50 89.7 ms 88.03 ms ⚪ -1.9%
⏱️ Latency P95 210.07 ms 203.58 ms ⚪ -3.1%
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 88.03 203.58 12,540 197.06 -6.49
Account 164.79 311.45 660 11 -11.13
TablesDB 85.28 160.09 6,820 109.35 -2.5
Storage 79.62 179.86 3,300 54.94 -7.63
Functions 128.99 244.53 1,760 30.05 -3.6

Top API waits (after)

API request Max wait (ms)
tablesdb.rows.get 510.4
account.name.update 506.98
functions.variables.update 457.73
functions.create 444.03
tablesdb.rows.delete 417.21

@levivannoort
levivannoort merged commit 02eb14e into main Oct 19, 2026
68 checks passed
@ChiragAgg5k ChiragAgg5k mentioned this pull request Oct 21, 2026
6 tasks
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.

Write execution snapshots without ClickHouse async insert

2 participants