Repository navigation
Description
In short: The usage worker handles one job per API request. For every job it loads the project from the database and sends a separate insert to ClickHouse, so one worker process tops out at about 1,000 jobs per second.
Impact:
- Above β 1,000 requests/s the usage queue grows without bound in Redis, where it pushes out the cache (π Bug Report: Usage queue backlog evicts the cache from RedisΒ #14084).
- Taking the project from the job payload and batching the inserts gave β 3Γ the throughput (2,800β3,300 jobs/s) with less CPU, and all data arrived in ClickHouse.
- The change is limited to
StatsUsage.php(about 40 lines).
π Reproduction steps
- Run Appwrite 2.3.0 self-hosted (Postgres) with the default
appwrite-worker(one process,stats-usagewith 8 coroutines). - Send sustained API traffic above ~1 000 requests/s, e.g.
GET /v1/tablesdb/{db}/tables/{table}/rows/{row}with 80 concurrent connections. - Watch
LLEN utopia-queue.queue.v1-stats-usagein Redis.
π Expected behavior
One usage worker keeps up with the request rate one API node can serve, or the queue at least drains quickly after a spike.
π Actual Behavior
Every API request produces one usage job. One worker process drains about 1 000 jobs/s (954β1 052 in four runs). Above that the queue grows without bound in Redis, where it competes with the cache (#14084). Two API nodes with 4 vCPU each serve β 2 500 req/s in our setup, so the queue grows by β 1 500 jobs every second under full load.
A CPU profile of the worker (excimer, β 28 000 jobs) shows where the time goes:
| Part | Share of worker CPU (inclusive) |
|---|---|
Load the project per job (app/init/worker/message.php, getDocument('projects', β¦)) |
32.8 % |
Accumulator::flush() per job, i.e. one HTTP insert to ClickHouse per job |
28.8 % |
ClickHouse::addBatch (columns, formatting) |
28.2 % |
| Queue handling (buffer, JSON decode, Redis) | 10.9 % |
Two things in StatsUsage cause most of it:
- The project is loaded from the database for every job.
StatsUsageinjectsproject(StatsUsage.php#L33), which calls$dbForPlatform->getDocument('projects', β¦)per message. The worker only needs the project's$idand$sequence, and both are already in the payload (Message\Usage::toArray(), available as theprojectContextresource). - Every job is flushed on its own.
action()creates a newAccumulatorper job (L63) and flushes it at the end (L126), so each job becomes one HTTP request to ClickHouse.Utopia\Usage\Accumulatoris designed to buffer and fold manycollect()calls and flush them in one batch (its doc block says so), but the worker never uses it that way.
Measured. Same worker (1 process, 8 coroutines) with a patched StatsUsage.php, β 35 000 jobs backlog each, two runs per variant. Completeness: sum of network.requests in ClickHouse against successful requests.
| Variant | Drain rate | Worker CPU | Data complete |
|---|---|---|---|
| 2.3.0 | 954 / 1 028 jobs/s | 73 / 79 % | yes |
Project from payload (projectContext) |
956 / 1 049 jobs/s | 49 / 58 % | yes |
| One accumulator per process, flush at 500 entries or 1 s | 1 864 / 1 718 jobs/s | 95 / 99 % | yes |
| Both | 3 270 / 2 789 jobs/s | 59 / 77 % | yes |
- With one insert per job, the worker mostly waits for ClickHouse. Skipping the project lookup saves CPU but not time.
- Batching alone almost doubles the rate, then the process is CPU-bound by the project lookup.
- Both together give β 3Γ the rate with less CPU than today.
π² Appwrite version
2.3.0
π» Operating system
Linux (Debian 12), Docker
π§± Your Environment
Self-hosted, Postgres adapter, ClickHouse for usage (async_insert on). API on two nodes with 4 vCPU each, Postgres/Redis/ClickHouse on a third node, worker on one API node.
π‘ Suggested fix
- Inject
projectContextinstead ofprojectinStatsUsageand use$projectContext->id/$projectContext->sequence. No database access is needed for usage events. - Keep one
Accumulatorper worker process and flush it when it holds N entries (we used 500) or is older than ~1 s, plus a timer so a quiet queue still flushes. Swap in a new accumulator before flushing, so coroutines that collect during the HTTP insert don't fold into entries that are about to be removed.
The patch we tested is about 40 lines (StatsUsage.php only).
Since when / regression?
Since usage moved to ClickHouse with Utopia\Usage (2.x). Not a regression of a single release.
Risks / things to consider for the fix
- With batching, a job counts as done before its data is written. A crash or restart of the worker loses up to the flush interval (β 1 s) of usage. The current code already treats usage as best-effort and does not retry failed inserts.
- On shutdown the buffer should be flushed (we did not implement that in the test patch).
- Our test traffic came from one IP, one user agent and one path, so a batch folded into β 10 rows. Real traffic has more distinct tags, so batches carry more rows and
addBatchformatting costs more. The saving from fewer HTTP inserts and no project lookup stays.
π Have you spent some time to check if this issue has been raised before?
- I checked and didn't find a similar issue (related: π Bug Report: Usage queue backlog evicts the cache from RedisΒ #14084, π Bug Report: Pool adapter checks out a connection for every capability getterΒ #14083)
π’ Have you read the Code of Conduct?
- I have read the Code of Conduct
Activity
JoshiJoshiJoshi commentedon Oct 4, 2026
Related work I missed when filing this (my duplicate search was broken, sorry):
The measurements above may help with both: on 2.3.0, batching alone took the worker from β 1 000 to β 1 800 jobs/s, using the queued project context alone cut worker CPU by β 25 points without changing throughput, and both together gave β 2 800β3 300 jobs/s with all data arriving in ClickHouse. So the two changes are worth more together than either one alone.
[-]π Bug Report: `stats-usage` worker processes only ~1 000 jobs/s because it loads the project from the database and sends one ClickHouse insert per job; batching both gives ~3Γ throughput[/-][+]π Bug Report: `stats-usage` worker limited to ~1,000 jobs/s[/+]HarshMN2345 commentedon Oct 12, 2026
Fixed in #14106, #14291.