# Add a Redis Session Store to a Fastify App from a Linear Issue

Yes. An agent reads the Linear issue, moves your Fastify sessions from process memory into Redis, proves it with the app's own tests and opens a pull request.

[Run this on Rush](https://cloud.getrush.ai/run/add-redis-sessions/fastify/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent starts from the issue text and the repository, cloned into its own cloud sandbox, and finds where sessions live before it changes anything. In an app shaped like fastify/demo (Fastify `^5.10.0`, TypeScript, Knex on `mysql2`), `src/plugins/external/session.ts` registers `@fastify/cookie` and `@fastify/session` with `secret: fastify.config.COOKIE_SECRET`, the cookie name from `COOKIE_NAME`, `httpOnly`, `sameSite: 'lax'` and a `maxAge` of 1800000 milliseconds, and no `store` at all. That is the gap the issue describes: with no store, `@fastify/session` keeps every session in the memory of the one process that created it, so a restart signs everyone out and two instances behind a load balancer do not share logins. The agent writes that finding into its plan, with the file and line that prove it.

Then it reads how the rest of the app depends on that session. `src/routes/api/auth/index.ts` handles `POST /api/auth/login`, looks the user up through `usersRepository.findByEmail` inside a Knex transaction, sets `request.session.user` to the id, email, username and roles, and calls `await request.session.save()`. `src/routes/api/autohooks.ts` adds an `onRequest` hook that answers `reply.unauthorized(...)` for any `/api` route except login when `request.session.user` is missing. `src/plugins/external/env.ts` validates configuration through `@fastify/env`, with `MYSQL_HOST`, `COOKIE_SECRET`, `COOKIE_NAME` and `COOKIE_SECURED` required and `RATE_LIMIT_MAX` defaulting to 100. None of that has to change; the store is the only new piece, and the agent keeps the login route and the hook as they are.

It makes the change in the files that own it. A new `src/plugins/external/redis.ts` registers a Redis client the way `src/plugins/external/knex.ts` registers Knex: decorated on the instance, built from a `REDIS_URL` the agent adds to the `@fastify/env` schema and to `.env.example`, and closed in an `onClose` hook so `close-with-grace` in `src/server.ts` still shuts the app down cleanly. `session.ts` gains a `store` backed by that client and declares `dependencies: ['redis']` on its `fastify-plugin` wrapper, because `@fastify/autoload` loads `plugins/external` first and the order inside it matters. The `healthCheck` in `src/plugins/external/under-pressure.ts`, which today runs `SELECT 1` through Knex, gets a Redis ping beside it, so a lost Redis connection shows up as a 503 instead of as users being logged out one by one.

Last, it proves the change with the project's own suite. `npm test` runs `npm run db:seed` and then `c8` over `tsx --test` for every file under `test/`, and `test/helper.ts` logs in by posting `basic@example.com` to `/api/auth/login` and reading the cookie named `COOKIE_NAME`, so every existing route test exercises the new store for free. The agent adds `test/app/session-store.test.ts`: a cookie issued by one app instance is accepted by a second instance built against the same Redis, and the entry disappears after `maxAge`. To run that, it starts MySQL and Redis in the sandbox, applies `npm run db:migrate` with `RATE_LIMIT_MAX=4` and `CAN_SEED_DATABASE=1` as `.github/workflows/ci.yml` does on its Node 22 and 24 matrix, adds a Redis service to that workflow and to `docker-compose.yml` beside the `mysql:8.4` one, and runs `npm run lint` and `npm run build -- --noEmit` before it pushes.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "A Linear issue labeled agent says: \"Sessions are lost on every deploy and are not shared between instances. Store Fastify sessions in Redis.\" Before changing anything, find how sessions are configured today: the @fastify/session registration, whether it names a store, the cookie options, the env schema in @fastify/env, and every route or hook that reads request.session. Add a Redis plugin under src/plugins/external that reads REDIS_URL from the env schema and closes its client on app close, give @fastify/session a Redis-backed store, declare the plugin dependency so load order is right, and add a Redis ping to the under-pressure health check. Keep the login route, the auth hook and the cookie options unchanged. Add a test that a session cookie from one app instance is accepted by a second instance using the same Redis, add a Redis service to docker-compose.yml and to the CI workflow, start MySQL and Redis in the sandbox, run the migrations the way CI does, and run npm test, npm run lint and the type-only build until they pass. Open a pull request that links back to the issue and explains what was already in place and what changed.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request on its own branch whose description starts with what the agent found: that `@fastify/session` was registered without a `store`, so sessions lived in one process's memory. The diff touches the new `src/plugins/external/redis.ts`, the `store` and `dependencies` lines in `session.ts`, the `REDIS_URL` entry in `env.ts` and `.env.example`, the Redis ping in the health check, the new test under `test/app/`, and the service blocks in `docker-compose.yml` and `.github/workflows/ci.yml`. Nothing in `src/routes/` changes.

The proof is in the session's transcript: the commands that started MySQL and Redis, the migration run, every `npm test` run with its output, including any that failed before a fix, and the lint and type-check runs. If the repository's CI runs on pull requests, the updated workflow runs there too with the new Redis service, so the reviewer sees the suite pass against a real Redis on both Node versions.

Before merging, a reviewer checks that `REDIS_URL` has a sane default for local development and is required in production, that the store's TTL matches the cookie's `maxAge` rather than keeping entries forever, that the health check does not make a single slow Redis call take the whole app down, and that the new test fails when the `store` line is removed.

## Run it on every Linear issue labelled agent

In the console, an automation with a `linear` trigger on `linear.issue.labeled` and the label `agent` starts one run per labelled issue in the Project's repository. The trigger adds the issue's text after the automation's prompt, so the prompt above stays fixed and each issue supplies the details. Changes made by an app integration never fire it, so the agent's own comments on the issue start nothing new, and the automation's `max_runs_per_hour` caps how many runs an afternoon of labelling can start.

From your own server, the same job is one `POST /o/{org}/sessions` per issue: the repository in `resources`, and the issue's `https://linear.app/<workspace>/issue/<ID>` URL beside it so Rush reads the issue through your Linear connection into the prompt. A `webhook_url` on the API key receives `run.completed` or `run.failed` for each run that key starts, with the `session_url` to read the pull request link from.

## Why run it on Rush?

Proving a session store works means running the app against a real MySQL and a real Redis at the same time, restarting the process between requests and watching a cookie survive. On a laptop that is two services to install and a database to seed; in a Rush sandbox the agent starts both on its own disk, migrates and seeds the test database exactly as the CI workflow does, and tears it all down when the run ends. Nothing on your machine is touched.

A label in Linear turns into a reviewable pull request with its evidence attached. The transcript shows the agent reading the existing `@fastify/session` registration before it writes a store, every test run with its output, and the health-check change next to the reason for it. Several labelled issues can run at once, each in its own sandbox on its own branch, and each pull request links back to the issue that asked for it.

## Frequently asked questions

### Will it change how login works or what the cookie looks like?

No. The login route keeps setting `request.session.user` and calling `request.session.save()`, and the cookie options in `session.ts` stay as they are. Only the place the session data is kept changes, from the default in-memory store to Redis, so existing clients keep working without a new login.

### Which Redis client does it add?

One that fits the project's plugin style: a `fastify-plugin` under `src/plugins/external` that decorates the instance with a client and closes it in an `onClose` hook, the same shape `knex.ts` already uses. The pull request names the package and version it chose and why, and the reviewer can ask for another in a follow-up.

### Does it run the tests against a real Redis?

Yes. The agent starts Redis and MySQL in its sandbox, runs `npm run db:migrate` with the same environment `.github/workflows/ci.yml` uses, and then `npm test`, which seeds the database and runs every file under `test/` through `tsx --test` with coverage from `c8`. The new session-store test runs there too.

### What happens to sessions that exist when this is deployed?

Sessions in the old in-memory store are not migrated, because they disappear with the process anyway. Users sign in once after the deploy, and from then on their sessions live in Redis and survive restarts. The pull request says this plainly so the deploy can be timed.

## More Fastify jobs

- [Add Request IDs and Structured Logging to a Fastify App](https://getrush.ai/run/add-request-id-logging/fastify/)
- [Add Soft Delete to a Fastify API from a Linear Issue](https://getrush.ai/run/add-soft-delete/fastify/)
- [Add User Registration to a Fastify App](https://getrush.ai/run/add-user-registration/fastify/)

## More jobs like this: Turn a Linear label into a PR

- [Add Refresh Tokens to an Express API That Uses jsonwebtoken](https://getrush.ai/run/add-refresh-tokens/jsonwebtoken/)
- [Add Refresh Token Rotation to a NextAuth.js App](https://getrush.ai/run/add-refresh-tokens/next-auth/)
- [Add Refresh Tokens to a FastAPI App on SQLModel](https://getrush.ai/run/add-refresh-tokens/sqlmodel/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
