mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-16 01:44:03 +03:00
7fffc11077
* server: use a leaky bucket algorithm for bytes-per-second cache rate limiting * Use evalsha properly * Adds redis cache limit parameters to PoliciesConfig * Loads Leaky Bucket Script On Server Start * Adds more redis logging and moves cache update into lua script * reverts setex in lua and adds notes * Refactors cacheStore and adds max TTL and cache size limits * Filter session vars in cache key * WIP * parens * cache-clear-hander POC implementation * cache-clear-hander POC implementation * Pro projectId used as cache key * POC working! * prefixing query-response keys in redis * Add cacheClearer to RedisScripts * Partial implementation of cacheClearer from scripts record * updating tests * [automated] stylish-haskell commit * Adds query look with up with metrics script * Adds missing module and lua script from last commit * Changes redis script module structure to match cache clearing branch * minor change to lua script * cleaning up cache clearing * generalising JsonLog * [automated] stylish-haskell commit * Draft Cache Metrics Endpoint * Adds Cache Metrics Handler * Adds hook handler module * Missed HandlerHook module in last commit * glob * Fixes redis mget bug * Removes cache totals and changes dashes to colons in metric cache keys * Adds query param to clear clear endpoint for deleting specific keys * Adds query param to clear clear endpoint for deleting specific keys * Cache Metrics on query families rather then queries * Replace Set with nub * Base16 Redis Hashes * Query Family Redis Keys With Roles * response headers for cache keys * fixing bug in family key by excluding operation name; using hash for response header instead of entire key * Adds query family to redis cache keys and cache clear endpoint * Fixes queryfamily hash bug * Moves cache endpoints to /pro * Moved cache clear to POST * Refactors cache clear function * Fixes query family format bug * Adds query cache tests and optional --redis-url flag to python test suite * Adds session variable cache test * Update pro changelog * adding documentation for additional caching features * more docs * clearing up units of leaky bucket params * Adds comments to leaky bucket script * removes old todo * Fixes session variable filtering to work with new query rootfield * more advanced defaulting behaviour for bucket rate and capacity. * Updates Docs * Moves Role into QueryFamily hash * Use Aeson for Cache Clear endpoint response * Moves trace to bracket the leaky bucket script * Misc review tweaks * Adds sum type for cache clear query params * Hardcodes RegisReplyLog log level * Update docs/graphql/cloud/response-caching.rst Co-authored-by: Phil Freeman <phil@hasura.io> * new prose for rate limiting docs * [automated] stylish-haskell commit * make rootToSessVarPreds total * [automated] stylish-haskell commit * Fixes out of scope error * Renamed _acRedis to _acCacheStore Co-authored-by: Solomon Bothwell <ssbothwell@gmail.com> Co-authored-by: Lyndon Maydwell <lyndon@sordina.net> Co-authored-by: David Overton <david@hasura.io> Co-authored-by: Stylish Haskell Bot <stylish-haskell@users.noreply.github.com> Co-authored-by: Lyndon Maydwell <lyndon@hasura.io> GitOrigin-RevId: dda5c1a3f902967b3d78310f950541a55fabb1b0
106 lines
3.3 KiB
ReStructuredText
106 lines
3.3 KiB
ReStructuredText
.. meta::
|
|
:description: Query response caching in Hasura Cloud
|
|
:keywords: hasura, docs, cloud, response, caching
|
|
|
|
.. _response_caching:
|
|
|
|
Query response caching
|
|
======================
|
|
|
|
.. contents:: Table of contents
|
|
:backlinks: none
|
|
:depth: 1
|
|
:local:
|
|
|
|
Introduction
|
|
------------
|
|
|
|
Hasura Cloud provides support for caching query responses, in order to
|
|
improve performance for queries which are executed frequently.
|
|
|
|
Cached responses are stored in for a period of time in a LRU (least-recently
|
|
used) cache, and removed from the cache as needed based on usage.
|
|
|
|
A query's response can be cached only if the following conditions hold:
|
|
|
|
- The query does not make use of remote schemas or remote joins
|
|
- The response JSON is under 100KB in size
|
|
|
|
Enable caching
|
|
--------------
|
|
|
|
In order to enable caching for a query response, or to return an existing
|
|
response from the cache (if one exists), simply add the ``@cached`` directive
|
|
to your query:
|
|
|
|
.. code-block:: graphql
|
|
|
|
query MyCachedQuery @cached {
|
|
users {
|
|
id
|
|
name
|
|
}
|
|
}
|
|
|
|
If the response was cached successfully, the HTTP response will include a
|
|
``X-Hasura-TTL`` header, whose value indicates the maximum number of seconds
|
|
for the returned response to remain in the cache.
|
|
|
|
Controlling cache lifetime
|
|
--------------------------
|
|
|
|
The maximum lifetime of an entry in the cache can be controlled using the ``ttl``
|
|
argument to the ``@cached`` query directive. The value is an integer number of seconds:
|
|
|
|
.. code-block:: graphql
|
|
|
|
query MyCachedQuery @cached(ttl: 120) {
|
|
users {
|
|
id
|
|
name
|
|
}
|
|
}
|
|
|
|
By default, a response will be cached with a maximum lifetime of 60 seconds.
|
|
The maximum allowed value is 300 seconds (5 minutes).
|
|
|
|
Rate Limiting
|
|
-------------
|
|
|
|
Cache writes are rate limited, with a rate depending on your plan. The rate
|
|
limit is based on the total number of bytes written to the cache in a sliding
|
|
window. If you exceed the rate limit, the HTTP response will indicate this
|
|
with a 429 (Too Many Requests) error response.
|
|
|
|
Session variables
|
|
-----------------
|
|
|
|
Queries using session variables are able to be cached.
|
|
|
|
Please note:
|
|
|
|
* A session variable will only influence the cache key for a query if it referenced by the execution plan.
|
|
In practice this means that session variables are only factored into cache keys if they are referenced
|
|
in the permissions for a query.
|
|
See https://hasura.io/docs/1.0/graphql/core/api-reference/schema-metadata-api/permission.html
|
|
|
|
Response headers
|
|
----------------
|
|
|
|
When you enable caching for a query, the following headers should be returned in the HTTP response:
|
|
|
|
* ``X-Hasura-Query-Cache-Key`` - Key for cached query response, unique to this query
|
|
* ``X-Hasura-Query-Family-Cache-Key`` - Key for the family of queries (ignores variable values)
|
|
* ``Cache-Control`` - Value: ``max-age={SECONDS}`` - Seconds until cache expires for query
|
|
|
|
These can be used by your application as you see fit, as well as by the cache clearing endpoints.
|
|
|
|
Clearing items from the cache
|
|
-----------------------------
|
|
|
|
A set of endpoints exist to clear items from the cache for the current project:
|
|
|
|
* ``POST /pro/cache/clear`` -- Clears all
|
|
* ``POST /pro/cache/clear?key={HASH}`` -- Clears key hash
|
|
* ``POST /pro/cache/clear?family={FAMILY}`` -- Clears items that match query family (ignoring vairables)
|