Skip to content

Queue mode

One qaechi-parser serve process parses jobs in the background, which is enough for a proof of concept. For steady, high volume, switch to queue mode: the HTTP API only accepts jobs and publishes them to Kafka, and a pool of workers does the parsing.

flowchart LR
  app[Your app] -- POST /jobs --> api[qaechi-parser serve<br/>QAECHI_QUEUE=kafka]
  other[Outbox, batch job,<br/>any producer] -. job message .-> jobs
  api -- job message --> jobs[(qaechi.parse.jobs)]
  jobs --> w1[worker process 1]
  jobs --> w2[worker process 2]
  jobs --> wn[worker process N]
  w1 & w2 & wn --> db[(PostgreSQL<br/>jobs and results)]
  api -- GET /jobs, /results --> db
  w1 & w2 & wn -- result event --> results[(qaechi.parse.results)]
  w1 & w2 & wn -. unreadable .-> dlq[(qaechi.parse.jobs.dlq)]

Turn it on

pip install "qaechi-parser[parse,server,kafka,postgres]"

export QAECHI_DATABASE_URL=postgresql://qaechi:secret@db:5432/qaechi
export QAECHI_KAFKA_BOOTSTRAP_SERVERS=kafka:9092

QAECHI_QUEUE=kafka qaechi-parser serve --host 0.0.0.0     # the API
qaechi-parser worker --processes 4                       # as many replicas as you need

The HTTP API does not change: POST /jobs still returns 202 with a job ID, and GET /jobs/{id} and GET /results read from PostgreSQL, so any API replica can answer. If Kafka cannot take the job, POST /jobs returns 503 and the job is marked failed, never silently lost.

The ready-made Scale stack in qaechi-stack wires this up with Kafka, PostgreSQL and the Grafana dashboards.

Choose a database

Jobs and results live in the database you already run. Pick it with the URL scheme and install the matching extra; the tables or collections are created on first start.

You run QAECHI_DATABASE_URL Extra
PostgreSQL 14+, Amazon RDS or Aurora PostgreSQL, Neon, Cloud SQL postgresql://user:pass@host:5432/qaechi postgres
Supabase the connection string from Project Settings, Database, with ?sslmode=require. Direct and pooler URLs both work postgres
MySQL 8.4+, MariaDB 11+, Aurora MySQL mysql://user:pass@host:3306/qaechi (add ?ssl=true for managed services) mysql
MongoDB 7+, MongoDB Atlas, Amazon DocumentDB mongodb://user:pass@host:27017/qaechi or mongodb+srv://... mongodb
Anything else implement the small Store protocol and call qaechi.parser.store.set_store()

Each backend gives the same guarantees: inserts are idempotent, and a job is claimed atomically so a redelivered message never runs twice.

How workers scale

Parsing is CPU heavy, so each worker process is one Kafka consumer and runs one job at a time.

parallel jobs = processes per replica x replicas      (at most the topic's partition count)

Messages are keyed by documentId, so jobs for the same document stay in order. Give the jobs topic at least as many partitions as the parallelism you want.

Delivery guarantees

Delivery is at least once, made safe by the worker:

Situation What the worker does
New job Runs it, saves the result, publishes a result event, then commits the offset
Same job delivered again (already finished) Skips it and commits. The document is not parsed twice
Worker died mid-job Kafka redelivers it to another worker. After QAECHI_WORKER_MAX_ATTEMPTS deliveries (default 3) the job is marked failed
Message cannot be read Copied to the DLQ topic with the reason in the qaechi-error header, then committed
Database or Kafka down The process exits without committing; your supervisor restarts it and nothing is lost

A job that fails while parsing (a broken file, a timeout) is a finished job with status failed; it is not retried.

Publish jobs from anywhere

Any system can put a job on the topic, for example a transactional outbox in your business service. The worker creates the job record if the API has not.

{
  "specVersion": "1",
  "jobId": "4f6c2a0e-0b7d-4a43-9d55-0d1c2b6b1a77",
  "request": {"documentId": "inv-2026-0042", "source": "invoices/0042.pdf", "docType": "invoice"}
}

Key: the documentId. Optional header: traceparent (W3C), so the worker's spans join your trace.

Result events

Every finished job is published to qaechi.parse.results, keyed by documentId:

{
  "specVersion": "1", "jobId": "4f6c2a0e-...", "documentId": "inv-2026-0042",
  "docType": "invoice", "status": "succeeded", "resultId": "9b1e...", "route": "parser",
  "groundedness": "yes", "error": null, "attempts": 1, "finishedAt": "2026-11-30T09:12:44+00:00"
}

Consume it to update your system as soon as a document is done instead of polling. Fetch the values with GET /results/{resultId}.

Settings

Variable Default Purpose
QAECHI_QUEUE inline kafka makes POST /jobs publish instead of running in-process
QAECHI_DATABASE_URL PostgreSQL, MySQL or MongoDB (choose). Required in queue mode so the API and workers share jobs
QAECHI_DATABASE_POOL_SIZE 5 Connections per process
QAECHI_KAFKA_BOOTSTRAP_SERVERS Brokers, e.g. kafka:9092
QAECHI_KAFKA_TOPIC_JOBS qaechi.parse.jobs Job requests
QAECHI_KAFKA_TOPIC_RESULTS qaechi.parse.results Result events
QAECHI_KAFKA_TOPIC_DLQ qaechi.parse.jobs.dlq Unreadable messages
QAECHI_KAFKA_GROUP_ID qaechi-parser-worker Consumer group of the workers
QAECHI_WORKER_PROCESSES 1 Consumer processes per worker command
QAECHI_WORKER_MAX_ATTEMPTS 3 Deliveries of one job before it is marked failed
QAECHI_KAFKA_CONF_* Any librdkafka setting: QAECHI_KAFKA_CONF_SECURITY_PROTOCOL=SASL_SSL becomes security.protocol

For AWS MSK with SASL/SCRAM, for example:

QAECHI_KAFKA_CONF_SECURITY_PROTOCOL=SASL_SSL
QAECHI_KAFKA_CONF_SASL_MECHANISM=SCRAM-SHA-512
QAECHI_KAFKA_CONF_SASL_USERNAME=qaechi
QAECHI_KAFKA_CONF_SASL_PASSWORD=...        # from your secret store

Observability

With the otel extra on, the worker adds qaechi_queue_messages_total{outcome} (processed, duplicate, dlq, gave_up) to the metrics, and one trace follows a job from POST /jobs through Kafka to the worker's pipeline steps.

Many workers share one model account: set QAECHI_REDIS_URL with QAECHI_LLM_RPM / QAECHI_LLM_TPM so they share one limit, and QAECHI_CACHE=on to reuse results for repeated files. See Caching and rate limits.

한국어