| 1 | --- |
| 2 | title: Redis |
| 3 | description: Redis in a Project. Bind Railway.ReadRedis / WriteRedis / ReadWriteRedis on a Service. |
| 4 | --- |
| 5 | |
| 6 | [`Railway.Redis`](/providers/railway/redis) is a Redis container in |
| 7 | a [Project](/railway/compute/projects) (`redis:7` or |
| 8 | `bitnami/redis`). Alchemy writes `REDIS_URL` / `REDISPASSWORD` on |
| 9 | the service. Bind [`ReadWriteRedis`](/providers/railway/readwriteredis) |
| 10 | on a [Service](/railway/compute/services). Reach it from a laptop |
| 11 | through [`TcpProxy`](/railway/networking#tcp-proxies) on port `6379`. |
| 12 | |
| 13 | ## Create Redis |
| 14 | |
| 15 | Pass a Project. Alchemy generates a unique name and password unless |
| 16 | you pass them. Default image is `redis:7`. |
| 17 | |
| 18 | ```typescript |
| 19 | const site = yield* Railway.Project("Site"); |
| 20 | const cache = yield* Railway.Redis("Cache", { project: site }); |
| 21 | ``` |
| 22 | |
| 23 | :::note |
| 24 | Prefer omitting `name` in tests and CI so names stay unique and |
| 25 | reclaimable. |
| 26 | ::: |
| 27 | |
| 28 | ## A stable name |
| 29 | |
| 30 | Pass `name` when you want a stable service name. Changing it later |
| 31 | updates the service in place. |
| 32 | |
| 33 | ```typescript |
| 34 | const cache = yield* Railway.Redis("Cache", { |
| 35 | project: site, |
| 36 | name: "cache", |
| 37 | }); |
| 38 | ``` |
| 39 | |
| 40 | ## Image |
| 41 | |
| 42 | Official Redis (`redis:7`) is started with `--requirepass`. Bitnami |
| 43 | reads `REDIS_PASSWORD`. Updating the image is in place. |
| 44 | |
| 45 | ```typescript |
| 46 | const cache = yield* Railway.Redis("Cache", { |
| 47 | project: site, |
| 48 | image: "bitnami/redis", |
| 49 | }); |
| 50 | ``` |
| 51 | |
| 52 | ## Bind from a Service |
| 53 | |
| 54 | Yield `Railway.ReadWriteRedis` (or `ReadRedis` / `WriteRedis`) in |
| 55 | the Service's constructor. Provide the matching `*Http` layer. Runtime commands |
| 56 | use `REDIS_URL` (`{name}.railway.internal`). |
| 57 | |
| 58 | ```typescript |
| 59 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 60 | |
| 61 | const Cache = Railway.Redis("Cache", { project: Site }); |
| 62 | |
| 63 | export default class Api extends Railway.Service<Api>()( |
| 64 | "Api", |
| 65 | { project: Site, main: import.meta.url, port: 3000 }, |
| 66 | Effect.gen(function* () { |
| 67 | const cache = yield* Railway.ReadWriteRedis(Cache); |
| 68 | return { |
| 69 | fetch: Effect.gen(function* () { |
| 70 | yield* cache.set("marker", "hello"); |
| 71 | const value = yield* cache.get("marker"); |
| 72 | return HttpServerResponse.json({ value }); |
| 73 | }), |
| 74 | }; |
| 75 | }).pipe(Effect.provide(Railway.ReadWriteRedisHttp)), |
| 76 | ) {} |
| 77 | ``` |
| 78 | |
| 79 | ## Public TCP |
| 80 | |
| 81 | Attach a [`TcpProxy`](/railway/networking#tcp-proxies) on `6379` to |
| 82 | reach Redis from outside the private network. |
| 83 | |
| 84 | ```typescript |
| 85 | const cache = yield* Railway.Redis("Cache", { project: site }); |
| 86 | const proxy = yield* Railway.TcpProxy("CacheProxy", { |
| 87 | redis: cache, |
| 88 | environment: site, |
| 89 | applicationPort: 6379, |
| 90 | }); |
| 91 | ``` |
| 92 | |
| 93 | ## Password |
| 94 | |
| 95 | Wrap `password` with `Redacted.make(...)`. If omitted, a password is |
| 96 | generated on first create and kept. Never persisted in attributes. |
| 97 | |
| 98 | ```typescript |
| 99 | import * as Redacted from "effect/Redacted"; |
| 100 | |
| 101 | const cache = yield* Railway.Redis("Cache", { |
| 102 | project: site, |
| 103 | password: Redacted.make("…"), |
| 104 | }); |
| 105 | ``` |
| 106 | |
| 107 | ## Where next |
| 108 | |
| 109 | [Regions](/railway/compute/regions) lists codes. |
| 110 | [Services](/railway/compute/services) is the container that binds |
| 111 | `ReadWriteRedis`. |
| 112 | The [`Redis` reference](/providers/railway/redis) lists every prop. |