Terminal prompt logo for Ali Haidry — alihaidry.dev~/AHAli Haidryalihaidry.dev
Phase 1 of 6Published

Local development

Every later control (network policies, admission rules, scans) assumes the application is not itself the weak point. So the code is hardened and tested first, on a laptop.

FastAPINext.jsPostgreSQLRedisDockerUpdated Nov 2026

Goal

What this phase delivers

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.

  1. 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 migrate
    Check: every container is healthy, the migrate service finished, and you can register, log in and chat between two browser windows.
  2. 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.py
    Check: a second run of upgrade head changes nothing, and the workers no longer touch the schema.
  3. Run the tests with honest coverage

    Configure coverage for greenlet and thread concurrency, otherwise async code looks untested.

    $ pytest --cov=app
    Check: all tests pass, and the coverage number stays the same when you run it twice.
  4. 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-files
    Check: 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.

  1. STEP 1Feature branchSmall change
  2. STEP 2Pre-commitFormat, lint, secrets
  3. STEP 3TestsUnit and API
  4. STEP 4ComposeRun it for real
  5. STEP 5Pull requestInto develop

Rollback: Revert the commit. Nothing outside your laptop has changed yet.

Diagram

Phase 1 workflow: feature branch, pre-commit, tests, docker compose, pull request, and the compose startup order
Figure: the loop I ran for every change, and the order docker compose starts the stack.

Issues met

These are real, from the run. The surprises are the useful part.

Two browser tabs logged each other out
Cause
Both tabs refreshed at the same moment, each rotated the refresh cookie and revoked the other session.
Fix
A single-flight refresh inside each tab and the Web Locks API across tabs, so only one refresh is ever in flight.
A client that left during the five-second auth window caused an unexpected ASGI error
Cause
The server tried to close a WebSocket that was already closed.
Fix
A small _close() helper that ignores already-closed sockets.
Coverage showed 82% when most code was exercised
Cause
Coverage was not configured for greenlet and thread concurrency, so async code paths were not measured.
Fix
Set the concurrency options in the coverage config. A coverage number is only as honest as its configuration.

Trade-offs

Access token storage
I chose: In memory only

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.

Schema management
I chose: Alembic run by a migrate Job

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.

Interview questions

Why harden the app before building any infrastructure?
Every later control assumes the application is not the weak point. A cluster full of policies does not help if the login flow can be bypassed.
How do you keep a JWT-based session safe in a single-page app?
Keep the short-lived access token in memory, restore the session from an httpOnly rotating refresh cookie, and protect the cookie endpoints with a custom header and an Origin allow-list on top of SameSite.
Why not create the tables at application startup?
With several workers they race each other. Run versioned migrations once, from a Job, before the new version starts.