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:
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:
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
sxsinstance 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
- Output formats — which formats stream and how.
- Execution modes and resource profiles — where the streaming work runs (main loop or worker), and the bulk profile.
- Bulk resource profile and worker jobs — backpressure, queue and pagination.
- Asynchronous exports — streaming to object storage.
- Scaling — why bounded memory lets you run many instances.
