← All Tutorials
DatabasesMar 5, 20266 min read

Deploy Redis for Caching and Sessions

Set up a managed Redis instance for caching, session storage, and real-time features.

Introduction

Redis is the industry standard for in-memory caching, session management, and real-time data structures. Rackline's managed Redis service provides automatic failover, persistence options, and TLS encryption. This tutorial covers provisioning Redis, configuring persistence, and connecting from your application.

Prerequisites

  • A Rackline account
  • An application deployed on Rackline

Step 1 — Provision a Managed Redis Instance

Create a Redis instance from the CLI or dashboard. Rackline supports Redis 7.4 with all modules including RedisJSON, RediSearch, and RedisTimeSeries. Choose a plan based on your data size and throughput requirements.

bash
# Create a managed Redis instance
rackline resources create database \
  --engine redis \
  --version 7.4 \
  --name my-app-cache \
  --region us-east-1 \
  --size cache-s-1vcpu-256mb

# Get connection details
rackline resources info my-app-cache

Step 2 — Configure Persistence Options

Rackline Redis supports both RDB snapshots and AOF (Append Only File) persistence. For caching, RDB snapshots every 15 minutes are sufficient. For session storage where data loss is unacceptable, enable AOF with everysec sync.

bash
# Configure persistence (RDB + AOF for durability)
rackline db configure my-app-cache \
  --persistence rdb-aof \
  --rdb-frequency 900 \
  --aof-sync everysec

# Set memory eviction policy
rackline db configure my-app-cache \
  --eviction-policy allkeys-lru \
  --maxmemory-percentage 80

Pro Tip

Use allkeys-lru eviction for pure caching and volatile-lru when mixing cached and persistent data. This ensures Redis always has room for new entries.

Step 3 — Connect from Your Application

Connect to Redis using the ioredis library for Node.js or redis-py for Python. Rackline Redis connections use TLS by default. The connection string includes all necessary authentication and encryption parameters.

typescript
// Node.js with ioredis
import Redis from "ioredis";

const redis = new Redis(process.env.REDIS_URL!, {
  maxRetriesPerRequest: 3,
  retryDelayOnFailover: 100,
});

// Cache a database query result
async function getCachedUser(id: string) {
  const cached = await redis.get(`user:${id}`);
  if (cached) return JSON.parse(cached);

  const user = await db.users.findUnique({ where: { id } });
  await redis.set(`user:${id}`, JSON.stringify(user), "EX", 3600);
  return user;
}

Step 4 — Use Redis for Session Storage

Redis is ideal for storing user sessions in a distributed environment. When your application scales horizontally across multiple instances, Redis ensures all instances share the same session data.

typescript
// Express.js session with Redis
import session from "express-session";
import RedisStore from "connect-redis";

app.use(session({
  store: new RedisStore({ client: redis }),
  secret: process.env.SESSION_SECRET!,
  resave: false,
  saveUninitialized: false,
  cookie: {
    secure: true,
    httpOnly: true,
    maxAge: 86400000, // 24 hours
  },
}));

Pro Tip

Set appropriate TTLs on all cached data. Without expiration, your Redis instance will eventually run out of memory regardless of eviction policy.

Conclusion

Your Redis instance is now running on Rackline with persistence, TLS encryption, and connection from your application. Redis dramatically reduces database load and improves response times for frequently accessed data. For monitoring Redis performance, check out the Monitoring and Alerts tutorial.