Why does the server prefer to return an early status code when it receives a request before the connection is fully established?
The connection has not been fully established when the request was received, why does the server prefer to return an early status code, and the point of consideration is whether this request can be replayed.This scenario comes from the early data mechanism of TLS: the client and server have not completed the full handshake, and the client sends the content to ask first, so the server receives the request before the connection is fully established.In response to such a request, the server can handle it normally or reject it first, clearly expressing its reluctance to take the risk of being replayed using status code 425.Whether to take this risk depends on whether the saved waiting value is worth it, and also depends on whether the request itself will cause a problem if it is executed repeatedly.
What does the early data save?
Early data, also known as 0-RTT data, allows the client to send a request in the round trip where the handshake is not yet completed, and the connection is just starting, without having to wait for the TLS handshake to be completed as in the regular process.The time saved is a complete round trip wait: in the past, we had to shake hands first and then send a request, now the handshake and request are squeezed into the same round trip to push forward. By definition, the client has already interacted with the server, so it can reuse this familiarity to send data in advance;And in the early data, the client sending the request was not required to add the Early-Data header extra, the request could describe what it wanted to do as usual.
What risks do the saved round trips bring?
The saved waiting is not without cost. The early data could be sent ahead of time because it skipped some protection that was only completed in the full handshake, the request arrived at the server before the connection was fully confirmed for identity and freshness.This leaves room for replay: one party has the opportunity to intercept this prematurely sent request, and then resubmit it unchanged to the server, causing the same operation to be executed twice.For some requests, it does not matter if they are executed repeatedly, but for others, it is an accident. The size of the risk is not fixed at the protocol layer, but follows the type of request, and the subsequent choices are determined by it.
What does this status code express?
Status code 425 Too Early is a client error status code indicating that the server is unwilling to risk processing a potentially replayed request, preferring to reject the request first to avoid potential replay attacks.Observe the tone: This is not a connection error, nor is it a mistake in the request format, but rather the server is主动表态—I admit that I received this request that came from the early data, but now I am unwilling to execute it.The server returns control to the client, allowing it to either send it again after the handshake is completely finished or take a more secure path. An early status code is a protective posture of retreat rather than a record of failure.
Which requests are suitable to send in early data?
Since the risk is concentrated on repeated execution, whether the early data judgment criteria can be clarified is also clear: to see if executing the request in the same way will change the current situation. Requests that only read information and whose results do not change with the number of times executed can only return the same content when replayed, with limited cost, and are suitable for抢占early data a bit of starting speed. Once the request will change the server side status - such as placing an order, changing a password, and deduction of this kind of write operation - does not belong to the category suitable for sending in advance, and the server is more inclined to directly block it back.
Why do we need to treat idempotent and non-idempotent separately
To summarize the above judgment into a single statement, it is that idempotent and non-idempotent requests should be treated separately. Idempotent requests, when executed repeatedly, result in the same outcome, while non-idempotent requests, when executed repeatedly, result in repeated effects: one order becomes two, and one deduction becomes two deductions.Because of this boundary, even if it means making an extra trip and taking a little more starting time, it is not a global switch, but a boundary based on request type. The safe approach is to allow only the secure part of the early data, and reject the rest first.Cut off all requests after the handshake, unnecessarily losing the time saved; putting all into early data, also exposing dangerous write operations to replay. The mature configuration is to draw a clear line between the two types of requests.
How to make choices at this level for a service built with Go
To be specific in a project, taking AnQiCMS as an example, it is based on Go language and built using the Iris framework. In such deployments, the TLS and connection layer are usually handed over to the front-end Web server for processing, when the handshake is completed, whether early data is enabled, and which requests are allowed to pass before the connection is fully established are often decided by the front-end server and the gateway, and the program code rarely directly touches this part.Therefore, the trade-offs related to 425 are more reflected in the front-end configuration rather than in the business logic. For services written in Go language, it is important to clarify the idempotent boundaries, hand over the write operations that should not be processed in advance to the channel after the handshake is completed, and entrust the handshake and replay protection to the front-end layer for unified handling, which is more in line with the division of labor of this technology stack.
Common questions
What exactly is saved by early data
What is saved is the round-trip delay brought by the handshake. The conventional process requires completing the TLS handshake before sending a request, but early data allows the client to send the request as soon as the connection starts and the handshake is not yet completed. The client's previous interaction with the server is the premise for it to send in advance.
Why does the server choose to return an early status code instead of handling it normally
Because there is a possibility of the request being replayed. 425 indicates that the server is unwilling to risk processing a potentially replayed request and prefers to reject first. This refusal avoids duplicate effects for write operations that change the state; for read-only idempotent requests, the server is more likely to process them directly.
Do we need to add an Early-Data header specifically for requests in early data?
As defined, it is not necessary. In early data, the client sending the request is not required to include the Early-Data header, the request can be described normally according to its own semantics. Whether it will be blocked back by an early status code depends on whether the request is idempotent and whether the server is willing to take the risk of replay, rather than whether this header is added or not.