Skip to main content

Caching and conditional requests

This page handles client side caching through the usage of the ETag header. Other forms of caching exist, e.g. inside the internal databases, in a proxy between the user and the API etc., but are outside the scope of this article.

What is the ETag header?

The ETag header, short for “entity tag”, is an identifier for a version of an element. It is sent in HEAD and GET responses, and can be used in later requests to determine whether content has changed or stayed the same, i.e. whether data cached by the client is still valid.

Furthermore, the ETag can be used to craft conditional requests - do action x only if the element y has not changed.

See also:

The responses below only show the status line and the headers which matter for caching; the full responses contain more headers, see the endpoint documentation.

Using If-None-Match to avoid a second request

Initial request

As we do not yet know the ETag value associated with the first request, we simply execute the request normally:

curl \
-H 'Authorization: Bearer secret-token:M3WHIDj4q62EY0XiZFMLnv' \
https://api.example.com/02IiNr0EPf9nVLAeSXQS6b16IfFg9aNoX8Vj4

The response contains the element and its ETag:

HTTP/1.1 200 OK
Etag: "Ab5tfHLLWiQ"
{
"type": "Data",
"id": "02IiNr0EPf9nVLAeSXQS6b16IfFg9aNoX8Vj4",
"data": {
"createdAt": "2020-01-01T00:00:00+00:00",
"updatedAt": "2020-01-01T00:00:00+00:00",
"name": "Data",
"scenario": "general.if-match"
}
}

Note: The ETag value itself contains quotes; they are part of the ETag.

Cached request

As we now know the ETag value of the request, we can send a second request with the If-None-Match header and the ETag as its value:

curl \
-H 'Authorization: Bearer secret-token:M3WHIDj4q62EY0XiZFMLnv' \
-H 'If-None-Match: "Ab5tfHLLWiQ"' \
https://api.example.com/02IiNr0EPf9nVLAeSXQS6b16IfFg9aNoX8Vj4

If the element has stayed the same, the API answers 304 Not Modified without a response body, the data which the client has cached is still valid:

HTTP/1.1 304 Not Modified
Etag: "Ab5tfHLLWiQ"

Cached request with an outdated ETag

Should the element have changed, or the ETag not match for any other reason, the API answers like the initial request: 200 OK with the up-to-date element in the body and its current ETag.

curl \
-H 'Authorization: Bearer secret-token:M3WHIDj4q62EY0XiZFMLnv' \
-H 'If-None-Match: "invalid"' \
https://api.example.com/02IiNr0EPf9nVLAeSXQS6b16IfFg9aNoX8Vj4
HTTP/1.1 200 OK
Etag: "Ab5tfHLLWiQ"

Using If-Match to create conditional requests

Conditional requests are useful to avoid lost updates, i.e. when you only want a change to be executed when you know that no other person has changed it in the meantime.

Initial request

Similarly to the If-None-Match-header, we first need to find out the ETag of the element to be updated, e.g. with the same GET request as above, which answers 200 OK with Etag: "Ab5tfHLLWiQ".

Sending a conditional successful request

If the element has not been changed in the meantime, a conditional request will be successful:

curl -X PATCH \
-H 'Authorization: Bearer secret-token:M3WHIDj4q62EY0XiZFMLnv' \
-H 'Content-Type: application/json' \
-H 'If-Match: "Ab5tfHLLWiQ"' \
-d '{
"key": "some update to the element"
}' \
https://api.example.com/02IiNr0EPf9nVLAeSXQS6b16IfFg9aNoX8Vj4
HTTP/1.1 204 No Content

The condition matches, so the update is applied. The element’s ETag has changed as a side effect, so this same If-Match value can not be reused for a second update.

Sending a conditional invalid request

If we repeat the same request with the old ETag, which is no longer valid, as we have just updated the element, the request fails and nothing is changed:

curl -X PATCH \
-H 'Authorization: Bearer secret-token:M3WHIDj4q62EY0XiZFMLnv' \
-H 'Content-Type: application/json' \
-H 'If-Match: "Ab5tfHLLWiQ"' \
-d '{
"key": "some update to the element"
}' \
https://api.example.com/02IiNr0EPf9nVLAeSXQS6b16IfFg9aNoX8Vj4
HTTP/1.1 412 Precondition Failed

Fetch the element again to see the current state and its new ETag, then decide whether to repeat the update.

How Ember Nexus calculates the ETag

The value of the Etag header is calculated as the XXH3 hash from the identifiers and updatedAt timestamps of all involved elements, as well as the instance’s ETag seed (config value cache.etagSeed, see documentation).

We use the XXH3 hash due to its performance benefits.

As the updatedAt timestamp uses microsecond precision, the resulting Etag will be different as long as updates happen at least one microsecond apart.

Collections larger than the configured cache.etagUpperLimitInCollectionEndpoints (see the configuration reference) do not receive an ETag at all, as computing one for very large collections would cause performance problems.

Details

Preconditions and errors

If-Match: * and If-None-Match: * are supported as well. On write endpoints a failing precondition answers 412 before any 409 Conflict is evaluated, and without the required access a conditional request answers 404 (403 for elements which the user can see but not change). See the file reference for the file-specific rules.

If-Match and If-None-Match accept at most 5 entity tags each; a header with more answers 400 Bad Request.

Other representations

Responses in another representation than the default one carry the name of the representation in their ETag: an element with the ETag "YgbFC4u5EgC" has the ETag "YgbFC4u5EgC-wkt" if the request asked for geo values as WKT with Prefer: geometry=wkt, see response format. These responses also carry Vary: Prefer, so caches keep both representations apart. If-None-Match compares the ETag of the requested representation, If-Match accepts both forms, as both describe the same state of the element.

The schema related nodes (Schema, SchemaRevision and Migration) have a reduced view for tokens without the capability schema.manage, see schemas. Where a response can contain them, i.e. for GET /schema/<type>, GET /schema, GET /schema/<type>/revisions and GET /<uuid> of such a node, the reduced view has the suffix -min ("YgbFC4u5EgC-min"), the full view has none. The suffixes are sorted and combined, e.g. "YgbFC4u5EgC-min-wkt". These responses carry Vary: Authorization, as the same URL answers differently depending on the token. If-Match accepts all forms, as they describe the same state.

All values of Vary are sent in a single header line, e.g. Vary: Origin, Authorization, Prefer, Accept-Encoding; Accept-Encoding is part of every line, as responses are compressed depending on it.

Schemas and migrations

The collections GET /schema, GET /schema/<type>/revisions and GET /migration have an ETag as well, which changes when a node of the collection is created or changes (the status of a migration included). Unlike the other collections they only lose the ETag above 5000 nodes.

Schemas can be changed conditionally: PUT /schema/<type>, PATCH /schema/<type> and DELETE /schema/<type> accept If-Match with the ETag of the schema, and answer 412 if it does not match. A PUT which would create the schema has no current ETag, so every If-Match fails with 412; this is how to make sure that a schema is not replaced by accident.

A single migration, GET /migration/<uuid>, has an ETag which changes with its status, so polling it with If-None-Match answers 304 until something happens; PATCH /migration/<uuid> accepts If-Match with it.

Other caching layers

Besides client-side ETag caching, two other cache-related aspects exist:

  • The internal databases Ember Nexus depends on, e.g. Neo4j and Elasticsearch, have their own caches. They may show slower “cold start” performance until their own caches are warmed up.
  • Ember Nexus itself stores/caches computed ETag values. They can be cleared and recalculated with the cache:clear:etag command.