In this article
Track outstanding DNS IDs per reused TCP connection, parse framed messages and match replies explicitly when queries complete out of order.
A resolver client reuses one TCP connection for concurrent artifact-host lookups. Replies arrive in a different order from the requests. A FIFO of callers is insufficient: DNS messages have their own identifiers and framing. Track outstanding query identities within each connection so a late response cannot silently become the address result for a newer lookup.
Keep transport and message boundaries
TCP provides an ordered byte stream, not one message per read. DNS over TCP uses a length prefix, so the reader must assemble complete framed messages before interpreting them. A read can contain partial or multiple messages. Correct framing identifies where a response ends; it does not determine which outstanding caller owns that response.
Allocate connection-scoped identifiers
The DNS TCP requirements prohibit reusing an in-flight query’s message ID on the same connection. Keep the connection lifetime and outstanding-ID set with the allocator. An ID available on another connection does not establish availability here. Bound concurrent queries rather than allowing an allocator to overwrite a live mapping when its namespace becomes occupied.
Match responses explicitly
Servers can process pipelined queries concurrently and return responses out of order. Match the message ID and, when the response contains a question, the name, class and type as required. Do not treat successful TCP ordering as request ordering. Keep malformed or unmatched replies observable instead of delivering them to the next waiting caller because its deadline is nearest.
Handle cancellation without ambiguity
A caller timing out does not retract bytes already sent or prove the server abandoned its query. Preserve a safe policy for late replies and identifier reuse. Depending on implementation, draining known outstanding responses or retiring the connection can be preferable to immediate ambiguous reuse. Closing a shared connection also affects other in-flight queries, so account for their outcomes explicitly.
Exercise the reused connection
Test concurrent distinct questions, deliberately reordered replies, fragmented framing and a cancelled caller followed by a late answer in an isolated resolver harness. Keep connection recreation and ID allocation visible. This checks the multiplexing contract rather than DNS network-policy permissions. A correct response association prevents cross-delivery, but still requires normal answer validation, deadline handling and a resolver path capable of reaching the intended authority.
Primary references
IETF DNS over TCP Implementation Requirements.
IETF DNS Message Format and TCP Framing.
IETF DNS over TCP Operational Requirements.
Related reading: Some DNS queries fail: why TCP and UDP port 53 both matter; Slow DNS in a Pod: search domains and ndots.
Earn rental income from GPUs
Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.