Skip to main content
A cache keeps the result of slow work, such as a report or a call to another API, so the next request reads it instead of computing it again. On Sevalla, the cache is a Redis database that the application reaches over an internal connection, so every instance of the web process shares it. FastAPI uses it through redis-py’s asyncio client.

Before you begin

Create a Redis database

1

Create the database

In the sidebar, click Databases, then click Create database. Under Database, choose Redis, or Valkey: the code in this guide works with both. Enter storefront-redis as the Display name, choose the same Data center as the application and a Size, then click Create database.
2

Connect it to the application

In the sidebar, click Applications, then click the application. Click the Networking tab. Under Connected services, click Add internal connection. Choose storefront-redis as the Service, turn on Add environment variables to the application, and click Add internal connection. Sevalla adds REDIS_HOST, REDIS_PORT, REDIS_PASSWORD and REDIS_URL. See Connect an application.
The Add internal connection sheet with the Service field and the Add environment variables to the application switchThe Add internal connection sheet with the Service field and the Add environment variables to the application switch

Connect to Redis

Install redis-py and update requirements.txt:
Open one Redis client when the application starts and close it when it stops, with a lifespan handler. In storefront/main.py, replace app = FastAPI():
storefront/main.py

Cache a response

Read from the cache first, and store a fresh result for 2 minutes when it’s empty:
storefront/main.py
Commit and push the changes. If Auto-deploy is off, click Deploy now on the application’s Overview tab.

Verify

Request the route twice, with your application’s domain:
Both responses show the same generated_at. After 2 minutes, the next request shows a new time. To look at the key, open the Redis database and click the Redis studio tab. See Database studio.

Control the CDN and edge cache

Sevalla can also cache responses at Cloudflare’s edge, in front of the application. Turn on the CDN and edge caching on the application’s Networking tab; see CDN and edge caching.
  • The CDN caches static files by file extension, such as .css, .js and images.
  • Edge caching caches pages and other responses too. It caches only GET requests, skips requests with an Authorization header, and doesn’t store responses that set a cookie.
  • When a response has no Cache-Control header, Sevalla adds one. With edge caching on, a 200 response without its own header can stay cached for up to 30 days, unless a deployment or a purge clears the cache first. See Control caching from your application.
So with edge caching on, set Cache-Control on every route. no-store keeps a response out of every cache, and max-age sets how long it may be kept:
storefront/main.py
To check what a route sends, run curl -s -D - -o /dev/null https://storefront-x7k2m.sevalla.app/catalog and read the Cache-Control header. (curl -I sends a HEAD request, which a FastAPI GET route answers with 405.)