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.
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
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 toserver.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:-
Request the route twice:
The first response has
X-Cache: missand takes about a second. The second hasX-Cache: hitand returns at once. -
On the database’s Redis studio tab, the
productskey appears with its value until it expires. See Database studio.
Use Redis from other processes
Environment variables belong to the application, so its background workers and cron jobs get the sameREDIS_* 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.