Inside the Server: Shards, Matching, and Two Internal Queues
The Four Services
| Service | Keeps state? | What it does |
|---|---|---|
| Frontend | No | The gRPC API that everything talks to. It handles auth, rate limits, and checks, then sends each request to the right History or Matching host. Scale it based on how many connections you have. |
| History | Yes. It owns shards. | The brain. It owns each workflow's current state, adds events, decides what happens next, and runs the two internal queues described below. |
| Matching | Yes. It owns task queues. | Holds the task queues and pairs new tasks with workers that are waiting for work. The fast hand-off from the Workers and task queues part happens here. |
| Worker (internal) | No | Runs Temporal's own background workflows: archiving, cleanup of old data, copying data between clusters. These are not your workers. The name is confusing and easy to mix up. |
The Two Internal Queues — the Key to the Whole Design
Each shard keeps two internal task queues in the database. Once you understand them, you'll see how Temporal stays reliable without distributed transactions:
Transfer tasks
"Something needs to be sent out of the shard." For example: send an activity task to Matching, start a child workflow, or add a row for search. They are handled in order, as fast as possible.
Timer tasks
"Something needs to happen at time T." For example: a workflow timer (see Timers), an activity timeout, a retry delay, or a workflow timeout. They are sorted by time and checked as the clock moves forward.
Here's the key detail: a task row is saved in the same database transaction as the history events that create it. For example, adding ActivityTaskScheduled and adding "tell Matching about this activity" either both happen or neither does. Then a separate process reads the task queue and does the real work, retrying until it succeeds.
This is called the transactional outbox pattern. It's why Temporal never needs a distributed transaction: it makes one local write that either fully happens or doesn't, then delivers what follows from it at least once. "At least once" means some things can happen twice. That's what the At-least-once and idempotency part covers.
Shards
| Shard for a workflow | hash(namespace, workflowID) % shardCount |
|---|---|
| Owners per shard | Exactly one History host |
| Shard count | Set when the cluster is created: 512 / 4K / 16K |
| Can you change it later? | No. You need a new cluster and a migration. |
Because each shard has only one owner, the History host can keep each workflow's current state in memory and update it without checking with anyone else. That state is a summary: what's running, what's waiting, and the next event ID.
When a History host dies, its shards move to other hosts. The new owner rebuilds the state from the database. While that happens, workflows on those shards pause. No data is ever at risk. The workflows just wait a little.
- Guarantees: Each workflow run has exactly one writer at a time. So its history always has one clear order, and two workers can never move the same workflow forward at the same time.
- Does not guarantee: That a shard is available while it's moving to a new owner. Or any ordering between different workflows. Or that visibility search shows the very latest state.