Goal
A real-time chat app that is correct before it ships: sessions that survive two browser tabs, WebSockets that authenticate on the first frame, schema changes that run once instead of racing, tests that mean something, and containers that run as non-root.
- AuthenticationIn-memory access token, httpOnly rotating refresh cookie, CSRF header and Origin allow-list.Done
- WebSocket securityFirst frame must be an auth message; identity comes from the verified account; messages persist before they publish to Redis.Done
- MigrationsAlembic replaces create_all, run once by a migrate Job instead of by every worker.Done
- Tests122 tests and about 96% coverage with concurrency-aware coverage settings.Done
- ContainersBackend and standalone Next.js images that run as non-root.Done
Steps and commands
Run these in dev first. Every step has a check that proves it worked, so you are never guessing.
Run the stack locally
Start the API, web app, PostgreSQL and Redis together so every later phase has a known-good baseline.
$ docker compose up --build -d $ docker compose ps $ docker compose logs migrateCheck: every container is healthy, the migrate service finished, and you can register, log in and chat between two browser windows.Move schema changes to Alembic
create_all at startup is fine on a laptop and wrong with two gunicorn workers racing each other. Generate and apply migrations explicitly.
$ alembic revision --autogenerate -m "describe the change" $ alembic upgrade head $ pytest tests/test_migrations.pyCheck: a second run of upgrade head changes nothing, and the workers no longer touch the schema.Run the tests with honest coverage
Configure coverage for greenlet and thread concurrency, otherwise async code looks untested.
$ pytest --cov=appCheck: all tests pass, and the coverage number stays the same when you run it twice.Install the automatic checks
Run the same formatters and linters on every commit that CI will run later.
$ pre-commit install $ pre-commit run --all-filesCheck: a badly formatted file is rewritten or rejected before it can be committed.
Workflow
A change starts on a feature branch. The same checks run on your machine and, from Phase 4, in the pipeline, so there are no surprises on the pull request.
- STEP 1Feature branchSmall change
- STEP 2Pre-commitFormat, lint, secrets
- STEP 3TestsUnit and API
- STEP 4ComposeRun it for real
- STEP 5Pull requestInto develop
Rollback: Revert the commit. Nothing outside your laptop has changed yet.
Diagram

Issues met
These are real, from the run. The surprises are the useful part.
Trade-offs
Why: A token that never touches localStorage cannot be read by an injected script, and the httpOnly refresh cookie restores the session on reload.
It costs: A page reload needs one extra refresh call, and multi-tab behaviour needs the locking described above.
Why: Migrations run once, are versioned and can be reviewed. Workers never race on the schema.
It costs: One more moving part to deploy and to keep ordered before the app.