Skip to content

πŸ› Bug Report: stats-usage worker limited to ~1,000 jobs/sΒ #14086

Closed

Description

@JoshiJoshiJoshi

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

  1. Run Appwrite 2.3.0 self-hosted (Postgres) with the default appwrite-worker (one process, stats-usage with 8 coroutines).
  2. Send sustained API traffic above ~1 000 requests/s, e.g. GET /v1/tablesdb/{db}/tables/{table}/rows/{row} with 80 concurrent connections.
  3. Watch LLEN utopia-queue.queue.v1-stats-usage in 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:

  1. The project is loaded from the database for every job. StatsUsage injects project (StatsUsage.php#L33), which calls $dbForPlatform->getDocument('projects', …) per message. The worker only needs the project's $id and $sequence, and both are already in the payload (Message\Usage::toArray(), available as the projectContext resource).
  2. Every job is flushed on its own. action() creates a new Accumulator per job (L63) and flushes it at the end (L126), so each job becomes one HTTP request to ClickHouse. Utopia\Usage\Accumulator is designed to buffer and fold many collect() 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

  1. Inject projectContext instead of project in StatsUsage and use $projectContext->id / $projectContext->sequence. No database access is needed for usage events.
  2. Keep one Accumulator per 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 addBatch formatting 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?

🏒 Have you read the Code of Conduct?

  • I have read the Code of Conduct

Activity

  1. added
    product / databases
    product / messaging
    product / self-hosted
    product / storage
    product / vcs
    on Oct 4, 2026
  2. JoshiJoshiJoshi commented on Oct 4, 2026

    @JoshiJoshiJoshi
    Author

    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.

  3. changed the title [-]πŸ› 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[/+] on Oct 4, 2026
  4. HarshMN2345 commented on Oct 12, 2026

    @HarshMN2345
    Member

    Fixed in #14106, #14291.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    product / databasesproduct / messagingproduct / self-hostedproduct / storageproduct / vcs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions