feat(remotewriter): mark remote write POSTs with idempotency key and attempt - #471
Merged
Conversation
…attempt
Replaying a remote write POST is a regular part of the delivery contract:
the send loop resends undelivered messages of a batch with the very same
payload, and the Go transport may replay a request on its own. Nothing on
the wire said so, which left a receiver unable to tell a replay from new
data, and left the send error log without the facts needed to locate the
failed message.
Every message POST now carries X-Idempotency-Key, built from destination,
head ID, target segment ID and message index, so it repeats over all the
retries of that message. Retry-Attempt is filled in as well - it was
hardcoded to 0, so the attempt number of the send loop never reached the
receiver.
Note that this header name also opts the POST into replays made by
http.Transport (Request.isReplayable), which happen below our round tripper
with identical headers. The idempotency key is therefore the only unit of
deduplication, and Retry-Attempt stays a diagnostic of our own loop. To make
that wire side visible, connections_total{state="new"|"reused"} counts the
connections requests go through via httptrace.
The failed send log now reports the destination URL, the attempt number, the
request duration and the message size in bytes and samples.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
u-veles-a
reviewed
Aug 12, 2026
u-veles-a
approved these changes
Aug 12, 2026
Measure the send duration with the injected clock instead of mixing it with the real one, stop shadowing the message list variable in the send goroutine, and clean up the createClient doc comment and its error typo. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
vporoshok
enabled auto-merge (squash)
August 12, 2026 18:29
u-veles-a
pushed a commit
that referenced
this pull request
Aug 13, 2026
…attempt (#471) * feat(remotewriter): mark remote write POSTs with idempotency key and attempt Replaying a remote write POST is a regular part of the delivery contract: the send loop resends undelivered messages of a batch with the very same payload, and the Go transport may replay a request on its own. Nothing on the wire said so, which left a receiver unable to tell a replay from new data, and left the send error log without the facts needed to locate the failed message. Every message POST now carries X-Idempotency-Key, built from destination, head ID, target segment ID and message index, so it repeats over all the retries of that message. Retry-Attempt is filled in as well - it was hardcoded to 0, so the attempt number of the send loop never reached the receiver. Note that this header name also opts the POST into replays made by http.Transport (Request.isReplayable), which happen below our round tripper with identical headers. The idempotency key is therefore the only unit of deduplication, and Retry-Attempt stays a diagnostic of our own loop. To make that wire side visible, connections_total{state="new"|"reused"} counts the connections requests go through via httptrace. The failed send log now reports the destination URL, the attempt number, the request duration and the message size in bytes and samples. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * fix(remotewriter): address review notes on the delivery marks Measure the send duration with the injected clock instead of mixing it with the real one, stop shadowing the message list variable in the send goroutine, and clean up the createClient doc comment and its error typo. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Replaying a remote write POST is a regular part of the delivery contract here:
Iterator.SendMessageresends undelivered messages of a batch with the very same payload, and the Go transport may replay a
request on its own. Nothing on the wire said so:
Retry-Attemptwas hardcoded to0inprotobufWriter.Write, so the attempt number never left theprocess, even though
remote.Client.Storealready knows how to send it;failed to send protobuf: <err>— no endpoint, no attempt, no size.What
Delivery marks. Both marks of a single delivery travel in the request context (
delivery.go) andreach the wire as headers:
X-Idempotency-Key=<destination>/<headID>/<targetSegmentID>/<messageIndex>, set bydeliveryRoundTripper. Messages of a batch are numbered, and a non-empty batch always advancestargetSegmentID, so the key repeats over all the retries of that message.Retry-Attempt, passed toStoreas the attempt number of the send loop (0for the first delivery).Upstream files are untouched: the round tripper is wrapped around the transport of the client built in
createClient.Semantics worth knowing. This exact header name also opts the POST into replays made by the Go
transport itself —
Request.isReplayabletreats a POST carrying
Idempotency-KeyorX-Idempotency-Keyas replayable, sohttp.Transportresends it when a reused connection fails (stale keep-alive, server closing an idle connection, HTTP/2
refused stream). Those replays happen inside a single
Client.Do, below our round tripper, and go outbyte for byte identical — same key, same
Retry-Attempt. Hence:Retry-Attemptis a diagnostic of our send loop, not a count of times a payload reached the receiver.This is documented in the
IdempotencyKeyHeaderdoc comment and in the package README.New metric.
prometheus_remote_storage_connections_total{remote_name,url,state="new"|"reused"}counts the connections requests go through (
httptrace.GotConn). Since the trace fires per transportattempt, it also exposes the replays above: more than one connection per send means the transport
resent the request. Growth of
state="new"under steady traffic means connections do not survivekeep-alive — the usual source of silent duplicates.
Send error log now reports everything needed to locate the message:
The URL is the redacted destination URL, the same value as in the metric labels.
Limitations
The key is stable within the life of an iterator. After a restart the same segments may be cut into
different batches (
targetSegmentIDand the number of shards float), so keys of the re-read data maydiffer from those of the first run. A content-derived key would fix that and can be a follow-up.
No receiver reads either header yet.
Tests
delivery_test.go, all green undergo test -tags stringlabels -race:(
{dst/head-42/7/0, 0},{dst/head-42/7/1, 0}, then{dst/head-42/7/0, 1}), and the failed attemptis logged with url / attempt / duration / bytes / samples;
protobufWriterpasses the attempt from the context toStore, and0when there are no marks;createClient+httptest: both headers arrive on the wire (this also proves thewrapper is not lost behind
otelhttp);state="new", the secondstate="reused".🤖 Generated with Claude Code