Skip to content

Redis

Source: src/Railway/Redis.ts

A Railway.Redis is a Redis container in a Project (redis:7 or bitnami/redis). Alchemy writes REDIS_URL / REDISPASSWORD on the service. Bind ReadRedis, WriteRedis, or ReadWriteRedis on a Service. Reach it from a laptop through TcpProxy on port 6379.

Pass a Project. Alchemy generates a unique name and password unless you pass them. Default image is redis:7.

const site = yield* Railway.Project("Site");
const cache = yield* Railway.Redis("Cache", { project: site });

Pass name when you want a stable service name. Changing it later updates the service in place.

const cache = yield* Railway.Redis("Cache", {
project: site,
name: "cache",
});

Official Redis (redis:7) is started with --requirepass. Bitnami reads REDIS_PASSWORD. Updating the image is in place.

const cache = yield* Railway.Redis("Cache", {
project: site,
image: "bitnami/redis",
});

Attach a TcpProxy on 6379 to reach Redis from outside the private network.

const cache = yield* Railway.Redis("Cache", { project: site });
const proxy = yield* Railway.TcpProxy("CacheProxy", {
redis: cache,
environment: site,
applicationPort: 6379,
});

Yield ReadWriteRedis (or ReadRedis / WriteRedis) in Service init. Provide the matching *Http layer. Runtime commands use REDIS_URL ({name}.railway.internal).

import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";
export const Site = Railway.Project("Site");
export const Cache = Railway.Redis("Cache", { project: Site });
export default class Api extends Railway.Service<Api>()(
"Api",
{ project: Site, main: import.meta.url },
Effect.gen(function* () {
const cache = yield* Railway.ReadWriteRedis(Cache);
return {
fetch: Effect.gen(function* () {
yield* cache.set("marker", "hello");
const value = yield* cache.get("marker");
return HttpServerResponse.json({ value });
}),
};
}).pipe(Effect.provide(Railway.ReadWriteRedisHttp)),
) {}

Resource-valued props accept the resource or an Effect producing it.

src/cache.ts
import * as Railway from "alchemy/Railway";
export const Site = Railway.Project("Site");
export const Cache = Railway.Redis("Cache", { project: Site });