Skip to main content
A Node.js application on Sevalla reaches a Redis or Valkey database over an internal connection: the connection adds the database’s host, port and password to the application as environment variables, and ioredis connects with them over the private network. This guide caches a slow response in Redis from an Express application. Because every instance and process of the application shares the database, the cache, sessions and queues you keep there work with more than one instance.

Before you begin

  • An Express application deployed on Sevalla, as in Deploy Express.
  • Node.js 20 or later in the application, which ioredis 6 needs. The Express guide pins Node.js 24.

Create a Redis database

1

Create the database

In the sidebar, click Databases, then click Create database. Under Database, choose Redis or Valkey and the version. Enter storefront-redis as the Display name, choose the same Data center as the application and a Size, then click Create database. See Create a database for every field.
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. Only services in the application’s location are listed.
The connection adds four variables to the application, which apply at the next deployment: Redis and Valkey databases have a password but no username, and clients use logical database 0. See Connect an application.

Install ioredis

Create the client

Create one client for the whole application, from the separate variables:
redis.js
The separate variables work for both engines. For a Valkey database, REDIS_URL starts with valkey://, which ioredis doesn’t parse as a URL. For a Redis database, new Redis(process.env.REDIS_URL) works too. ioredis reconnects on its own when the connection drops, and queues commands until it’s back.

Cache a slow response

Add a route to server.js that keeps its result in Redis for 60 seconds:
server.js
EX 60 makes Redis delete the key after 60 seconds, so the next request loads fresh data. Delete the key with redis.del("products") when the data changes.

Deploy and verify

Commit and push the changes. With auto-deploy on, the push starts a deployment; otherwise, start one with Deploy now on the application’s Overview tab. The deployment also gives the application the variables the internal connection added. When the deployment’s status changes to Deployed:
  1. Request the route twice:
    The first response has X-Cache: miss and takes about a second. The second has X-Cache: hit and returns at once.
  2. On the database’s Redis studio tab, the products key appears with its value until it expires. See Database studio.
If the requests fail, read the application’s runtime logs:

Use Redis from other processes

Environment variables belong to the application, so its background workers and cron jobs get the same REDIS_* variables as the web process. Import the same redis.js in a worker to process a queue, or store sessions in Redis with your framework’s Redis session store so that a visitor stays signed in on every instance.