

# Caching

**Welcome to the new AWS Amplify**  
Amplify now puts hosting, a Postgres database, and sign-in in one AWS account. Deploy from your terminal or from Git, check every change in a preview first, and roll back in one step if something breaks.  
Looking for Amplify Hosting? See the [Amplify Hosting User Guide](https://docs.aws.amazon.com/amplify/hosting/userguide/welcome.html).

With AWS Amplify, you can cache your app’s responses at the edge. Amplify then serves repeat requests without running your app, so pages load faster and your app does less work.

## What Amplify caches
<a name="caching-what"></a>

Your app decides what is cached through the `Cache-Control` header on each response. Your framework sets this header for you in most cases, for example for prerendered pages and for pages that use incremental static regeneration (ISR). Amplify handles your app’s responses in the following ways:
+  **Static build assets** – Amplify caches hashed build assets, such as the files under `/_next/static/` for Next.js and `/_astro/` for Astro, for a long time. Their names change whenever their content changes.
+  **Responses that allow shared caching** – To cache a server-rendered response at the edge, send a `Cache-Control` header with `s-maxage`. Its value is the number of seconds that the edge can keep the response. To keep serving the cached response as a fresh one renders in the background, add `stale-while-revalidate`. The following `Cache-Control` header allows shared caching:

  ```
  Cache-Control: public, s-maxage=60, stale-while-revalidate=600
  ```
+  **Responses that aren’t cached** – Amplify doesn’t store a response with `Cache-Control: private` or `no-store` at the edge. It also doesn’t store a response that sets a cookie. If a route sets a cookie on every response, then it renders on every request.

For framework-specific caching support, including ISR and on-demand revalidation, see [SSR supported features](ssr-supported-features.md).

## Cookies and the cache key
<a name="caching-cookies"></a>

By default, cookies aren’t part of the cache key. Amplify serves a cached response to every visitor, whatever cookies they send. Send `Cache-Control: private` or `no-store` on any response that depends on who is signed in.

To cache a separate copy of each response for each combination of cookies, set `cache_key` in `amplify.jsonc`. In the following example, replace `my-app` with your app’s name:

```jsonc
{
  "name": "my-app",
  // Key the cache on every cookie in the request.
  "cache_key": {
    "cookies": { "include": ["*"] }
  }
}
```

The example sets `include` to `["*"]`. This keys the cache on every cookie in the request. To key the cache on no cookies, set `include` to `[]`. You can’t list individual cookies. When you key on every cookie, your cache hit rate drops. Visitors with different cookies get separate copies. The setting takes effect on your next deployment.

## Refresh cached content
<a name="caching-refresh"></a>

Your framework’s own APIs refresh cached content, for example `revalidateTag` and `revalidatePath` in Next.js. You can also refresh cached content yourself, by cache tag. A cache tag is a label that your framework attaches to cached content, such as the tags that you pass to `revalidateTag` in Next.js.

The following table describes the two ways to refresh content and when to use each.


| Action | What happens to matching entries | When to use it | 
| --- | --- | --- | 
| Revalidate | Amplify marks them as stale. The next request gets the cached response immediately, while a fresh version renders in the background. | Routine content updates, where serving the previous version briefly is fine. | 
| Purge | Amplify removes them. The next request for each entry waits for a fresh render. | Content that must not be served again, such as a wrong price. | 

Both act on one deployment. The change can take a few seconds to reach every AWS Region.

### Refresh cached content in the console
<a name="caching-refresh-console"></a>

To refresh cached content in the console:

1. Sign in to the Amplify console.

1. Choose an app.

1. In the left navigation pane, choose **Deployments**.

1. Next to the active deployment, choose the actions menu.

1. Choose **Revalidate cache** or **Purge cache**.

1. For **Cache tags**, enter the tags to refresh, for example `products`.

1. Choose **Revalidate** or **Purge**.

### Refresh cached content using the CLI
<a name="caching-refresh-cli"></a>

The following AWS Command Line Interface (AWS CLI) command revalidates the entries tagged `products`. It acts on the `production` environment’s active deployment. Replace `products` with your cache tag and `production` with your environment name. Without `--env`, the command uses the `preview` environment.

```bash
aws amplifygen3 cache revalidate --tag products --env production
```

To purge the entries tagged `prices`, run the following AWS CLI command. Replace `prices` with your cache tag and `production` with your environment name:

```bash
aws amplifygen3 cache purge --tag prices --env production
```
+ Repeat `--tag` to act on several tags. An entry that matches any of the tags is affected. You can pass up to 128 tags, each up to 256 characters.
+ Add `--deployment-id` to act on a specific deployment instead of the active one.
+ Add `--expiration-period` to `cache revalidate` to set a new expiration time, in seconds, for the revalidated entries.

For all options, see [AWS Command Line Interface support](aws-cli-support-chapter.md).