Redis

Harpia’s App boilerplate comes pre-configured with a RedisStore class powered by Bun’s native RedisClient — no ioredis or external dependencies required. This store implements the generic Store interface from @harpiats/core, making it a drop-in replacement for any feature that requires a persistent, in-memory key-value layer.

Configuration

Redis connection settings are read from your .env file at runtime:

REDIS_HOST=localhost
REDIS_USER=
REDIS_PASS=
REDIS_PORT=6379

The RedisStore class (located at app/config/redis.ts) is already wired up and ready to use:

// app/config/redis.ts
import type { Store } from "@harpiats/core";
import { RedisClient } from "bun";

export class RedisStore implements Store {
  // Reads REDIS_HOST, REDIS_PORT, REDIS_USER, REDIS_PASS from .env
  // and connects automatically with auto-reconnect enabled.
}

The constructor accepts an optional db argument to select a specific Redis database (default: 0):

import { RedisStore } from "app/config/redis";

const store = new RedisStore();      // Uses DB 0
const cacheStore = new RedisStore(1); // Uses DB 1 for isolation

Using Redis for Sessions

To store user sessions in Redis instead of memory, pass a RedisStore instance to the session or JWT configuration.

Bearer (JWT) with Redis

When generating the auth setup with bun harpia generate setup and selecting Bearer → Redis, the generated app/config/jwt.ts will use the RedisStore automatically.

You can also configure it manually:

// app/config/jwt.ts
import { Jwt } from "@harpiats/core";
import { RedisStore } from "app/config/redis";

export const jwt = new Jwt({
  secret: process.env.JWT_SECRET!,
  store: new RedisStore(),
  expiresIn: "7d",
});
// app/config/session.ts
import { Session } from "@harpiats/core";
import { RedisStore } from "app/config/redis";

export const session = new Session({
  secret: process.env.JWT_SECRET!,
  store: new RedisStore(),
  expiresIn: 60 * 60 * 24 * 7, // 7 days in seconds
});

Using Redis for Telemetry

The default Harpia boilerplate switches the Telemetry store to Redis in production, ensuring request metrics survive server restarts and are shared across instances:

// app/config/telemetry.ts
import { MemoryStore, Telemetry as HarpiaTelemetry } from "@harpiats/core";
import { RedisStore } from "./redis";

export const Telemetry = new HarpiaTelemetry({
  store: process.env.ENV === "production" ? new RedisStore(0) : new MemoryStore(),
  ignore: ["favicon.ico"],
});

RedisStore API

The RedisStore class implements the following methods from the Store interface:

MethodDescription
on()Resolves to true if the connection to Redis is active.
get(key)Retrieves and deserialises a JSON value by key. Returns undefined if not found.
set(key, data)Serialises and stores a value without an expiry.
setEx(key, data, ttlSeconds)Stores a value with a TTL (time-to-live) in seconds. The key is automatically deleted after expiry.
delete(key)Removes a key from Redis.

[!TIP] Use setEx when caching data that should automatically invalidate, such as one-time tokens, rate-limiting counters, or temporary session data.