Production Go API Stack
Production Go API with Nginx, PostgreSQL, Redis caching, and structured logging
Overview
NGINX is a high-performance web server and reverse proxy that has become the backbone of modern web infrastructure, powering over 400 million websites worldwide. Originally developed to solve the C10K problem of handling 10,000 concurrent connections, NGINX uses an event-driven, asynchronous architecture that dramatically outperforms traditional process-based servers under high load conditions.
This production stack combines NGINX's proven reverse proxy capabilities with Go's exceptional performance characteristics for building scalable APIs. The architecture leverages PostgreSQL's ACID compliance and advanced JSON support for reliable data persistence, while Redis provides sub-millisecond caching and session storage. Go's compiled binary approach, combined with NGINX's efficient connection handling, creates a stack capable of serving thousands of concurrent API requests with minimal resource overhead.
This configuration targets engineering teams building high-throughput APIs that need production-grade reliability without the complexity of cloud-native orchestration. The stack excels for fintech applications requiring transaction integrity, e-commerce platforms with high concurrent user loads, and SaaS products where response time directly impacts user experience. The combination of Go's garbage collector optimizations and Redis caching can reduce database load by 80-90% while maintaining data consistency through PostgreSQL's robust transaction system.
Key Features
- Event-driven NGINX architecture handles 10,000+ concurrent connections with minimal memory footprint
- Go's compiled binary deployment eliminates runtime dependencies and reduces container size to under 20MB
- PostgreSQL JSONB support enables hybrid relational-document queries with full ACID compliance
- Redis Lua scripting enables atomic rate limiting and complex caching operations in sub-millisecond timeframes
- Multi-stage Docker builds separate development dependencies from production runtime for enhanced security
- Structured logging with Go's context package provides distributed tracing across NGINX, API, and database layers
- PostgreSQL connection pooling through Go's database/sql package optimizes resource utilization under load
- NGINX upstream health checks automatically route traffic away from failed API instances
Common Use Cases
- 1E-commerce APIs requiring sub-100ms response times with Redis product catalog caching
- 2Financial services applications needing PostgreSQL's ACID transactions for payment processing
- 3Multi-tenant SaaS platforms using PostgreSQL row-level security and Redis session isolation
- 4Real-time analytics dashboards leveraging PostgreSQL's window functions and Redis pub/sub
- 5Content management systems utilizing PostgreSQL's full-text search with Redis query result caching
- 6IoT data ingestion APIs using Go's goroutines for concurrent processing and PostgreSQL time-series storage
- 7Mobile app backends requiring JWT authentication with Redis token blacklisting and PostgreSQL user management
Prerequisites
- Minimum 2GB RAM (1GB for PostgreSQL, 512MB for Redis, 256MB for Go API, 256MB for NGINX)
- Docker Engine 20.10+ with BuildKit support for multi-stage Dockerfile optimization
- Available ports 80 (NGINX), 5432 (PostgreSQL), 6379 (Redis), or configure alternatives via environment variables
- Understanding of Go module structure and PostgreSQL migration patterns for application development
- Basic knowledge of NGINX upstream configuration for production load balancing scenarios
- SSL certificate management if deploying with HTTPS termination at NGINX layer
For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms
docker-compose.yml
docker-compose.yml
1services: 2 nginx: 3 image: nginx:alpine4 container_name: go-nginx5 restart: unless-stopped6 ports: 7 - "${NGINX_PORT:-80}:80"8 volumes: 9 - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro10 depends_on: 11 - api1213 api: 14 build: 15 context: .16 dockerfile: Dockerfile17 target: production18 container_name: go-api19 restart: unless-stopped20 environment: 21 - GIN_MODE=release22 - PORT=808023 - DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@db:5432/${DB_NAME}?sslmode=disable24 - REDIS_URL=redis://redis:637925 - JWT_SECRET=${JWT_SECRET}26 depends_on: 27 db: 28 condition: service_healthy29 redis: 30 condition: service_started3132 db: 33 image: postgres:16-alpine34 container_name: go-db35 restart: unless-stopped36 environment: 37 - POSTGRES_USER=${DB_USER}38 - POSTGRES_PASSWORD=${DB_PASSWORD}39 - POSTGRES_DB=${DB_NAME}40 volumes: 41 - postgres_data:/var/lib/postgresql/data42 healthcheck: 43 test: ["CMD-SHELL", "pg_isready -U ${DB_USER}"]44 interval: 5s45 timeout: 5s46 retries: 54748 redis: 49 image: redis:7-alpine50 container_name: go-redis51 restart: unless-stopped52 command: redis-server --appendonly yes53 volumes: 54 - redis_data:/data5556 migrate: 57 build: 58 context: .59 dockerfile: Dockerfile60 target: migrate61 container_name: go-migrate62 environment: 63 - DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@db:5432/${DB_NAME}?sslmode=disable64 depends_on: 65 db: 66 condition: service_healthy67 profiles: 68 - tools6970volumes: 71 postgres_data: 72 redis_data: .env Template
.env
1# Go API Production Stack2NGINX_PORT=8034# Security5JWT_SECRET=your-jwt-secret-key67# Database8DB_USER=goapi9DB_PASSWORD=goapi_password10DB_NAME=goapi_dbUsage Notes
- 1Multi-stage Dockerfile for small production image
- 2Run migrations: docker compose --profile tools up migrate
- 3API uses Redis for caching and rate limiting
- 4Nginx handles SSL termination and load balancing
- 5Health checks ensure proper startup order
- 6Binary compiled with CGO_ENABLED=0 for Alpine
Individual Services(5 services)
Copy individual services to mix and match with your existing compose files.
nginx
nginx:
image: nginx:alpine
container_name: go-nginx
restart: unless-stopped
ports:
- ${NGINX_PORT:-80}:80
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
depends_on:
- api
api
api:
build:
context: .
dockerfile: Dockerfile
target: production
container_name: go-api
restart: unless-stopped
environment:
- GIN_MODE=release
- PORT=8080
- DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@db:5432/${DB_NAME}?sslmode=disable
- REDIS_URL=redis://redis:6379
- JWT_SECRET=${JWT_SECRET}
depends_on:
db:
condition: service_healthy
redis:
condition: service_started
db
db:
image: postgres:16-alpine
container_name: go-db
restart: unless-stopped
environment:
- POSTGRES_USER=${DB_USER}
- POSTGRES_PASSWORD=${DB_PASSWORD}
- POSTGRES_DB=${DB_NAME}
volumes:
- postgres_data:/var/lib/postgresql/data
healthcheck:
test:
- CMD-SHELL
- pg_isready -U ${DB_USER}
interval: 5s
timeout: 5s
retries: 5
redis
redis:
image: redis:7-alpine
container_name: go-redis
restart: unless-stopped
command: redis-server --appendonly yes
volumes:
- redis_data:/data
migrate
migrate:
build:
context: .
dockerfile: Dockerfile
target: migrate
container_name: go-migrate
environment:
- DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@db:5432/${DB_NAME}?sslmode=disable
depends_on:
db:
condition: service_healthy
profiles:
- tools
Quick Start
terminal
1# 1. Create the compose file2cat > docker-compose.yml << 'EOF'3services:4 nginx:5 image: nginx:alpine6 container_name: go-nginx7 restart: unless-stopped8 ports:9 - "${NGINX_PORT:-80}:80"10 volumes:11 - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro12 depends_on:13 - api1415 api:16 build:17 context: .18 dockerfile: Dockerfile19 target: production20 container_name: go-api21 restart: unless-stopped22 environment:23 - GIN_MODE=release24 - PORT=808025 - DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@db:5432/${DB_NAME}?sslmode=disable26 - REDIS_URL=redis://redis:637927 - JWT_SECRET=${JWT_SECRET}28 depends_on:29 db:30 condition: service_healthy31 redis:32 condition: service_started3334 db:35 image: postgres:16-alpine36 container_name: go-db37 restart: unless-stopped38 environment:39 - POSTGRES_USER=${DB_USER}40 - POSTGRES_PASSWORD=${DB_PASSWORD}41 - POSTGRES_DB=${DB_NAME}42 volumes:43 - postgres_data:/var/lib/postgresql/data44 healthcheck:45 test: ["CMD-SHELL", "pg_isready -U ${DB_USER}"]46 interval: 5s47 timeout: 5s48 retries: 54950 redis:51 image: redis:7-alpine52 container_name: go-redis53 restart: unless-stopped54 command: redis-server --appendonly yes55 volumes:56 - redis_data:/data5758 migrate:59 build:60 context: .61 dockerfile: Dockerfile62 target: migrate63 container_name: go-migrate64 environment:65 - DATABASE_URL=postgres://${DB_USER}:${DB_PASSWORD}@db:5432/${DB_NAME}?sslmode=disable66 depends_on:67 db:68 condition: service_healthy69 profiles:70 - tools7172volumes:73 postgres_data:74 redis_data:75EOF7677# 2. Create the .env file78cat > .env << 'EOF'79# Go API Production Stack80NGINX_PORT=808182# Security83JWT_SECRET=your-jwt-secret-key8485# Database86DB_USER=goapi87DB_PASSWORD=goapi_password88DB_NAME=goapi_db89EOF9091# 3. Start the services92docker compose up -d9394# 4. View logs95docker compose logs -fOne-Liner
Run this command to download and set up the recipe in one step:
terminal
1curl -fsSL https://docker.recipes/api/recipes/production-go-api-stack/run | bashTroubleshooting
- Connection refused on port 80: Check NGINX_PORT environment variable and ensure no conflicts with existing services
- Go API panics with database connection errors: Verify DATABASE_URL format and ensure PostgreSQL health check passes before API startup
- Redis connection timeouts: Increase Redis maxclients setting or implement connection pooling in Go application code
- NGINX 502 Bad Gateway: Confirm Go API is listening on port 8080 and check upstream configuration in nginx.conf
- PostgreSQL migration failures: Run migrations separately using docker compose --profile tools up migrate before starting main services
- High memory usage in Go API: Enable Go's garbage collector debug logging with GOGC environment variable tuning
Community Notes
Loading...
Loading notes...
Download Recipe Kit
Get all files in a ready-to-deploy package
Includes docker-compose.yml, .env template, README, and license
Components
nginxgopostgresredis
Tags
#go#golang#api#production#nginx#postgres#redis
Category
Full Web StacksAd Space
Shortcuts: C CopyF FavoriteD Download