Skip to content
Get started
Get started
Docker: PostgreSQL and Three Brokers
View Markdown
examples · docker topology

PostgreSQL plus three active brokers.

Self-contained Compose fleet with private east-west networking, unique broker IDs, /ready gates, loopback bindings, and automatic cleanup.

Docker Compose Topology

Spin up PostgreSQL 19.0 plus three isolated brokers locally in one Compose stack

broker-a / b unique BROKER_IDs broker-c DATA_PATH="" · /ready 3 active brokers postgres:19.0 shared namespace verify.sh auto-teardown trap shared SQL pool Private Network internal: true Loopback-only ports + AUTH_TOKENS + METRICS_AUTH enabled out of the box
Reproducible local HA: three active brokers (broker-a, broker-b, broker-c) share one private Docker network and PostgreSQL namespace with clean teardown.
examples/postgres-multibroker/compose.yaml
x-broker-environment: &broker-environment
DATA_PATH: ''
BUNQUEUE_STORAGE_DRIVER: postgres
BUNQUEUE_POSTGRES_URL: postgres://bunqueue:local-demo-only@postgres:5432/bunqueue
BUNQUEUE_POSTGRES_NAMESPACE: realistic-example
BUNQUEUE_POSTGRES_POOL_SIZE: '4'
BUNQUEUE_POSTGRES_LEASE_DURATION_MS: '10000'
BUNQUEUE_POSTGRES_POLL_INTERVAL_MS: '50'
AUTH_TOKENS: demo-token
METRICS_AUTH: 'true'
TCP_PORT: '6789'
HTTP_PORT: '6790'
SHUTDOWN_TIMEOUT_MS: '15000'
x-broker: &broker
build:
context: ../..
dockerfile: Dockerfile
depends_on:
postgres:
condition: service_healthy
healthcheck:
test: ['CMD-SHELL', 'wget --spider -q http://127.0.0.1:6790/ready']
interval: 2s
timeout: 3s
retries: 30
start_period: 5s
restart: unless-stopped
networks: [demo]
services:
postgres:
image: postgres:19.0-alpine
environment:
POSTGRES_DB: bunqueue
POSTGRES_USER: bunqueue
POSTGRES_PASSWORD: local-demo-only
healthcheck:
test: ['CMD-SHELL', 'pg_isready -U bunqueue -d bunqueue']
interval: 2s
timeout: 3s
retries: 30
volumes:
- postgres-data:/var/lib/postgresql
restart: unless-stopped
networks: [demo]
broker-a:
<<: *broker
environment:
<<: *broker-environment
BUNQUEUE_BROKER_ID: broker-a
ports:
- '127.0.0.1:${BROKER_A_TCP_PORT:-16789}:6789'
- '127.0.0.1:${BROKER_A_HTTP_PORT:-16790}:6790'
broker-b:
<<: *broker
environment:
<<: *broker-environment
BUNQUEUE_BROKER_ID: broker-b
ports:
- '127.0.0.1:${BROKER_B_TCP_PORT:-17789}:6789'
- '127.0.0.1:${BROKER_B_HTTP_PORT:-17790}:6790'
broker-c:
<<: *broker
environment:
<<: *broker-environment
BUNQUEUE_BROKER_ID: broker-c
ports:
- '127.0.0.1:${BROKER_C_TCP_PORT:-18789}:6789'
- '127.0.0.1:${BROKER_C_HTTP_PORT:-18790}:6790'
sdk-example:
profiles: [tools]
build:
context: ../..
dockerfile: examples/postgres-multibroker/client.Dockerfile
entrypoint: ['bun', 'run', 'examples/postgres-multibroker/run.ts']
command: ['all']
environment:
BUNQUEUE_TOKEN: demo-token
BROKER_A_HOST: broker-a
BROKER_A_PORT: '6789'
BROKER_A_HTTP_URL: http://broker-a:6790
BROKER_B_HOST: broker-b
BROKER_B_PORT: '6789'
BROKER_B_HTTP_URL: http://broker-b:6790
BROKER_C_HOST: broker-c
BROKER_C_PORT: '6789'
BROKER_C_HTTP_URL: http://broker-c:6790
BUNQUEUE_EXAMPLE_SCENARIO_TIMEOUT_MS: ${BUNQUEUE_EXAMPLE_SCENARIO_TIMEOUT_MS:-60000}
depends_on:
broker-a:
condition: service_healthy
broker-b:
condition: service_healthy
broker-c:
condition: service_healthy
networks: [demo]
volumes:
postgres-data:
networks:
demo:
internal: true
SettingWhy it matters
postgres:19.0-alpineRecommended PostgreSQL 19 release
DATA_PATH: ''Clears the image’s SQLite default so PostgreSQL starts cleanly
Shared URL + namespaceMakes PostgreSQL authoritative across all three brokers
Unique BUNQUEUE_BROKER_IDPrevents session and lease-owner collisions
/ready health checkGates traffic on DB and maintenance-loop health, not just process life
AUTH_TOKENS + METRICS_AUTHEnforces authenticated TCP and /prometheus access
internal: true + loopback portsBlocks outbound internet and keeps demo ports off external interfaces
  1. From the repo root with Docker running, execute ./examples/postgres-multibroker/verify.sh (no external bunqueue or PostgreSQL needed).
  2. Confirm JSON pass lines (diagnostic functional timing, not a benchmark): { "durationMs": 7, "scenario": "topology", "status": "PASS" }.
  3. On exit, the trap removes all project containers, volumes, networks, and local images while preserving the exit code.
#!/bin/sh
set -eu
EXAMPLE_DIR=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)
PROJECT_NAME=${BUNQUEUE_EXAMPLE_PROJECT:-bunqueue-pg-example-$(date +%s)-$$}
case "$PROJECT_NAME" in
bunqueue-pg-example-?*) ;;
*)
echo "BUNQUEUE_EXAMPLE_PROJECT must start with bunqueue-pg-example-" >&2
exit 2
;;
esac
case "$PROJECT_NAME" in
*[!a-z0-9_-]*)
echo "BUNQUEUE_EXAMPLE_PROJECT may contain only lowercase letters, digits, _ and -" >&2
exit 2
;;
esac
compose() {
docker compose --project-name "$PROJECT_NAME" --file "$EXAMPLE_DIR/compose.yaml" "$@"
}
cleanup() {
original_status=$?
trap - EXIT INT TERM
set +e
compose down --volumes --remove-orphans
resources_status=$?
compose down --volumes --remove-orphans --rmi local
images_status=$?
if [ "$resources_status" -ne 0 ] || [ "$images_status" -ne 0 ]; then
echo "Example cleanup failed" >&2
fi
if [ "$original_status" -ne 0 ]; then
exit "$original_status"
fi
if [ "$resources_status" -ne 0 ] || [ "$images_status" -ne 0 ]; then
exit 1
fi
exit 0
}
trap cleanup EXIT
trap 'exit 130' INT
trap 'exit 143' TERM
compose config --quiet
compose --progress plain build
compose up --detach --wait --wait-timeout 120 postgres broker-a broker-b broker-c
for scenario in topology multi-queue reliability flow; do
compose run --rm --no-deps sdk-example "$scenario"
done
compose ps

The SDK container connects over Docker DNS; host loopback ports (overridable via BROKER_{A,B,C}_{TCP,HTTP}_PORT): A 127.0.0.1:16789 / :16790, B :17789 / :17790, C :18789 / :18790. Optional overrides: BUNQUEUE_EXAMPLE_SCENARIO_TIMEOUT_MS (default 60000) and BUNQUEUE_EXAMPLE_PROJECT (must start with bunqueue-pg-example-; all resources in that project are destroyed on exit).

In production, separate /healthz (HTTP liveness) from /ready (returns 503 on DB/maintenance degradation so load balancers drain traffic without restarting the broker).

Next: connect multiple queues and workers.