Postgres per lane
A TypeScript server built with Hono that writes rows to its lane’s own Postgres container and counts visits in a Redis that every lane shares. A branch that bumps the image tag in docker-compose.yml runs that version without touching the other lanes, and a destructive migration only breaks that branch’s data; a shared Postgres, as in infra-postgres-shared, costs less but runs one version for everyone.
Requirements
Section titled “Requirements”- devlanes, with
devlanes setuprun once - Node 22 or later
- Docker with Compose 2.24.4 or newer, which
per_laneneeds for the!overrideand!resettags in its override file.devlanes doctorprints both versions.
Run it
Section titled “Run it”cd infra-postgres-per-lanedevlanes startdevlanes start --no-ui prints the start as text:
example-postgres-per-lane@0 (main): 1/1 readyready api http://127.0.0.1:20510 https://example-postgres-per-lane.localhostOpen https://example-postgres-per-lane.localhost. The page says This is lane 0 of example-postgres-per-lane. and shows the lane’s Postgres container, port and rows, and the shared Redis with this lane’s database index and visit count; “Add a row” inserts into Postgres and “Add a visit” increments the counter in Redis.
During a start
Section titled “During a start”- Infra. Redis starts once in the shared Compose project
example-postgres-per-lane(infra.project_name). Postgres starts in the lane’s own project,example-postgres-per-lane-lane<N>, with an override file devlanes writes tocompose/in the lane data folder. The override dropscontainer_name, publishes5432on a port from the lane’s block and addsdev.devlanes.*labels. Thepgdatavolume belongs to the lane’s project, so each lane gets its own. infra.setup.schemaruns once per lane. It feedsinfra/schema.sqltopsqlthroughdocker compose -p "$DEVLANES_COMPOSE_LANE_PROJECT" exec -T postgres ...; plaindocker compose execwould target the shared project, where Postgres does not run.- The
installtask runsnpm installon the lane’s first start. - The
apiservice runsnpm run dev(tsx watch src/index.ts) withDATABASE_URLpointing at${{ infra.postgres.port }}, which differs per lane, andREDIS_URLending in/${{ lane.redis_db }}, the lane index. The ready check waits for/healthz.
docker ps --filter name=postgres-per-lane --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'NAMES STATUS PORTSexample-postgres-per-lane-lane0-postgres-1 Up 9 seconds (healthy) 127.0.0.1:20511->5432/tcpexample-postgres-per-lane-redis-1 Up 12 seconds (healthy) 127.0.0.1:52667->6379/tcpdevlanes env show api prints the lane data folder as DEVLANES_LANE_DATA (~/.devlanes/data/example-postgres-per-lane/<lane id>). The override is in it:
cat "$(devlanes env show api | grep ^DEVLANES_LANE_DATA= | cut -d= -f2)/compose/devlanes.compose-override.yaml"# Generated by devlanes for lane 0 of example-postgres-per-lane; rewritten on every start.services: "postgres": container_name: !reset null ports: !override - "127.0.0.1:20511:5432/tcp" labels: "dev.devlanes.kind": "compose" "dev.devlanes.lane": "0" "dev.devlanes.project": "example-postgres-per-lane" "dev.devlanes.scope": "lane" "dev.devlanes.service": "postgres"Try next
Section titled “Try next”Start a second lane:
git worktree add ../examples-checkout -b feat/checkout, thencd ../examples-checkout/infra-postgres-per-lane && devlanes start. The new worktree is lane 1. In this folder,devlanes lane new --name example-postgres-per-lane-review --start --no-uigives the same lane 1 without a second worktree. Lane 1 gets its own Postgres container and port, while both lanes use the same Redis:NAMES STATUS PORTSexample-postgres-per-lane-lane1-postgres-1 Up 5 seconds (healthy) 127.0.0.1:20551->5432/tcpexample-postgres-per-lane-lane0-postgres-1 Up 30 seconds (healthy) 127.0.0.1:20511->5432/tcpexample-postgres-per-lane-redis-1 Up 33 seconds (healthy) 127.0.0.1:52667->6379/tcphttps://example-postgres-per-lane1.localhost says
This is lane 1 of example-postgres-per-lane.and showsexample-postgres-per-lane-lane1-postgres-1with no rows and Redis database 1, so its visit count starts at 0 whatever lane 0 counted.Freeze lane 1 with
devlanes debug freeze example-postgres-per-lane@1. Its processes are suspended and its Postgres is paused; the shared Redis keeps running:frozen example-postgres-per-lane@1a request, a connection or a command thaws it, or run devlanes debug thaw example-postgres-per-lane@1NAMES STATUSexample-postgres-per-lane-lane1-postgres-1 Up 18 seconds (Paused)example-postgres-per-lane-lane0-postgres-1 Up 43 seconds (healthy)example-postgres-per-lane-redis-1 Up 46 seconds (healthy)Thaw lane 1 with a request:
curl -fsS https://example-postgres-per-lane1.localhost/healthz(add--ssl-revoke-best-efforton Windows) printsokand the container is no longer paused.Clean lane 1 with
devlanes lane clean 1 --yes. Teardown runsFLUSHDBon Redis database 1, then the lane’s Compose project goes with its container and volume. Redis and lane 0 stay:cleaned example-postgres-per-lane@1: stopped 1 unit, teardown redisTerminal window docker volume ls --filter name=postgres-per-laneDRIVER VOLUME NAMElocal example-postgres-per-lane-lane0_pgdataClean up everything:
devlanes lane clean 0 --yesremoves lane 0’s Postgres and volume. The shared Redis outlives every lane, so remove it withdocker compose -p example-postgres-per-lane down -v.
devlanes.yaml:infra.per_lane, the Redis index inlanes.vars, the setup and teardown entries, the URLs inglobal_env, theinstalltask and theapiservice.docker-compose.yml: Postgres and Redis with pinned tags, ephemeral loopback ports and healthchecks.infra/schema.sql: thenotestable the setup entry creates.src/db.tsandsrc/redis.ts: the queries behind the page and the buttons.src/routes/: the page,/api/stateand the form handlers.