Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 39 ยท Writing Data

Batching, Retries and Write Errors

Write to InfluxDB efficiently with batching, flush intervals, retries with jitter, partial write handling, dead-letter queues and idempotent replays.

A script that writes one point per HTTP call works on a laptop and falls over in production. A collector that loses points whenever the network hiccups loses data exactly when you most want it. This lesson shows the three techniques every serious writer uses: batching, retrying and handling bad lines without blocking good ones.

The code below is JavaScript that simulates a server. It shows the logic that official client libraries and Telegraf implement for you, so you can read their settings (batch_size, flush_interval, retry) with understanding. No InfluxDB server is involved.

Why batch

Every write request has a fixed cost: connection, authentication, parsing headers and a durable commit. Sending 10,000 points one by one pays that cost 10,000 times. Sending them in two batches pays it twice. The server also writes larger batches to storage more efficiently and compresses them better.

Strategy Requests for 10,000 points Trade-off
One point per request 10,000 Lowest latency, worst throughput
Batches of 100 100 Good for low rates
Batches of 5,000 2 Best throughput, a failure loses or retries more
Single batch of 10,000+ 1 Risk of 413 and long retries

A batcher therefore has two triggers:

  1. Size trigger: flush when the buffer reaches N points (for example 5,000).
  2. Time trigger: flush at least every T seconds (for example every 1 to 10 s), so that a quiet period does not leave points waiting forever.
JS A batcher with size and time triggers (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Always flush on shutdown. A process that exits with a half-full buffer silently loses those points; official libraries expose a close() or flush() call for exactly this.

Retrying correctly

Retry only temporary failures (429, 5xx, network errors) as described in the HTTP lesson. A good policy has four parts:

  • Exponential back-off: wait 1 s, 2 s, 4 s, 8 s...
  • Jitter: add randomness so that a thousand clients do not retry at the same instant after an outage.
  • A cap on the delay and on the total retry time or buffered bytes.
  • A decision about overflow: when the buffer is full, drop the oldest points, drop the newest, or block the producer. Choose consciously.

Retries are safe because of the same-timestamp rule: re-sending a point with the same measurement, tags and timestamp merges into the existing point rather than creating a duplicate (Timestamps). That is why you should always set explicit timestamps at the source for data you might replay: if you let the server assign the arrival time, a retry creates a second point with a different time.

JS Retries with back-off against a flaky server (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Partial writes and bad lines

Real batches contain the occasional bad line: a type conflict, a missing field, a value that is too long. What happens next depends on the version and settings:

  • 1.x and 2.x: lines that parse are written; the response reports the first failing line with a 400 or 422 status, so a partial write has happened even though you got an error. Do not retry the whole batch blindly: the good lines are already stored (harmless thanks to idempotence), but the bad one will fail again forever.
  • 3.x with accept_partial=true (default): valid lines are written, and the error body lists the rejected lines. With accept_partial=false nothing is written if any line is invalid.

A robust writer therefore:

  1. Reads the error body to find which lines failed.
  2. Retries only temporary failures.
  3. Sends the permanently bad lines to a dead-letter file or queue along with the error message, so a human can fix the producer.
  4. Counts and alerts on rejections (a growing counter means a schema bug).

This stand-in validates lines before sending and routes the bad ones aside:

JS Dead-letter bad lines (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Throughput planning

Estimate before you ship. If 2,000 devices each report 5 fields every 10 seconds, that is 200 points per second (one point per device per interval holds all 5 fields), or 12,000 per minute. At batches of 5,000 and a flush every 5 seconds, each batch carries about 1,000 points, so the time trigger dominates. Use the SQL stand-in to compute volume and daily storage estimates:

SQL Back-of-envelope volume estimate (SQLite stand-in)

Press Run (or Ctrl + Enter) to see the result here.

The 60 bytes per point is an uncompressed line-protocol guess; columnar storage compresses much better, so measure real usage after a trial.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why batch writes?

    Each request has a fixed cost, so larger batches give much higher throughput.
  2. 2. What are the two usual flush triggers of a batcher?

    Flush when N points are buffered or when T seconds have passed, whichever comes first.
  3. 3. Why is retrying a write safe in InfluxDB?

    Idempotent identity (measurement, tag set and timestamp) makes replays harmless, provided you set timestamps at the source.
  4. 4. A batch partly fails with a 422 type conflict. What should you do?

    Permanent errors will not succeed on retry; route them aside and fix the source.

Summary

  • Batch writes by size and time, and flush on shutdown.
  • Retry only 429, 5xx and network errors, with exponential back-off, jitter and a cap.
  • Set timestamps at the source so replays merge instead of duplicating.
  • Treat partial writes carefully: log, dead-letter and alert on rejected lines instead of retrying them.

Next: Writing with Client Libraries