Docs / Serverless

How workers & queues work

Every endpoint has one queue and up to maxWorkers workers. This page explains how jobs flow through the queue, how the autoscaler decides how many workers to run, and what a worker does during its lifetime.

The queue

Jobs submitted with /run or /runsync are stored in a durable, per-endpoint queue. Workers take jobs in first-in, first-out order. Jobs sent with policy.lowPriority: true are only picked up when no normal-priority job is waiting.

  • A job waits in the queue for at most its time-to-live (policy.ttlInSec, default 24 hours). If no worker picks it up in time it ends as TIMED_OUT.
  • The time a job spends waiting is reported as delayTime (milliseconds) on the job object. It includes any cold start the job had to wait for.
  • Queue depth is visible on GET /health as jobs.inQueue. Use POST /purge-queue to drop every queued job, for example after a bad deploy.

Autoscaling

The autoscaler re-evaluates each endpoint every second and keeps the number of workers between minWorkers and maxWorkers. How aggressively it scales up depends on scalerType:

scalerTypescalerValue meansScales up when
QUEUE_DELAYSeconds (default 4)The oldest queued job has waited longer than scalerValue seconds. Good default: it ignores short bursts that existing workers absorb.
REQUEST_COUNTTarget jobs per workerIn-queue plus in-progress jobs divided by scalerValue exceeds the current worker count. Use it for latency-sensitive endpoints with predictable job duration.

Scaling down is controlled by idleTimeoutInSec: a worker that has had no job for that many seconds is stopped. Workers are never stopped in the middle of a job.

Worker lifecycle

Each worker has an ID like wrk_8q3m2x9k and moves through the states below. List them with GET /endpoints/{endpointId}/workers or on the endpoint's Workers tab in the console.

Worker stateMeaning
INITIALIZINGThe worker is starting: pulling the image or restoring a Warmstart snapshot, then running your module-level code.
IDLEReady and waiting for a job. Billed until the idle timeout expires (Flex) or always (Active).
RUNNINGExecuting one or more jobs.
THROTTLEDScheduled but waiting for a GPU from your priority list to free up on the host.
UNHEALTHYThe worker crashed or failed its health check. It is replaced automatically.

A worker's life starts with initialization: the image is pulled (or restored from a Warmstart snapshot), the container starts and your module-level code runs, typically loading model weights onto the GPU. Only then does the SDK start polling for jobs.

Cold starts and Warmstart

A cold start is everything that happens before a new worker can take its first job: image pull, container start and model load. For a 10 GB image with a 7 GB model this can take 30 to 90 seconds.

Warmstart removes most of that. After a worker finishes initializing, Apipods snapshots its memory and disk and caches the snapshot on hosts in the endpoint's regions. New workers restore from the snapshot instead of starting from scratch, typically in under 2 seconds. The first worker after each deploy is always a full cold start, because that is the worker that produces the snapshot.

Tip: Load models at module level, not inside the handler. Work done before apipods.serverless.start() is captured by the snapshot; work done inside the handler is repeated for every job.

GPU selection

When a worker starts, the scheduler walks gpuPoolIds in order and takes the first pool with free capacity in one of the endpoint's regionIds. If none is free, the worker waits in THROTTLED until a GPU frees up. Listing two or three pools that all fit your model is the single best way to avoid throttling. The pool a worker landed on is available inside the container as APIPODS_GPU_POOL_ID.

Limits

LimitValue
Concurrent GPUs per organization (pods + workers)Plan limit: 10 on Starter, 50 on Team, custom on Enterprise
maxWorkers default3
Execution timeout600 s default, up to 86400 s (24 h)
Idle timeout5 s default, 1 to 3600 s
Job time-to-live in queue86400 s default
Input payload10 MB for /run, 20 MB for /runsync