hasura.io Open in urlscan Pro
2606:4700::6812:77a  Public Scan

Submitted URL: https://link.hasura.io/NjMxLUhNTi00OTIAAAGEwi-264I6JX8URk-NBZmyHZ8e5stugeKEWE2d2MvSNJjiOpFxm5u8bM_q8xIYakozu4rb-fY=
Effective URL: https://hasura.io/docs/latest/graphql/cloud/response-caching/
Submission: On June 02 via api from CH — Scanned from DE

Form analysis 2 forms found in the DOM

<form id="mktoForm_1261" style="display: none; font-family: Helvetica, Arial, sans-serif; font-size: 13px; color: rgb(51, 51, 51); width: 1px;" novalidate="novalidate" class="mktoForm mktoHasWidth mktoLayoutLeft">
  <style type="text/css">
    .mktoForm .mktoButtonWrap.mktoSimple .mktoButton {
      color: #fff;
      border: 1px solid #75ae4c;
      padding: 0.4em 1em;
      font-size: 1em;
      background-color: #99c47c;
      background-image: -webkit-gradient(linear, left top, left bottom, from(#99c47c), to(#75ae4c));
      background-image: -webkit-linear-gradient(top, #99c47c, #75ae4c);
      background-image: -moz-linear-gradient(top, #99c47c, #75ae4c);
      background-image: linear-gradient(to bottom, #99c47c, #75ae4c);
    }

    .mktoForm .mktoButtonWrap.mktoSimple .mktoButton:hover {
      border: 1px solid #447f19;
    }

    .mktoForm .mktoButtonWrap.mktoSimple .mktoButton:focus {
      outline: none;
      border: 1px solid #447f19;
    }

    .mktoForm .mktoButtonWrap.mktoSimple .mktoButton:active {
      background-color: #75ae4c;
      background-image: -webkit-gradient(linear, left top, left bottom, from(#75ae4c), to(#99c47c));
      background-image: -webkit-linear-gradient(top, #75ae4c, #99c47c);
      background-image: -moz-linear-gradient(top, #75ae4c, #99c47c);
      background-image: linear-gradient(to bottom, #75ae4c, #99c47c);
    }
  </style>
  <div class="mktoButtonRow"><span class="mktoButtonWrap mktoSimple" style="margin-left: 120px;"><button type="submit" class="mktoButton">Submit</button></span></div><input type="hidden" name="formid" class="mktoField mktoFieldDescriptor"
    value="1261"><input type="hidden" name="munchkinId" class="mktoField mktoFieldDescriptor" value="631-HMN-492">
</form>

<form style="display: none; font-family: Helvetica, Arial, sans-serif; font-size: 13px; color: rgb(51, 51, 51); visibility: hidden; position: absolute; top: -500px; left: -1000px; width: 1600px;" novalidate="novalidate"
  class="mktoForm mktoHasWidth mktoLayoutLeft"></form>

Text Content

Skip to main content
Docs
 * Hasura GraphQL Engine
 * Hasura Cloud

ProductBlogTutorials
v2.x
 * v2.x
 * v1.x

SearchK
PricingLoginGet Started


Hasura CoreHasura Cloud
 * Hasura Cloud
   * Index
   * Getting Started
   * Projects & collaborators
   * Metrics
   * API Security
   * Regression tests
   * Read replicas
   * Query response caching
   * Distributed tracing
   * Query Tags
   * Preview apps
   * Dedicated VPC
   * Billing
   * Account management
   * Hasura Pro CLI
   * API Reference
   * Glossary

 * 🏠
 * Hasura Cloud
 * Query response caching

Version: v2.x
On this page


QUERY RESPONSE CACHING


INTRODUCTION

Hasura Cloud provides support for caching query responses, in order to improve
performance for queries which are executed frequently. This includes actions and
queries against remote schemas.

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 that has forward_client_headers
   (see RemoteSchemaDef) set to true.
 * The query isn't an Action that has forward_client_headers (see
   ActionDefinition) set to true.
 * The response JSON is under 100KB in size

SUPPORT

Query response caching is available for Hasura Cloud projects on the Free tier
and above.


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:

query MyCachedQuery @cached {
  users {
    id
    name
  }
}


If the response was cached successfully, the HTTP response will include a
Cache-Control header, whose value (max-age={SECONDS}) 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:

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).


FORCING THE CACHE TO REFRESH

The cache entry can be forced to refresh, regardless of the maximum lifetime
using the refresh argument to @cached. The value is a boolean:

query MyCachedQuery @cached(refresh: true) {
  users {
    id
    name
  }
}



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 warning header: "Warning: 199 - cache-store-capacity-exceeded".


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/latest/graphql/core/api-reference/schema-metadata-api/permission/


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 variables)

Edit this page

Previous
Read replicas
Next
Distributed tracing
 * Introduction
 * Enable caching
 * Controlling cache lifetime
 * Forcing the cache to refresh
 * Rate Limiting
 * Session variables
 * Response headers
 * Clearing items from the cache

DID YOU FIND THIS PAGE HELPFUL?




START WITH GRAPHQL ON HASURA FOR FREE

 * Build apps and APIs 10x faster
 * Built-in authorization and caching
 * 8x more performant than hand-rolled APIs

Try GraphQL with Hasura


© 2022 Hasura Inc. All rights reserved

Submit