# ETag

## What it is

An ETag is an HTTP validator for a particular representation of a resource. The
server sends a tag with a response so another request can refer to the version
the client already received. Clients treat the value as opaque. A server may
derive it from its own representation identity, but the client can use the
validator without knowing how it was generated.

A strong validator supports comparison where exact representation data matters.
A weak ETag indicates that the server considers the representations equivalent
for certain operations even though their data can differ. In the HTTP header
syntax a weak validator has a `W/` prefix. Use the comparison required by the
request condition. A check that needs strong comparison cannot gain that
guarantee from a weak ETag.

Conditional retrieval is a common use. A client can send `If-None-Match` with a
validator it has. For a matching representation, a `GET` or `HEAD` request can
receive status `304`, which lets an existing stored response be reused.
Conditional writes can use `If-Match` to require that the resource still matches
a strong validator before an update. A failed condition can produce status
`412`. The server still needs to apply ordinary access checks for the operation.

## Why it matters

A reader who returns to an article may already have its body. Repeating the
transfer adds latency and work. A validator allows the server to confirm that
the selected representation remains current. It also gives the reader a way to
fetch a changed version when the stored validator no longer matches. The cache
policy determines when to ask the server, and the validator helps it answer.

A shared document editor can use validators for a different decision. Two people
may read the same version. If one saves an update, the other can submit a
conditional write based on the version they originally edited. When the stored
version has changed, the service can refuse the second condition and offer a way
to review the new document. The user can see and resolve the editing conflict
while the newer document stays intact.

Representation identity needs care when there are variants. A URL can serve a
compressed response and an uncompressed one. A locale, media type or selection
rule can also affect the data. A validator has to be meaningful for the
representation the condition refers to. Include that boundary in tests,
particularly when an intermediary adds compression or another transformation.

## What it looks like in practice

Consider a published help page. Load it through its public address and retain
both the body and ETag. Send a conditional GET with the stored tag. Verify that
a matching representation produces the expected status and that the response
carries the metadata the cache needs to use its previous copy. Change the
document through the normal release path and repeat the condition. The client
should receive the updated body and a meaningful validator for it.

For an editing example, read a record and remember its strong validator. Apply
an update through another authorised operation, then submit the original edit
with its older tag as a condition. Verify the failure result and read the stored
record back. A test that checks only a status code can miss a server that
applied the write before rejecting the response. Check both the client-facing
outcome and durable state.

Engineer's
[small-server case study](/portfolio/a-whole-company-on-one-small-server/)
describes hosting public content with limited resources. Conditional retrieval
is an example of making a repeated read economical on such a service. Measure it
at the public route and verify the behaviour after content changes. The
implementation can give returning readers current content while avoiding a body
transfer when the version is still valid.

Test both request conditions using real clients or a reproducible fixture.
Observe a returning browser, then a request with a deliberately older tag.
Include a public path that passes through the service's normal compression and
routing. The tests show which layer produced the validator and which
representation the comparison applied to.

## Where it goes wrong

A validator generated from a resource's name or deployment timestamp can have a
misleading relationship to the bytes. Two responses might share the same tag
even though the representation differs. Other outputs can acquire new tags
unnecessarily on every build. Tie validator generation to the comparison the
application promises. Include compressed variants and responses where only an
important generated field changes.

Conditional writes need their comparison in the same operation that changes
state. An application can read the current version, check the tag and execute a
later unconditional update. Another writer then has room to change the record
between those steps. Arrange for the update to check the version it changes and
test two overlapping requests. An expected conflict should leave the existing
edit intact.

An intermediary can generate, adjust or remove a validator while applying a
transformation. Capture the response seen by the client after compression and
routing. Compare the tags for the supported variants and reproduce the
conditional request through that same boundary. This prevents a test from
certifying a private origin response whose validator never reaches the public
client. If a cache reuses the stored body, inspect its metadata too. The test
should show that the retained bytes match the representation the client
requested.

Finally, distinguish the version of a representation from permission to use it.
A cached tag says what content a client observed and supplies no authority.
Protect the request through the same access checks the normal route uses. A
useful validator has a clear resource scope, a documented comparison and a test
that can demonstrate what happens after an update.

## Further reading

- [RFC 9110 on entity tags](https://www.rfc-editor.org/rfc/rfc9110.html#section-8.8.3).
  Defines strong and weak entity-tag semantics.
- [Mozilla's ETag documentation](https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/ETag).
  Shows practical validation and concurrent editing examples.
- [Amazon S3 conditional writes](https://docs.aws.amazon.com/AmazonS3/latest/userguide/conditional-writes.html).
  Documents precondition checking for an object store.

<https://engineer.company/glossary/etag/>
