# Shared workers

> **For AI agents:** the complete documentation index is at [llms.txt](https://questdb.com/docs/llms.txt). Every page is available as markdown by appending `.md` to its URL, or by sending an `Accept: text/markdown` request header.

Configure QuestDB's network, query and write thread pools, and split network pools into worker groups to raise HTTP, PostgreSQL wire and ILP throughput on large and multi-socket servers.

QuestDB distributes work across three specialized thread pools. Each pool can
be sized independently, letting you balance CPU resources between network I/O,
query execution, and write operations based on your workload profile. On large
Linux servers, you can also split a network pool into
[worker groups](#network-worker-groups) to raise network throughput.

- **Network pool**: handles HTTP, PostgreSQL, and ILP server I/O
- **Query pool**: executes parallel query operations, such as filters, GROUP BY
  and SAMPLE BY, GROUP BY over hash joins, HORIZON JOIN and WINDOW JOIN
- **Write pool**: manages table writes and housekeeping tasks, and WAL apply
  jobs when `wal.apply.worker.count` is `0`

All three pools use the same default sizing formula, which scales with the
number of available CPUs:

| CPU count | Default threads per pool |
|-----------|--------------------------|
| > 32      | `max(2, CPU count - 2)`  |
| > 16      | `max(2, CPU count - 1)`  |
| ≤ 16      | `max(2, CPU count)`      |

On a typical 8-core machine, each pool defaults to 8 threads. On a 64-core
server, each pool gets 62 threads. Adjust these values when you need to shift
CPU capacity toward a specific workload, for example reducing the network pool
and increasing the query pool on a read-heavy system.

## Dedicated pools

WAL apply, materialized view refresh, and live view refresh run on their own
pools, not on the write pool. Each is sized by its own key:
[`wal.apply.worker.count`](/docs/configuration/wal/#walapplyworkercount),
[`mat.view.refresh.worker.count`](/docs/configuration/materialized-views/#matviewrefreshworkercount),
and
[`live.view.refresh.worker.count`](/docs/configuration/live-views/#liveviewrefreshworkercount).
Each defaults to `2` on hosts with up to 8 CPU cores, `3` on up to 32 cores, and
`4` on larger hosts. Setting `wal.apply.worker.count` to `0` moves WAL apply to
the write pool, while `0` for either refresh pool disables that refresh.

## Network worker groups

Each network server (HTTP, PostgreSQL wire and ILP/TCP) has an IO dispatcher,
which hands the events on its connections to the pool's workers. Every event
passes through the dispatcher twice: once when data arrives on a connection, and
again when the connection goes back to waiting for its next event. The
dispatcher runs on the pool's workers, but only one worker at a time can run it,
and the workers take turns through a lock, which adds its own cost. When each
request needs significant processing, this cost is small in comparison. When
requests are small and frequent, the dispatcher's serial work dominates, and on
a large pool it limits the server's throughput.

On Linux, you can split a network thread pool into worker groups. Each group
runs its own IO dispatcher for every server on the pool, so the dispatchers work
in parallel. Each group needs at least 8 workers, so only pools of 16 or more
workers can split. On a machine with several CPU sockets, QuestDB also confines
each group to one socket when the group count divides evenly over the sockets
(see [CPU socket placement](#placement) for all the conditions). Splitting is
off by default, and on non-Linux operating systems QuestDB ignores the setting.

Depending on your configuration, there may be one or more thread pools that
handle network traffic. By default, only one shared network pool handles
everything, but you can also enable dedicated HTTP, PostgreSQL wire, and ILP/TCP
pools. Each of them has its own worker group count:

| Pool | Key |
|------|-----|
| Shared network pool | `shared.network.worker.groups` |
| HTTP pool, when `http.worker.count` is above `0` | `http.worker.groups` |
| PostgreSQL wire pool, when `pg.worker.count` is above `0` | `pg.worker.groups` |
| ILP/TCP IO pool, when `line.tcp.io.worker.count` is above `0` | `line.tcp.io.worker.groups` |

A group count of `1` (the default) leaves the pool unsplit; a count below `1` is
invalid and the server fails to start. If this is configured for a thread pool
that ends up not hosting a server, it has no effect. For example, HTTP might be
disabled by config, and then its dedicated pool wouldn't host any server.

### When to split

Consider splitting on machines with about 32 or more CPUs; on smaller machines
the dispatcher is rarely the bottleneck. Splitting helps most when a server
handles many concurrent connections that send small, frequent requests. A sign
that the dispatcher limits throughput: throughput stays the same, or even rises,
when you lower the pool's worker count, such as `shared.network.worker.count`.

Start with one group per CPU socket, or two groups on a machine with one socket,
using a count that keeps the [socket placement](#placement). The table under
[Sizing limits](#sizing-limits) shows which counts do. Then check the startup
log, as in the [examples](#worker-group-examples).

Splitting has costs:

- Each group works on its own. A busy group doesn't borrow workers or fiber
  capacity from an idle one, and the kernel routes new connections to the groups
  by a hash, not by load. One group can saturate while another idles, and a
  connection only ever uses its own group's workers. This matters most when
  there are few connections, or a few of them carry most of the load.
- When each request needs significant processing, such as a heavy query, the
  dispatcher's cost is already small, and splitting gains little.
- Connection limits apply per group (see
  [Connections on a split pool](#connections-on-a-split-pool)), and on confined
  groups a large sort uses only one socket's CPUs.

### Sizing limits

There are two rules that limit the number of worker groups in a pool:

1. A group must have at least 8 workers.
2. A pool can't be split into more than 16 groups.

These limits only guard against unreasonable values; they aren't targets.
Choose the group count as described in [When to split](#when-to-split).

QuestDB lowers a configured count that breaks a rule, so a pool gets
`min(configured count, 16, workers / 8)` groups, with `workers / 8` rounded
down. If that comes out below 2, the pool stays unsplit. The workers are divided
into contiguous blocks whose sizes differ by at most one.

At the default network pool size, this allows:

| Host CPUs | Default network workers | Most groups | Most groups confined to two CPU sockets |
|-----------|-------------------------|-------------|-----------------------------------------|
| 16        | 16                      | 2           | 2                                       |
| 32        | 31                      | 3           | 2                                       |
| 64        | 62                      | 7           | 6                                       |
| 96        | 94                      | 11          | 10                                      |

On two CPU sockets, only an even group count can be confined to the sockets
(see [CPU socket placement](#placement)).

### CPU socket (NUMA) placement {#placement}

On a machine with several CPU sockets, QuestDB confines each worker group to the
CPUs of one CPU socket and spreads the groups evenly over the sockets. A worker
that runs the dispatcher first takes exclusive ownership of the cache line that
holds the dispatcher's lock. If the previous owner ran on another CPU socket,
the cache line has to travel over the cross-socket interconnect, which is much
slower than a transfer within one socket. With each group on one socket, that
traffic stays within the socket.

The startup log calls this confinement pinning: it says `split by CPU socket`
for confined groups and `without CPU pinning` for unconfined ones.

In the following cases the groups stay unconfined, and the log says why:

- All cores are already on a single CPU socket. QuestDB counts only the cores it
  is allowed to use. Various tools can restrict this: `taskset`, a cgroup
  cpuset, `docker --cpuset-cpus`, etc.
- The group count doesn't divide evenly over the CPU sockets, for example 3
  groups on 2 CPU sockets. Watch out for automatic group reduction: you may have
  asked for four groups, but QuestDB reduced it to three to fit the limits. This
  will result in three unconfined groups.
- The detected CPU layout doesn't look like real hardware, as on some virtual
  machines: QuestDB can't read the topology, finds more than 8 CPU sockets, a
  CPU socket with fewer than 8 allowed CPUs, a CPU without a NUMA node, or a
  NUMA node that spans two CPU sockets, or the CPU sockets' allowed CPU counts
  differ by more than 25%.
- The configuration enables a pool's thread affinity, which keeps each worker on
  the core it names.

Confinement also applies to the extra threads a query starts. A query over HTTP
or PostgreSQL wire that sorts 1,024,000 rows or more
(`cairo.sql.sort.encoded.parallel.threshold`), such as a large `ORDER BY`,
spreads the sort over threads that inherit the worker's CPUs, so the sort runs
on one socket's CPUs only.

### Worker group examples

Each example shows the `server.conf` settings and the startup log lines that
report the outcome, without the timestamp and logger prefix. To check how
QuestDB split your pools, search the log for `workers split`,
`workers not split` and `reduced to`. The CPU lists depend on how the host
numbers its CPUs.

**A pool too small to split.** On a 12-CPU host, the network pool has 12 workers
by default, too few for two groups of 8, so QuestDB doesn't split it:

```ini title="server.conf"
shared.network.worker.groups=2
```

```text
network workers not split: 12 network workers cannot fill 2 groups of 8
```

**The shared pool on a two-socket host.** On a 64-CPU host with two CPU
sockets, the network pool has 62 workers by default. Four groups put two groups
on each socket:

```ini title="server.conf"
shared.network.worker.groups=4
```

```text
network workers split by CPU socket: group 0 workers 0-15 on CPUs 0-31 (socket 0), group 1 workers 16-30 on CPUs 0-31 (socket 0), group 2 workers 31-46 on CPUs 32-63 (socket 1), group 3 workers 47-61 on CPUs 32-63 (socket 1)
```

**A count that loses socket placement.** On the same host, 62 workers fill at
most 7 groups of 8, so QuestDB lowers a count of 8 to 7. Seven groups don't
divide evenly over two sockets, so they stay unconfined. A count of 6 keeps the
placement.

```ini title="server.conf"
shared.network.worker.groups=8
```

```text
shared.network.worker.groups=8 reduced to 7: 62 network workers fill 7 groups of 8
network workers split into 7 groups without CPU pinning: 7 groups don't divide evenly over 2 sockets [layout=socket 0: CPUs 0-31 on node 0, socket 1: CPUs 32-63 on node 1]
```

**A dedicated pool.** A server on its own pool is split by that pool's key, not
by `shared.network.worker.groups`. On the same host:

```ini title="server.conf"
http.worker.count=32
http.worker.groups=4
```

```text
http workers split by CPU socket: group 0 workers 0-7 on CPUs 0-31 (socket 0), group 1 workers 8-15 on CPUs 0-31 (socket 0), group 2 workers 16-23 on CPUs 32-63 (socket 1), group 3 workers 24-31 on CPUs 32-63 (socket 1)
```

With `shared.network.worker.groups=4` in place of `http.worker.groups=4`, the
shared pool splits for the PostgreSQL wire and ILP/TCP servers, but the HTTP
server's pool doesn't:

```text
http workers not split: shared.network.worker.groups doesn't reach the HTTP server's own pool, which http.worker.groups splits
```

Other lines report a pool that isn't split or isn't confined, and why:

```text
network workers not split: shared.network.worker.groups applies on Linux only, where SO_REUSEPORT spreads connections over the groups' listeners
network workers not split: the shared network pool hosts no HTTP, PostgreSQL wire or ILP/TCP server
network workers split into 4 groups without CPU pinning: shared.network.worker.affinity sets the workers' CPUs
```

### Connections on a split pool

The groups of a split pool share each server's port through `SO_REUSEPORT`:

- The kernel spreads new connections over the groups by a hash, not by load.
- Max connection count (`*.net.connection.limit`) applies per group, so with
  four groups you're actually allowing four times the limit.
- A group at its connection limit stops accepting, but the kernel keeps routing
  new connections to it, and they wait in its backlog. If one of the group's
  connections closes first, the group accepts them. If
  `*.net.connection.queue.timeout` passes first (counted from when the group
  reached its limit), the group closes its listener. That refuses the waiting
  connections, and new connections go to the other groups until the group drops
  below its limit and reopens.
- `pg.net.connection.queue.timeout` defaults to 5 minutes, so a PostgreSQL
  client routed to a full group can wait that long while other groups have room.
  Consider lowering it when you split the pool: a full group then closes its
  listener sooner, and new connections go to the other groups.
- HTTP's soft limits per endpoint (`http.json.query.connection.limit`,
  `http.ilp.connection.limit` and `http.export.connection.limit`) still count
  connections across the whole server. Startup requires the soft limits you set
  to add up to at most `http.net.connection.limit`, which on a split pool is one
  group's limit. If you lower `http.net.connection.limit` to keep the server's
  total unchanged after a split, lower the soft limits too, or QuestDB fails to
  start.
- QuestDB fails to start if another process already holds the port, as it does
  with an unsplit pool.

:::warning

After QuestDB starts, another process running as the same OS user can bind the
port of a split pool's server with `SO_REUSEPORT`, and the kernel then sends it
a share of the new connections. Run QuestDB under a dedicated OS user so that no
other process can join its ports.

:::

### Fiber mode on a split pool

In Fiber mode, each group has its own fiber runtime, so a connection's fibers
stay in its group, and on its CPU socket when the groups are confined to CPU
sockets. The pool's fiber limits divide across the groups in proportion to
their workers.

### Monitoring worker groups

The fiber metrics report each group of a split pool on its own. Their Prometheus
samples carry a `group` label next to `worker_pool`, so summing by
`worker_pool` gives the pool's totals. In
[`sys.metrics`](/docs/operations/logging-metrics/#persisted-metrics), the column
names add the group to the pool name, for example
`worker_pool_fiber_live__shared_network_g0`.

### Resource groups on a split pool

With QuestDB Enterprise [resource groups](/docs/concepts/resource-groups/), the
part of a query that runs on the network pool runs only on the workers of the
worker group that accepted its connection, and competes for CPU only with the
queries in that worker group. CPU weights then balance network pool CPU within
each worker group, not across the pool, so a resource group whose queries run in
a quiet worker group gets that worker group's full capacity, whatever its
weight. CPU that a resource group uses in one worker group still counts against
it in the others. Parallel tasks on the shared query pool compete across the
whole query pool, which isn't split. See
[CPU weight is a share, not a reservation](/docs/concepts/resource-groups/#cpu-weight-is-a-share-not-a-reservation).

### Work that isn't split

On a split pool, some work runs on the workers of every group:

- A server bound to port `0` gets a single dispatcher, because each group's
  listener would get a different ephemeral port.
- On the shared network pool, ILP and QWP over UDP, and the background jobs the
  pool runs, including the parallel query jobs when
  `shared.query.worker.count=0`.

## shared.network.worker.affinity

- **Default**: none
- **Reloadable**: no

Comma-delimited list of CPU ids, one per thread specified in
`shared.network.worker.count`. By default, threads have no CPU affinity.
Pinning threads to specific cores can reduce context-switch overhead on
NUMA systems. Setting it also stops QuestDB from confining the pool's worker
groups to CPU sockets; see [CPU socket placement](#placement).

## shared.network.worker.count

- **Default**: see table above
- **Reloadable**: no

Number of worker threads for the network pool, which handles HTTP, PostgreSQL,
and ILP server I/O. Increasing this value raises network I/O parallelism at
the expense of CPU resources available to queries and writes.

## shared.network.worker.fiber.enabled

- **Default**: `true`
- **Reloadable**: no

Runs the network pool in Fiber mode, where a query can suspend and release its
worker instead of holding it until it finishes. Setting this to `false` puts the
pool in legacy mode.

QuestDB Enterprise [resource groups](/docs/concepts/resource-groups/) require
Fiber mode on every pool that executes SQL. HTTP and PostgreSQL run on this pool
whenever their own worker counts are zero, which is the default, so this is
usually the setting that matters. With the pool in legacy mode, resource groups
left at their default disable themselves at startup and log the pool and setting
responsible, while `resource.groups.enabled=true` set explicitly fails startup.

## shared.network.worker.groups

- **Default**: `1`
- **Reloadable**: no

Linux only. The number of worker groups the shared network pool splits into
while it hosts an enabled HTTP, PostgreSQL wire or ILP/TCP server, each group
with its own IO dispatcher. See [Network worker groups](#network-worker-groups).

## shared.query.worker.affinity

- **Default**: none
- **Reloadable**: no

Comma-delimited list of CPU ids, one per thread specified in
`shared.query.worker.count`. By default, threads have no CPU affinity.

## shared.query.worker.count

- **Default**: see table above
- **Reloadable**: no

Number of worker threads for the query pool, which executes parallel query
operations such as filters, GROUP BY and SAMPLE BY,
[GROUP BY over hash joins](/docs/configuration/cairo-engine/#parallel-group-by-over-hash-joins),
[HORIZON JOIN](/docs/query/sql/horizon-join/#parallel-execution) and
[WINDOW JOIN](/docs/query/sql/window-join/). Increasing this value raises query
parallelism at the expense of CPU resources available to network I/O and
writes.

## shared.query.worker.fiber.enabled

- **Default**: `true`
- **Reloadable**: no

Runs the query pool in Fiber mode, where parallel query work can suspend and
release its worker instead of holding it until it finishes. Setting this to
`false` puts the pool in legacy mode.

QuestDB Enterprise [resource groups](/docs/concepts/resource-groups/) require
Fiber mode here whenever this pool has workers and HTTP or PostgreSQL is
enabled. A query pool set to zero workers turns parallel SQL off and needs no
check of its own.

## shared.worker.haltOnError

- **Default**: `false`
- **Reloadable**: no

When enabled, a worker thread stops if it encounters an unexpected error.
Intended for debugging. In production, leave this disabled so that transient
errors do not reduce the size of a thread pool.

## shared.write.worker.affinity

- **Default**: none
- **Reloadable**: no

Comma-delimited list of CPU ids, one per thread specified in
`shared.write.worker.count`. By default, threads have no CPU affinity.

## shared.write.worker.count

- **Default**: see table above
- **Reloadable**: no

Number of worker threads for the write pool, which manages table writes and
housekeeping tasks, and WAL apply jobs when `wal.apply.worker.count` is `0`.
Increasing this value raises write parallelism at the expense of CPU resources
available to network I/O and queries. See [Dedicated pools](#dedicated-pools)
for the WAL apply and view refresh pools.
