Streaming

A data query can return anything from a handful of observations to hundreds of millions. sxs never loads a response into memory: it streams observations from ClickHouse to the client, for every format. Result size therefore has no impact on the memory used by an sxs instance.

Principle

A data response is a pipeline, not a buffer:

ClickHouse row stream → (optional) SDMX builder → HTTP response

Rows are read from ClickHouse, transformed on the fly and written to the socket as they are produced, with backpressure all the way through: if the client (or the database) is slower than the producer, the stream pauses instead of queueing in memory.

How much work happens in the middle depends on the format:

OutputPath
SDMX-JSON (json)streamed through the JavaScript SDMX builder
SDMX-CSV (csv, csvfilewithlabels)streamed through the JavaScript SDMX builder
Native CSV (format=csv when enabled)streamed directly by ClickHouse
Parquet / Arrow / ArrowStream / Protobufstreamed directly by ClickHouse (no JavaScript builder)

Native formats are produced and streamed by ClickHouse itself, so there is almost no per-row work in sxs — see Output formats. arrowstream is a true incremental IPC stream, well suited to large analytical extracts.

The same principle applies on the write side: an ingestion reads its source (CSV/Parquet/JSON from a bucket or a stream) and processes it as a stream, so a multi-gigabyte load does not require a multi-gigabyte process.

Why

  • Bounded memory — a large response does not inflate the process; the memory used depends on the pipeline buffers, not on the result size.
  • Small instances — an sxs instance keeps a small footprint, so many of them can serve an HTTP farm (see Scaling).
  • Backpressure — a slow client or a slow database slows the pipeline down instead of accumulating data in memory.
  • Early bytes — the client starts receiving data before the query has finished.

How to use it

Nothing to do for normal queries

Streaming is automatic: request JSON or CSV and sxs streams the response. The only bound you choose is how much you ask for.

Bound a response with pagination

Use limit (and offset) to cap a single response, and rely on the client to page. Very large synchronous responses are rejected with 413 above SYNC_MAX_OBSERVATIONS. See Bulk resource profile and worker jobs.

Prefer native formats for large extracts

For big extracts, native formats (Parquet, Arrow, ArrowStream) stream directly from ClickHouse and avoid the JavaScript builder entirely. They are the cheapest way to pull a lot of data. See Output formats.

Use exports for really large or recurring extracts

When a result is too large or too slow for a synchronous response, use an asynchronous export: sxs streams the data straight into an object-storage file and gives you a durable job to track and download. See Asynchronous exports.

Uploads stream too

Ingestion streams its input, so large loads are memory-safe as well. See Ingestion queue.

See also