Production Docker Compose Stack for Autonomous AI Agents: n8n Queue Mode + PostgreSQL + Redis + Caddy (2026 Guide)
Transparency note: Some links in this article are affiliate links. If you sign up through them, AgentStack may earn a commission at no extra cost to you. We only recommend tools we evaluate rigorously.
Quick Verdict (TL;DR)
- Why Queue Mode matters for AI agents: Single-container n8n handles webhook ingestion, UI rendering, and LLM execution inside one Node.js process. When 10 parallel AI agent runs spike RAM, webhooks time out. Queue Mode (
EXECUTIONS_MODE=queue) offloads execution to isolated Redis-backed worker containers. - Best flat-rate host for this 5-container stack: A 2 vCPU / 8 GB RAM KVM VPS such as Hostinger VPS (KVM 2 around $8.79/mo promo) runs
n8n-main, 2–3n8n-workerreplicas, PostgreSQL 16, Redis 7.2, and Caddy with zero per-execution charges. - Managed alternative: If you prefer Git-push PaaS over Docker Compose ops, Railway supports multi-service Redis + Postgres templates, though 24/7 metered RAM/CPU typically scales to $30–$65+/mo for multi-worker setups.
If you already deployed a single-container n8n server and started seeing slow editor loads or 504 webhook timeouts during bursty AI agent runs, you have outgrown single-process execution.
Below is the exact 2026 production Docker Compose Queue Mode stack—including the container_name scaling trap most GitHub gists get wrong, memory guardrails, and horizontal worker scaling.
Side-by-Side Comparison
| Architecture | Concurrent AI Agents | Webhook Reliability Under Load | Monthly Cost (24/7) | Horizontal Worker Scaling |
|---|---|---|---|---|
| Single-Container n8n + SQLite | Low (write locks) | Drops webhooks during heavy loops | ~$5–$6/mo VPS | No |
| n8n Queue Mode + Postgres + Redis on Hostinger VPS | High (multi-worker) | High (main node only ingests webhooks) | ~$8.79/mo (KVM 2) | Yes (--scale n8n-worker=3) |
| Multi-Service Queue Stack on Railway | High (managed) | High | ~$30–$65+/mo (metered) | Yes (dashboard replicas) |
| n8n Cloud Pro | Managed tiers | High | ~€50/mo (10k executions) | Managed by n8n |
How Queue Mode Architecture Works
In Queue Mode, responsibilities split cleanly across four layers:
caddyterminates TLS (automatic Let's Encrypt HTTPS) and proxies traffic strictly to internaln8n-main:5678.n8n-mainserves the web editor, schedules cron triggers, and receives inbound webhooks—then immediately enqueues jobs into Redis without blocking.redis(Bull queue) buffers pending workflow executions so traffic spikes never overwhelm your server.n8n-worker(1 to N replicas) pulls jobs from Redis, executes LLM/API nodes in parallel, and writes state to PostgreSQL 16.
Step 1: Production .env File
Create /opt/n8n-queue/.env (chmod 600 .env). Both n8n-main and every n8n-worker must share the exact same N8N_ENCRYPTION_KEY:
DOMAIN_NAME=agents.yourdomain.com
SSL_EMAIL=ops@yourdomain.com
TIMEZONE=UTC
# Generate with: openssl rand -hex 32
N8N_ENCRYPTION_KEY=replace_with_64_char_hex_encryption_key
N8N_USER_MANAGEMENT_JWT_SECRET=replace_with_64_char_hex_jwt_secret
DB_POSTGRESDB_DATABASE=n8n
DB_POSTGRESDB_USER=n8n_user
DB_POSTGRESDB_PASSWORD=ReplaceWithStrongPostgresPass2026!
QUEUE_BULL_REDIS_PASSWORD=ReplaceWithStrongRedisPass2026!
Step 2: Scalable docker-compose.yml (With Scaling Trap Fixed)
Notice two critical production details below:
- No
container_name:onn8n-worker: Many tutorials hardcodecontainer_name: n8n-worker, which crashes the moment you rundocker compose up -d --scale n8n-worker=3. - Explicit
mem_limitcaps: Guarantees a runaway LLM JSON loop inside a worker cannot OOM-kill PostgreSQL or Redis.
volumes:
postgres_storage:
redis_storage:
n8n_storage:
caddy_data:
caddy_config:
networks:
n8n-net:
driver: bridge
x-n8n-env: &n8n-env
N8N_ENCRYPTION_KEY: ${N8N_ENCRYPTION_KEY}
N8N_USER_MANAGEMENT_JWT_SECRET: ${N8N_USER_MANAGEMENT_JWT_SECRET}
DB_TYPE: postgresdb
DB_POSTGRESDB_HOST: postgres
DB_POSTGRESDB_PORT: 5432
DB_POSTGRESDB_DATABASE: ${DB_POSTGRESDB_DATABASE}
DB_POSTGRESDB_USER: ${DB_POSTGRESDB_USER}
DB_POSTGRESDB_PASSWORD: ${DB_POSTGRESDB_PASSWORD}
QUEUE_BULL_REDIS_HOST: redis
QUEUE_BULL_REDIS_PORT: 6379
QUEUE_BULL_REDIS_PASSWORD: ${QUEUE_BULL_REDIS_PASSWORD}
EXECUTIONS_MODE: queue
OFFLOAD_MANUAL_EXECUTIONS_TO_WORKERS: "true"
EXECUTIONS_DATA_PRUNE: "true"
EXECUTIONS_DATA_MAX_AGE: "168"
GENERIC_TIMEZONE: ${TIMEZONE}
NODE_ENV: production
services:
postgres:
image: postgres:16-alpine
restart: unless-stopped
mem_limit: 1536m
environment:
POSTGRES_DB: ${DB_POSTGRESDB_DATABASE}
POSTGRES_USER: ${DB_POSTGRESDB_USER}
POSTGRES_PASSWORD: ${DB_POSTGRESDB_PASSWORD}
volumes:
- postgres_storage:/var/lib/postgresql/data
networks:
- n8n-net
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${DB_POSTGRESDB_USER} -d ${DB_POSTGRESDB_DATABASE}"]
interval: 5s
timeout: 5s
retries: 5
redis:
image: redis:7.2-alpine
restart: unless-stopped
mem_limit: 512m
command: redis-server --appendonly yes --requirepass ${QUEUE_BULL_REDIS_PASSWORD}
volumes:
- redis_storage:/data
networks:
- n8n-net
healthcheck:
test: ["CMD", "redis-cli", "-a", "${QUEUE_BULL_REDIS_PASSWORD}", "ping"]
interval: 5s
timeout: 5s
retries: 5
n8n-main:
image: docker.n8n.io/n8nio/n8n:latest
restart: unless-stopped
mem_limit: 1536m
environment:
<<: *n8n-env
N8N_HOST: ${DOMAIN_NAME}
WEBHOOK_URL: https://${DOMAIN_NAME}/
ports:
- "127.0.0.1:5678:5678"
volumes:
- n8n_storage:/home/node/.n8n
networks:
- n8n-net
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
n8n-worker:
image: docker.n8n.io/n8nio/n8n:latest
restart: unless-stopped
mem_limit: 1536m
command: worker --concurrency=10
environment:
<<: *n8n-env
volumes:
- n8n_storage:/home/node/.n8n
networks:
- n8n-net
depends_on:
n8n-main:
condition: service_started
postgres:
condition: service_healthy
redis:
condition: service_healthy
caddy:
image: caddy:alpine
restart: unless-stopped
mem_limit: 256m
ports:
- "80:80"
- "443:443"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile:ro
- caddy_data:/data
- caddy_config:/config
networks:
- n8n-net
depends_on:
- n8n-main
Step 3: Launch & Scale Workers Horizontally
Pair this with a standard Caddy reverse proxy (reverse_proxy n8n-main:5678 with response_header_timeout 300s), lock down UFW (22, 80, 443 only), and launch two parallel workers immediately:
docker compose up -d --scale n8n-worker=2
docker compose ps
Verify Redis queue connectivity and live worker registration:
docker compose logs --tail=30 n8n-worker
You should see n8n worker is now ready on both replicas. On an 8 GB RAM VPS (like Hostinger VPS KVM 2), 2 workers at --concurrency=10 comfortably process 20 concurrent AI agent workflows without slowing down the web UI.
Practical Workload Separation Tip
Keep your Docker Queue Mode stack dedicated to agent orchestration, RAG pipelines, and webhook processing. For reputation-sensitive workloads like high-volume B2B cold email outreach, hand off multi-inbox rotation and warm-up to a specialized platform like Salesforge via API/webhook rather than trying to run SMTP mail servers on your automation VPS.
Who Should Choose What?
- Choose Self-Hosted n8n Queue Mode on Hostinger VPS (KVM 2) if you run concurrent webhooks, AI agents, or client automations and want predictable sub-$10/month flat hosting with horizontal worker scaling.
- Choose Railway if you want managed multi-service Postgres + Redis + worker replicas without touching Ubuntu or Caddy.
- Choose n8n Cloud if your team prefers zero infrastructure ownership and stays within managed execution tiers.
Recommended Tools Mentioned in This Guide
| Tool | Starting Price | Best For | Official Link |
|---|---|---|---|
| Hostinger VPS | $4.99/mo | Low-cost KVM VPS for self-hosting n8n, Ollama, and autonomous AI agent workers | Get Hostinger VPS |
| n8n Cloud | Self-hosted Free / €20/mo Cloud | Developers building custom LangChain & AI agent pipelines with full code control | Start with n8n |
| Railway | $5/mo usage-based | Deploying Python AI agents, n8n instances, and APIs in under 60 seconds | Deploy on Railway |
| Salesforge | $48/mo | Multi-inbox AI cold email personalization and deliverability infrastructure | Explore Salesforge |
Frequently Asked Questions
When should I upgrade self-hosted n8n from single-container to Queue Mode?
Switch to Queue Mode as soon as you run concurrent AI agent webhooks, scheduled cron jobs that overlap, or heavy LLM loops that make the n8n web editor laggy or cause webhook timeouts.
How much RAM do I need for n8n Queue Mode with PostgreSQL and Redis?
An 8 GB RAM KVM VPS (such as Hostinger KVM 2) is the comfortable production sweet spot: ~1.5 GB for PostgreSQL, ~512 MB for Redis, ~1.5 GB for n8n-main, and 1.5 GB each for 2 n8n-worker replicas.
Why did docker compose up --scale n8n-worker=3 fail in my setup?
Most failures happen because container_name: is hardcoded on the n8n-worker service in docker-compose.yml. Removing container_name lets Docker Compose assign unique names to each worker replica.
Do all n8n workers need the same N8N_ENCRYPTION_KEY?
Yes. Both n8n-main and every n8n-worker replica must receive the exact same N8N_ENCRYPTION_KEY environment variable so workers can decrypt stored API credentials from PostgreSQL.