# Span store engines: PostgreSQL against the analytics store

**Last updated:** 2026-11-23T14:05:13-03:00 (UTC-3)

Where the [span store](/docs/features/traces/) should keep spans: in the transactional database (`spans.engine: database`, the default without an analytics store) or straight in the [analytics store](/docs/features/analytics-store/) (`spans.engine: analytics_store`, the derived default once the `analytics_store` block is configured, ClickHouse). Both engines were run on the same machine under the same load, with every span stored (`sample_rate: 1.0`). The tables are generated from [`span-store-engines.json`](https://github.com/m7gtw/gateway/blob/main/docs/benchmarks/span-store-engines.json) by `scripts/render-span-engines.py`; the prose around them is written by hand.

## Which one to choose

- **Keep `database`** when there is no analytics store, when traffic stays below a few hundred requests per second, or when single-trace reads must be as fast as possible. It needs nothing besides PostgreSQL, and a trace opens in about 0.5 ms at a million stored spans (2 ms at ten million).
- **Choose `analytics_store`** when ClickHouse already runs beside the gateway and span volume weighs on PostgreSQL. At 1.1–1.4 thousand requests per second it removed about a quarter of PostgreSQL's WAL records per request and 12–17% of its CPU at three of the four points, took no gateway CPU, and stored spans in about a seventh of the disk. In exchange, ClickHouse spends about 90 µs more CPU per request, a trace opens in about 10 ms at a million spans (19 ms at ten million), and spans written while ClickHouse is down are dropped and counted (requests are never affected).
- The request log, the ledger, keys and authorization stay in PostgreSQL with either engine.

Set it in the `spans` block and restart:

```yaml
spans:
  engine: analytics_store # needs the analytics_store block; database is the default
analytics_store:
  engine: clickhouse
  addresses: ["clickhouse:9000"]
  username_env: GATEWAY_ANALYTICS_STORE_USER
  password_env: GATEWAY_ANALYTICS_STORE_PASSWORD
```

## Method

- **Machine:** Mac Mini M4 Pro (12 cores, 24 GB), macOS 26.6.2, OrbStack (Docker 29.8.2, a Linux VM with 12 CPUs).
- **Rig:** one container per process, all sharing one network namespace over loopback. The gateways run on CPUs 0–3 (`GOMAXPROCS=4`), the load generator (`gwbench`) on 4–7 and the instant simulated provider (`gwbench -mock`) on 8–9. ClickHouse 25.12.11.4 (the pinned digest, the shipped server file and user, 6 GB) runs on CPU 10 and PostgreSQL 16.15 (the pinned image, default settings) on CPU 11.
- **Builds:** the commit before the engine setting (`77b32515`, spans in PostgreSQL) and the build with the setting, run once with `engine: database` and once with `engine: analytics_store` (batch default 5,000). Each runs its shipped `config/gateway.yaml` with `span_store` on, `sample_rate: 1.0` and the shipped `spans` block, OTLP export off, and its own PostgreSQL database on the same server. All three run at once; only the one under load is busy.
- **Load:** seven repetitions. In each, every point runs the baseline, the baseline again (the A/A pair, which measures the noise), then the two engines, in an order rotated between repetitions. Points: buffered and streamed Chat Completions at 8 and 32 concurrent clients, 50 warmup calls, then 7,500 to 8,700 requests (about 6–7 s). PostgreSQL and ClickHouse CPU (cgroup) and WAL (`pg_stat_wal`) cover the run plus 2.5 s of catch-up and are divided by the requests served.
- **Reads:** sequential reads through the admin API by a global administrator, of 200 real requests from the load runs after 20 warmup reads, with no load running. Each store was first brought to one and then to ten million spans by synthetic traces of three spans (request, routing, provider attempt, with real spans' attributes), in 20 teams, spread over 7 days, with their summaries.
- **Outage:** a 30,000-request run (buffered, 8 clients) on the analytics store engine, with ClickHouse stopped (connections refused) or paused (a hung server) for 10 s from the sixth second. The writer's counters come from `GET /api/v1/admin/span-store` before, during and after.

<!-- generated by scripts/render-span-engines.py from span-store-engines.json: do not edit below -->

### Load: medians of the repetitions, ranges in brackets

| Point | Build | p50 ms | p99 ms | req/s | gateway CPU µs/req | PostgreSQL CPU µs/req | WAL records/req | WAL bytes/req | ClickHouse CPU µs/req |
|---|---|---|---|---|---|---|---|---|---|
| buffered c=8 | before (77b32515), database | 6.49 [5.69–6.71] | 14.06 [10.46–17.60] | 1,141 [1,081–1,349] | 332 [326–338] | 775 [625–835] | 45.0 [41.7–49.2] | 12,720 [6,250–27,405] | 47 [23–53] |
| buffered c=8 | spans.engine database | 6.45 [5.89–6.79] | 13.45 [11.83–14.64] | 1,152 [1,098–1,278] | 328 [324–336] | 772 [689–867] | 46.4 [42.8–47.4] | 11,634 [6,486–20,828] | 29 [21–41] |
| buffered c=8 | spans.engine analytics_store | 6.18 [5.82–6.35] | 12.12 [11.18–32.67] | 1,225 [1,063–1,303] | 334 [331–374] | 668 [637–720] | 33.1 [27.1–34.7] | 8,795 [4,250–18,210] | 122 [95–146] |
| buffered c=32 | before (77b32515), database | 23.83 [22.68–25.11] | 38.55 [34.19–67.31] | 1,314 [1,177–1,366] | 321 [319–324] | 722 [667–780] | 46.1 [42.8–47.2] | 12,063 [6,784–26,761] | 29 [19–32] |
| buffered c=32 | spans.engine database | 23.09 [20.96–24.61] | 35.85 [31.95–40.23] | 1,343 [1,264–1,462] | 317 [311–318] | 672 [620–711] | 45.2 [41.6–49.1] | 12,975 [7,184–18,475] | 32 [23–48] |
| buffered c=32 | spans.engine analytics_store | 23.49 [22.45–24.46] | 35.09 [32.13–35.69] | 1,328 [1,281–1,391] | 323 [318–331] | 707 [672–778] | 33.6 [31.4–35.3] | 7,756 [4,070–12,193] | 120 [105–141] |
| streamed c=8 | before (77b32515), database | 6.74 [6.47–7.26] | 14.33 [12.34–20.05] | 1,114 [1,042–1,178] | 416 [413–424] | 770 [672–843] | 43.8 [41.4–48.0] | 13,986 [6,464–22,802] | 38 [27–45] |
| streamed c=8 | spans.engine database | 6.56 [6.40–6.92] | 12.91 [12.44–16.68] | 1,157 [1,093–1,193] | 411 [409–415] | 710 [664–726] | 44.9 [40.8–47.4] | 9,547 [6,572–26,539] | 26 [20–44] |
| streamed c=8 | spans.engine analytics_store | 6.42 [6.31–6.87] | 12.69 [11.19–13.46] | 1,185 [1,111–1,218] | 416 [410–418] | 646 [629–702] | 33.0 [29.4–34.3] | 9,160 [4,177–12,278] | 125 [117–127] |
| streamed c=32 | before (77b32515), database | 24.36 [23.54–28.38] | 41.00 [37.52–63.63] | 1,275 [1,093–1,307] | 407 [403–412] | 758 [668–854] | 43.4 [42.7–47.5] | 13,864 [6,405–25,272] | 39 [25–48] |
| streamed c=32 | spans.engine database | 24.69 [23.20–25.11] | 37.30 [33.99–39.06] | 1,258 [1,245–1,345] | 401 [398–407] | 725 [671–757] | 43.8 [41.5–45.8] | 13,383 [6,526–18,814] | 26 [20–39] |
| streamed c=32 | spans.engine analytics_store | 23.02 [21.97–23.72] | 33.44 [30.46–45.86] | 1,359 [1,304–1,430] | 396 [395–400] | 630 [605–653] | 33.7 [32.4–35.8] | 8,565 [4,229–13,565] | 117 [105–141] |

### Load: change against the first baseline run of the same repetition (median [range])

`A/A` is the baseline's second run, so it measures the noise.

| Point | Δ | A/A | spans.engine database | spans.engine analytics_store |
|---|---|---|---|---|
| buffered c=8 | p99 | -9.5% [-23.4%–+14.6%] | -0.1% [-27.5%–+13.1%] | -14.5% [-26.9%–+212.3%] |
| buffered c=32 | p99 | -6.5% [-25.2%–+44.7%] | -8.3% [-46.9%–+7.7%] | -9.0% [-48.5%–-0.2%] |
| streamed c=8 | p99 | -5.0% [-29.0%–+6.0%] | -5.9% [-31.8%–+20.7%] | -8.2% [-32.9%–+0.0%] |
| streamed c=32 | p99 | -10.1% [-19.2%–-0.1%] | -7.8% [-38.6%–-2.3%] | -23.0% [-45.2%–+12.2%] |
| buffered c=8 | p50 | -6.6% [-8.7%–+3.7%] | +1.1% [-5.8%–+3.5%] | -5.0% [-7.2%–+8.5%] |
| buffered c=32 | p50 | -1.7% [-7.5%–+5.9%] | -4.4% [-7.6%–-2.0%] | -0.3% [-9.9%–+3.3%] |
| streamed c=8 | p50 | -0.8% [-11.7%–+1.5%] | -1.9% [-10.8%–+1.6%] | -4.7% [-13.1%–-0.4%] |
| streamed c=32 | p50 | -0.6% [-3.4%–+5.7%] | -1.7% [-11.5%–+5.1%] | -6.7% [-16.4%–-3.9%] |
| buffered c=8 | req/s | +6.7% [-2.3%–+11.5%] | -1.5% [-5.3%–+8.8%] | +9.6% [-21.2%–+13.3%] |
| buffered c=32 | req/s | +2.4% [-5.5%–+9.5%] | +5.0% [+2.2%–+11.2%] | +0.8% [-1.5%–+11.5%] |
| streamed c=8 | req/s | +0.3% [-1.3%–+12.4%] | +3.4% [-1.0%–+12.0%] | +6.4% [+1.0%–+16.9%] |
| streamed c=32 | req/s | +1.0% [-4.5%–+5.1%] | +5.2% [-3.9%–+13.9%] | +11.2% [+6.1%–+19.3%] |
| buffered c=8 | gateway CPU/req | -1.5% [-2.1%–+0.0%] | -0.9% [-1.8%–+0.9%] | +0.9% [-1.5%–+14.7%] |
| buffered c=32 | gateway CPU/req | -0.9% [-1.6%–+0.9%] | -1.6% [-3.1%–-0.6%] | +0.6% [-1.9%–+3.8%] |
| streamed c=8 | gateway CPU/req | -0.2% [-2.1%–+1.7%] | -1.4% [-2.6%–-0.7%] | -1.0% [-2.8%–+0.7%] |
| streamed c=32 | gateway CPU/req | +0.2% [-1.9%–+1.2%] | -1.5% [-2.7%–-0.2%] | -2.7% [-3.2%–-1.2%] |
| buffered c=8 | PostgreSQL CPU/req | -11.9% [-18.7%–+1.1%] | +2.1% [-14.2%–+10.6%] | -13.4% [-20.7%–+6.9%] |
| buffered c=32 | PostgreSQL CPU/req | -4.6% [-9.1%–+7.4%] | -7.0% [-8.8%–-4.5%] | +0.8% [-10.0%–+6.3%] |
| streamed c=8 | PostgreSQL CPU/req | +1.3% [-18.9%–+11.3%] | -5.7% [-20.1%–+1.7%] | -12.2% [-23.4%–-6.4%] |
| streamed c=32 | PostgreSQL CPU/req | -2.8% [-8.5%–+11.6%] | -3.4% [-17.2%–+13.3%] | -16.9% [-23.6%–-9.4%] |
| buffered c=8 | WAL records/req | -2.5% [-11.5%–+12.9%] | -1.5% [-5.9%–+13.5%] | -27.4% [-44.9%–-17.8%] |
| buffered c=32 | WAL records/req | -2.5% [-5.9%–+8.2%] | +0.9% [-11.9%–+6.9%] | -26.0% [-32.3%–-19.5%] |
| streamed c=8 | WAL records/req | +0.4% [-10.9%–+22.0%] | -0.2% [-11.4%–+9.8%] | -25.5% [-32.9%–-16.9%] |
| streamed c=32 | WAL records/req | +8.6% [-12.8%–+17.4%] | -1.5% [-3.6%–+7.2%] | -23.6% [-28.1%–-18.7%] |

Runs: 112, responses with an error: 0.

### Read latency through the admin API (sequential reads of real traces, ms)

| Stored spans | Engine | `GET /traces/{id}` p50 | p95 | p99 | journey p50 | p95 | p99 | `GET /requests/{id}/trace` p50 | p99 |
|---|---|---|---|---|---|---|---|---|---|
| 999,999 | database | 0.5 | 0.7 | 1.2 | 0.4 | 0.5 | 0.6 | 0.2 | 0.4 |
| 999,999 | analytics_store | 9.5 | 10.9 | 13.8 | 1.8 | 3.6 | 5.0 | 1.5 | 1.9 |
| 9,999,999 | database | 2.1 | 3.6 | 5.6 | 1.1 | 2.0 | 3.0 | 0.2 | 3.0 |
| 9,999,999 | analytics_store | 19.1 | 23.4 | 32.5 | 6.7 | 11.8 | 15.0 | 1.7 | 4.8 |

### Storage per span

| Engine | Stored spans | Bytes on disk | Bytes per span |
|---|---|---|---|
| database (spans and trace summaries, indexes included; real spans of the load runs) | 686,250 | 466,386,944 | 680 |
| analytics_store (spans and summary rows, compressed; real spans of the load and outage runs) | 816,522 | 84,799,268 | 104 |
| database (at 10M, synthetic traces included) | 9,999,999 | 6,450,552,832 | 645 |
| analytics_store (at 10M, synthetic traces included) | 9,999,999 | 862,528,067 | 86 |

### Analytics store outage under load

| Fault | Requests | Errors | p50 ms | p99 ms | written | dropped (write failed) | dropped (queue full) | failed batches | last error during the fault | written again after |
|---|---|---|---|---|---|---|---|---|---|---|
| ClickHouse stopped for 10 s (connections refused) | 30,050 | 0 | 7.05 | 15.25 | 25,226 | 1,181 | 3,643 | 12 | `analytics_store_unreachable` | yes (last_error cleared, batches written) |
| ClickHouse paused for 10 s (a hung server) | 30,050 | 0 | 7.63 | 18.47 | 22,298 | 334 | 7,418 | 2 | `analytics_store_timeout` | yes (last_error cleared, batches written) |

<!-- end of generated tables -->

## Reading

- **Load.** The build with the setting and `engine: database` is indistinguishable from the commit before it: every change sits inside the A/A spread. With `engine: analytics_store`:
  - **PostgreSQL:** WAL records per request fell by 24–27% (median), below the A/A range in every repetition, and PostgreSQL CPU by 12–17% at three points (inside the noise at buffered c=32). What PostgreSQL still writes is the request log row, the ledger and the receipts.
  - **Request path:** gateway CPU per request is unchanged (−3% to +1%), p50 and p99 are within or below the noise, and throughput is +1% to +11%, mostly above the A/A median: PostgreSQL is this rig's limit, and it does less.
  - **ClickHouse:** about 120 µs of CPU per request, against about 30 µs idle in the other builds: about 90 µs for the inserts and their merges.
  - **Completeness:** every response was a 200, and the analytics store held exactly one span of each kind per request served (228,750 of each) and one summary row per trace.
- **Reads.** PostgreSQL answers a single trace from its index in 0.5 ms at a million spans and 2 ms at ten million. ClickHouse has no row index: the trace is found through a bloom filter over the table's granules and costs about 10 ms at a million and 19 ms at ten million, with the summary rows read alongside. The journey, which reads the request log in PostgreSQL either way, takes 1.8 and 6.7 ms against 0.4 and 1.1 ms. Both stay well below what a person notices in the console.
- **Disk.** ClickHouse stores a span, with its summary, in about 100 bytes against about 650–680 in PostgreSQL with its indexes: about a seventh.
- **Outage.** No request failed or waited: p99 stayed within about 1.5× of the quiet runs while ClickHouse was down or hung. Spans of the outage were dropped and counted, never awaited: refused connections fail at once, so most spans waited in the queue and were written when ClickHouse came back (16% lost); a hung server holds each batch for the writer's 5-second timeout, so the queue filled sooner (26% lost). During the fault `/span-store` still answered with the counters and named the cause (`analytics_store_unreachable`, `analytics_store_timeout`); afterwards the writer resumed on its own. Every request served is accounted for: written plus dropped equals requests.

## What was not measured

- A replicated or remote ClickHouse, a managed PostgreSQL, or a network between the gateway and either store: everything ran over loopback on one machine.
- Sampling below 1.0 (both engines would write proportionally less), and spans with retries, guardrails or tools (more spans per request).
- Reads under write load, and trace lists or aggregates (later slices).
- The database engine's behaviour during a PostgreSQL outage: there the request path itself depends on PostgreSQL.

## Reproduce

The rig scripts used for this run are not part of the repository; the method above, `gwbench` and the shipped files are. After editing the JSON, regenerate the tables with `python3 scripts/render-span-engines.py` (`--check` verifies the page).
