Skip to content

Commit 0e8088d

Browse files
committed
chore(docker): opt-in lagged postgres replica for local dev
A streaming standby behind the "replica" compose profile with a configurable recovery_min_apply_delay, so replica-lag behavior can be reproduced deterministically on a laptop. Also raises the primary's max_connections so multi-instance local testing has connection headroom.
1 parent 934be00 commit 0e8088d

1 file changed

Lines changed: 50 additions & 0 deletions

File tree

docker/docker-compose.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ name: triggerdotdev-docker
1111
volumes:
1212
database-data:
1313
database-data-alt:
14+
database-replica-data:
1415
redis-data:
1516
minio-data:
1617
clickhouse-data:
@@ -44,6 +45,55 @@ services:
4445
- wal_level=logical
4546
- -c
4647
- shared_preload_libraries=pg_partman_bgw
48+
# The webapp opens ~50 pooled connections per instance and Electric another
49+
# ~40, so the default 100 is exhausted by one webapp + Electric alone. Raise
50+
# it so multiple instances / load tests have headroom.
51+
- -c
52+
- max_connections=500
53+
54+
# Opt-in streaming read replica with configurable apply lag — a dial-a-lag rig for
55+
# testing replica-race behavior (e.g. the realtime read-your-writes gate) locally.
56+
# Start with: COMPOSE_PROFILES=replica pnpm run docker
57+
# One-time primary prep (allows replication connections; additive, survives restarts):
58+
# docker exec database bash -c 'grep -q "host replication" "$PGDATA/pg_hba.conf" || echo "host replication all all md5" >> "$PGDATA/pg_hba.conf"'
59+
# docker exec database psql -U postgres -c "SELECT pg_reload_conf()"
60+
# Then point the webapp at it: DATABASE_READ_REPLICA_URL=postgresql://postgres:postgres@localhost:5433/postgres
61+
# Tune the lag via REPLICA_APPLY_DELAY (e.g. 150ms, 2s). Wipe database-replica-data to re-init.
62+
database-replica:
63+
container_name: ${CONTAINER_PREFIX:-}database-replica
64+
profiles: ["replica"]
65+
build:
66+
context: .
67+
dockerfile: Dockerfile.postgres
68+
restart: always
69+
depends_on:
70+
- database
71+
volumes:
72+
- ${DB_REPLICA_VOLUME:-database-replica-data}:/var/lib/postgresql/data/
73+
environment:
74+
PGPASSWORD: postgres
75+
REPLICA_APPLY_DELAY: ${REPLICA_APPLY_DELAY:-150ms}
76+
networks:
77+
- app_network
78+
ports:
79+
- "${POSTGRES_REPLICA_HOST_PORT:-5433}:5432"
80+
entrypoint: ["bash", "-c"]
81+
command:
82+
- |
83+
set -e
84+
if [ ! -s "$$PGDATA/PG_VERSION" ]; then
85+
echo "initializing streaming replica from 'database'..."
86+
mkdir -p "$$PGDATA"
87+
chown postgres:postgres "$$PGDATA"
88+
chmod 0700 "$$PGDATA"
89+
until gosu postgres pg_basebackup -h database -U postgres -D "$$PGDATA" -Fp -Xs -R; do
90+
echo "primary not ready for replication (did you run the one-time pg_hba prep above?); retrying..."
91+
rm -rf "$$PGDATA"/* 2>/dev/null || true
92+
sleep 2
93+
done
94+
fi
95+
# max_connections must be >= the primary's (hot-standby requirement).
96+
exec docker-entrypoint.sh postgres -c hot_standby=on -c max_connections=500 -c "recovery_min_apply_delay=$$REPLICA_APPLY_DELAY"
4797
4898
redis:
4999
container_name: ${CONTAINER_PREFIX:-}redis

0 commit comments

Comments
 (0)