QuestDB Python Client in Practice: Fast ILP Writes and pandas Ingestion
You cannot curl on every single write from a Python service. There are three paths for writing data into QuestDB, in recommended order:
| Path | Protocol / port | Batching | Use when |
|---|---|---|---|
Official questdb client | TCP 9009 | built-in buffer + backpressure | high-frequency online writes |
| InfluxDB-compatible HTTP | 9092 | manual line joining | scripts, cross-language, zero-dependency |
| Raw socket | TCP 9009 | fully self-managed | you need to control flush cadence |
1. The official client: first choice
pip install questdb
The official Python client is a Rust binding with a built-in write buffer. Three lines get you writing:
from questdb import QuestDB
with QuestDB("127.0.0.1", 9009) as qdb:
qdb.write_line("cpu,host=web1,dc=osu usage=0.575 1581020590000000000")
qdb.write_lines([
"cpu,host=web2 usage=0.630 1581020591000000000",
"cpu,host=web2 usage=0.580 1581020592000000000",
])
Two points that matter:
write_linesbeats row-by-rowwrite_line: pass a list in one call and the client batches the network round-trip. On local Docker, a batch of 1,000 lines is an order of magnitude faster than a singlewrite_lineper row.- Context manager closes the connection on exit. For long-running services, keep the connection and write in the background; only reconnect on failure.
2. The HTTP path: the 9092 compatible port
No dependency, or you are in an HTTP-only environment (Serverless, edge functions) — QuestDB’s InfluxDB-compatible port works as-is:
import requests
def write_ilp_http(lines: list[str], db: str = "metrics") -> None:
requests.post(
"http://127.0.0.1:9092/write",
params={"db": db, "precision": "ns"},
data="\n".join(lines) + "\n",
headers={"Content-Type": "text/plain"},
)
The InfluxDB 2.x style is also supported: POST /api/v2/write?org=org&bucket=metrics&precision=ns. Watch out: the precision parameter must match the unit of the timestamps in the line protocol. The most common failure is a nanosecond timestamp interpreted as milliseconds — your data lands in the year 58,000 AD.
3. Bulk pandas backfills
Historical backfills (a full year of metering CSVs, for example) are best done with DataFrames. The official client’s write_df turns a DataFrame into batched ILP:
import pandas as pd
from questdb import QuestDB
df = pd.read_csv("metering_2025.csv", parse_dates=["ts"])
with QuestDB("127.0.0.1", 9009) as qdb:
for i in range(0, len(df), 100_000):
qdb.write_df(df.iloc[i:i + 100_000], ts_column="ts")
Prerequisites: the timestamp column must be datetime64[ns] (which is what parse_dates gives you by default); a 100k-row chunk is the sweet spot between memory pressure and batch efficiency — too big and single-batch memory spikes, too small and round-trips stop amortizing.
4. Three batching rules
- Never one row per request: every HTTP request carries round-trip latency; at high frequency the network is the bottleneck. TCP batching amortizes it to near zero.
- One timestamp unit per table: nanoseconds everywhere, or milliseconds everywhere. Mixing them is data disorder.
- Cap line length: ILP single lines have a size limit (the write buffer); high-cardinality metrics should be split across lines.
5. Failure handling: retries and dedupe
ILP is fire-and-forget — there is no per-row ack. After a network blip and reconnect, a retry may have sent the same batch twice. So keep a deterministic dedupe rule on the read side:
-- on duplicate ts, keep the first row
SELECT ts, symbol, FIRST(price) AS price
FROM ticks
GROUP BY ts, symbol;
On the write side, the “in-memory buffer + retry on failure + resend after reconnect” pattern:
import time
from questdb import QuestDB
pending: list[str] = []
def flush(qdb) -> None:
while True:
try:
qdb.write_lines(pending)
pending.clear()
break
except Exception:
time.sleep(0.5)
def safe_write(qdb, line: str) -> None:
pending.append(line)
if len(pending) >= 500:
flush(qdb)
And use event time for timestamps, not arrival time: a gateway clock drifting by minutes means out-of-order writes (QuestDB accepts them but flags the table). NTP alignment is the cheap, one-time fix.
6. Pitfall list
- Row-by-row writes without batching — latency-bound, throughput collapses.
- Mixing ns/ms units inside one table.
- The DataFrame
tscolumn is notdatetime64[ns]—write_dferrors or truncates precision. - No connection resilience — one network blip kills the process.
- Arrival-time timestamps — clock drift creates out-of-order writes.
7. What next
- Write concurrency and memory knobs → our ILP high-concurrency tuning article.
- Batch consumption from message queues → our QuestDB + Kafka pipeline article.
- Candles from backtest data → our tick-to-K-line article.