Skip to content

Batch and async serving on llm-d

Use this guide to fill a TPU pool's slack capacity with work that can wait: offline evaluations, dataset scoring, background summarization. Two components put a queue in front of the llm-d Router. The Batch Gateway (experimental) serves OpenAI's /v1/files and /v1/batches APIs with job tracking; the Async Processor (llm-d-async) pulls from GCP Pub/Sub or a Redis sorted set and dispatches only while the pool has spare capacity. Both are accelerator independent: they talk to the router, not the TPUs. Chapter 21 places them next to flow control.

Prerequisites

  • A deployed optimized baseline on TPU; both components dispatch to its router.
  • Batch Gateway: Helm 3, PostgreSQL 12 or newer, Redis 6 or newer (or Valkey 8), and a ReadWriteMany storage class (or S3).
  • Async Processor: Kubernetes 1.31 or newer, and either a GCP project with the Pub/Sub API enabled or Redis.
Choose When
Batch Gateway Clients expect the OpenAI Batch API, job status and result files; multi-tenant batch service
Async Processor Event-driven pipelines on Pub/Sub or Redis; slack-capacity filling gated on Prometheus; per-tenant quotas and priority tiers

They can run together: the Batch Gateway then publishes each request of a job into the Async Processor's queue.

Steps

1. Batch Gateway: deploy

export NAMESPACE=batch-gateway
kubectl create namespace ${NAMESPACE}
kubectl create secret generic batch-gateway-secrets -n ${NAMESPACE} \
  --from-literal=redis-url="redis://redis-master.redis.svc.cluster.local:6379/0" \
  --from-literal=postgresql-url="postgresql://user:password@postgresql.postgresql.svc.cluster.local:5432/batchgateway"
export INFERENCE_GW_URL="http://optimized-baseline-epp.llm-d-optimized-baseline.svc.cluster.local:80"

kubectl apply -n ${NAMESPACE} -f - <<EOF
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: batch-gateway-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi  # example size, tune to your workload
EOF

helm install batch-gateway oci://ghcr.io/llm-d/charts/batch-gateway \
  -n ${NAMESPACE} \
  --set processor.config.globalInferenceGateway.url="${INFERENCE_GW_URL}" \
  --set "apiserver.config.batchAPI.passThroughHeaders={Authorization}" \
  --set global.fileClient.fs.pvcName="batch-gateway-pvc"

Replace the Redis and PostgreSQL URLs with your own. In Gateway mode, INFERENCE_GW_URL is the Gateway address instead of the EPP Service.

Check: kubectl get pods -n ${NAMESPACE} shows apiserver, processor and gc pods Running, and the health endpoint answers:

kubectl port-forward -n ${NAMESPACE} svc/batch-gateway-apiserver 8081:8081 &
curl http://localhost:8081/health

2. Batch Gateway: run a job

Write batch_input.jsonl with one request per line in the OpenAI Batch format, using your served model (for example Qwen/Qwen3-32B on v6e):

{"custom_id": "req-001", "method": "POST", "url": "/v1/chat/completions", "body": {"model": "my-model", "messages": [{"role": "user", "content": "Hello"}], "max_tokens": 100}}
kubectl port-forward -n ${NAMESPACE} svc/batch-gateway-apiserver 8000:8000 &
curl -X POST http://localhost:8000/v1/files \
  -F "purpose=batch" \
  -F "file=@batch_input.jsonl"
curl -X POST http://localhost:8000/v1/batches \
  -H "Content-Type: application/json" \
  -d '{
    "input_file_id": "<file-id-from-upload>",
    "endpoint": "/v1/chat/completions",
    "completion_window": "24h"
  }'
curl http://localhost:8000/v1/batches/<batch-id> | jq '{status, request_counts}'
OUTPUT_FILE_ID=$(curl -s \
  http://localhost:8000/v1/batches/<batch-id> | jq -r '.output_file_id')
curl http://localhost:8000/v1/files/${OUTPUT_FILE_ID}/content > results.jsonl

Check: the batch reaches completed and results.jsonl holds one response per custom_id.

3. Async Processor: deploy

Get the router address, create the queue (Pub/Sub shown; one request topic per model and priority, a dead-letter topic and a result topic), then install the chart:

export REPO_ROOT=$(realpath $(git rev-parse --show-toplevel))
export IP=$(kubectl get service optimized-baseline-epp -n llm-d-optimized-baseline -o jsonpath='{.spec.clusterIP}')

export REQUEST_TOPIC_NAME=async-proc-requests
export SUBSCRIPTION_NAME=async-proc-requests-sub
export DLQ_NAME=async-proc-requests-dlq
export RESULT_TOPIC_NAME=async-proc-results
gcloud pubsub topics create $REQUEST_TOPIC_NAME
gcloud pubsub topics create $DLQ_NAME
gcloud pubsub topics create $RESULT_TOPIC_NAME

export NAMESPACE=llm-d-async
export MQ_PROVIDER=gcp-pubsub # or redis
export ASYNC_VERSION=v1.0.0
[ "$MQ_PROVIDER" = "redis" ] && TARGET_KEY="ap.transportConfig.queues[0].igw_base_url" || TARGET_KEY="ap.transportConfig.topics[0].igw_base_url"
helm install llm-d-async \
    oci://ghcr.io/llm-d/charts/llm-d-async \
    -f ${REPO_ROOT}/guides/batch-serving/asynchronous-processing/${MQ_PROVIDER}/values.yaml \
    --set ${TARGET_KEY}=http://${IP}:80 \
    -n ${NAMESPACE} --create-namespace --version ${ASYNC_VERSION}

The upstream guide also creates the request subscription (exactly-once delivery, exponential backoff, the dead-letter topic) and grants the chart's llm-d-async service account access; follow it for those gcloud commands. Fill in the values file's dispatch gates so queued work is released only while the pool has headroom.

Check: kubectl get pods -n ${NAMESPACE} shows the processor Running.

4. Async Processor: send and read one request

gcloud pubsub topics publish $REQUEST_TOPIC_NAME --message='{"id":"testmsg","created":1700000000,"deadline":1999999999,"payload":{"model":"your-model","prompt":"Hi, good morning"}}'
gcloud pubsub subscriptions create async-proc-results-sub --topic=$RESULT_TOPIC_NAME
gcloud pubsub subscriptions pull async-proc-results-sub --auto-ack --limit=1

Set model to your served model. deadline and created are Unix-second numbers; a quoted deadline fails to decode.

Check: the pull returns the result message for testmsg.

5. Size the processor's concurrency

Workers are in-flight requests; each blocks until the router answers. By Little's law, required_workers ≈ target_throughput (req/s) × avg_request_latency (s): with 2 s requests the default ap.concurrency: 64 caps you at 32 req/s. Set concurrency ≈ ceil(target_throughput × W × 1.3), keep each replica at about 128 to 256, and add replicas beyond that. Long outputs on TPU raise W and the required concurrency in proportion.

Check: worker utilization, sum by (pool_name) (llm_d_async_async_inflight_requests) / llm_d_async_async_pool_worker_limit, stays below 1.0 while the pool has headroom.

6. Watch the backlog

Component Signal Reading
Async llm_d_async_async_broker_backlog (trust a zero only when llm_d_async_async_broker_backlog_source_available is 1) Work not yet pulled
Async llm_d_async_async_dispatch_budget, llm_d_async_async_gate_decisions_total by reason Budget 0 with gate_closed is the gate working; error is not
Async llm_d_async_async_queue_residence_time_millis against llm_d_async_async_inference_latency_time_millis Which side the delay is on
Batch Gateway job_queue_wait_duration_seconds, active_workers / total_workers Backlog and worker saturation (scope by namespace)
Batch Gateway batch_processor_aimd_decreases_total by signal The pool pushing back with 429 or 5xx

Check: under a background job, interactive TTFT (Q01 in Observability) stays within its SLO.

7. Clean up

helm uninstall llm-d-async -n llm-d-async
helm uninstall batch-gateway -n batch-gateway

Check: helm list -A no longer shows either release.

Troubleshooting

Symptom Cause Fix
Async backlog grows, workers idle, budget near 0 The gate is closed because the pool is saturated Expected on a slack-filling path; if gate_metric_source_available is 0, fix the Prometheus source first
Async backlog grows, utilization at 1.0 Worker limit caps throughput Raise the pool's workers while the TPU pool has headroom
Shed requests climbing Router or servers answer 429 Capacity; see Autoscaling
Batch queue wait rises, AIMD limit falls Backend pushing back Fix the inference side; more workers will not help
Batch jobs expire Jobs aged out before running Capacity or completion window
Batch PVC never binds No ReadWriteMany class Use the S3 backend (global.fileClient.type=s3)

References