Workers and Task Queues: The Server Never Runs Your Code
Workers Ask for Work. The Server Doesn't Push It.
A worker opens a gRPC call that says: "Give me a workflow task from queue orders." The server holds the call open and doesn't answer until a task is ready, or until about a minute has passed. Then the worker asks again right away. This is called long polling. It has some useful results:
| Benefit | Why long polling gives you this |
|---|---|
| No incoming connections needed | Workers can run behind NAT, in a private VPC, on a laptop, or in another cloud. This is why Temporal Cloud works without exposing your services. Your code and your systems stay in your network. Only task details and payloads go back and forth. |
| Built-in backpressure | A worker that's too busy just stops asking for work. The work waits in the queue, where you can see it, instead of being pushed at a process that can't handle it. |
| The server doesn't track workers | No list to keep up to date, no health checks, no service discovery. A worker exists for as long as it keeps asking for work. |
| Still fast | The worker's request is already waiting on the server. So when a new task arrives, handing it over takes a few milliseconds, not a whole polling cycle. |
A task queue is just a name. You don't create it, size it, or set it up. It exists as soon as someone polls it or sends a task to it. Each name holds two kinds of tasks: workflow tasks and activity tasks.
Sticky Execution — What Keeps Replay Cheap
If every workflow task had to replay from event 1, a workflow would get much slower as its history grew. The cost would grow with the square of its length. So Temporal uses a shortcut.
After a worker runs a workflow's first task, it tells the server: "Send this workflow's next tasks to my own private queue." The worker then keeps the rebuilt workflow in a cache in memory.
- Cache hit (the usual case). The server sends only the new events since the last task. The worker applies them to the cached workflow and carries on. No replay needed.
- Cache miss (the worker restarted, or the workflow was dropped from the cache). The task on the private queue has a short timeout of a few seconds. When it runs out, the server sends the task to the shared queue instead. Whichever worker picks it up does a full replay. The result is exactly the same. It just costs more.
Sticky execution only makes things faster. It has nothing to do with correctness. Any worker can always rebuild any workflow from its history. That's why you can deploy, autoscale, and lose machines without ever thinking about which worker has which workflow.
You can also use task queues to control where work runs. Two useful patterns:
- A task queue per host. An activity returns the name of a queue that only its own machine listens to. The next activity is sent to that queue, so it runs on the same machine, where a downloaded file is already waiting.
- Capability queues, such as
gpu,pci-compliant, oreu-region. The queue name says which machines are allowed to run the work.
- Guarantees: A task sent to a task queue reaches at least one worker that polls it. You can add, remove, or restart workers at any time without affecting whether any workflow runs correctly.
- Does not guarantee: That a task is delivered if no worker polls that queue name. A typo in a task queue name gives you a workflow that starts and then waits silently forever. The server doesn't know that no one is listening.