Inside the Server: Shards, Matching, and Two Internal Queues

The Four Services

ServiceKeeps state?What it does
FrontendNoThe 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.
HistoryYes. 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.
MatchingYes. 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)NoRuns 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.
One request path through the Temporal server, as seven numbered steps. 1, your client calls StartWorkflowExecution. 2, the Frontend authenticates, rate-limits and routes. 3, the History host owning shard N handles it, where N is hash of namespace and workflow ID modulo the shard count. 4, persistence: one transaction writes the history events, the mutable state and a transfer task; the client's call returns once this commits. 5, the transfer task processor reads the task and pushes it to Matching. 6, the Matching host owning the task queue takes it. 7, a worker parked on a long poll receives the workflow task. Steps 5 to 7 are asynchronous: a transactional outbox, one atomic local write followed by at-least-once delivery.
One request path: start a workflow, then a worker picks up its first task

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 workflowhash(namespace, workflowID) % shardCount
Owners per shardExactly one History host
Shard countSet 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.