NestJS course Β· Module 9: Deployment and Infrastructure

PROJECT: Full deployment of a NestJS application to production

4 min read Β·
In this lesson2

Legate! Your application runs on your laptop, but Consul Caesar.js asks the questions that come up in every real deployment: what happens when the server goes dark at three in the morning? Who will notice that the database is slowing down? How do you know yesterday's backup can be restored? The Consul entrusts you with the greatest challenge - a complete deployment of the Roman legion to production. It is the true test of the skills you have gained during our NestJS campaign.

Project Specification

Your task is to create a complete deployment system for the Legionary Legion API application. Every part builds on the lessons of this and earlier modules, so before you start, go back to them like to staff maps. The hours given are an approximate amount of work.

1. Infrastructure (4-5 hours)

  • Docker and Docker Compose configuration
  • Kubernetes/Helm charts setup
  • CI/CD pipeline configuration
  • Monitoring and logging

Start with an image built in a multi-stage build and a Compose file that brings up the application together with MongoDB and Redis. In Kubernetes describe the replicas, the rolling update strategy and the readiness and liveness probes. The pipeline should run the tests, build the image and deploy it only after the tests are green.

2. Security (2-3 hours)

  • SSL/TLS implementation
  • Security headers and rate limiting
  • Secrets management
  • Vulnerability scanning

You already know HTTPS, Helmet headers and throttler limits. Keep secrets outside the repository and the image, and let the application refuse to start when one of them is missing. Add a vulnerability scanner, e.g. Trivy, to the pipeline, so that an image with a critical flaw never reaches production.

3. Performance (2-3 hours)

  • Load balancing and auto-scaling
  • Caching strategy
  • Database optimization
  • CDN setup

Measure before you optimize: a load test result before and after your changes is the best evidence. Match indexes to the most frequent queries, add a Redis cache with well-thought-out invalidation, and cache headers for the CDN. Autoscaling adds replicas based on metrics such as CPU, so the application must be stateless: sessions and caches live in Redis, not in process memory.

4. Monitoring (2-3 hours)

  • Health checks
  • Metrics collection
  • Alerting system
  • Log aggregation

Split health endpoints into liveness and readiness, expose metrics for Prometheus, and write logs in JSON with a request identifier. An alert should wake someone up only when something needs to be done - an alarm that is too loud quickly stops being heard.

5. Backup & Recovery (1-2 hours)

  • Automated backups
  • Disaster recovery plan
  • Data integrity checks

Run backups automatically, set your RTO and RPO, then carry out one full restore on a fresh environment and write down how long it took.

Evaluation Criteria

  • Completeness - all components work together
  • Security - proper safeguards on every layer, from TLS to secrets
  • Performance - the application handles production traffic, as a load test confirms
  • Monitoring - full observability: metrics, logs and alerts
  • Documentation - deployment and recovery runbooks

The mentor will assess the project from the repository, so the README must contain the start-up commands, an architecture description and the results of the restore test. I recommend writing the runbook alongside the configuration not at the end - whatever you do not write down straight away will be mere guesswork at three in the morning.

Good luck, legate! Your legion is counting on you, and you will send the finished system to your mentor at the end of the module.

Code for this lesson: deployment-project/docker-compose.yml
# PROJECT: Full NestJS Application Deployment to Production

version: '3.8'

services:
  # === API NestJS ===
  api:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: roman-api
    restart: unless-stopped
    ports:
      - "4000:4000"
    environment:
      NODE_ENV: production
      PORT: 4000
      MONGO_URI: mongodb://mongo:27017/roman_empire
      REDIS_HOST: redis
      REDIS_PORT: 6379
      JWT_SECRET: ${JWT_SECRET}
      ALLOWED_ORIGINS: https://roman-empire.com
    depends_on:
      mongo:
        condition: service_healthy
      redis:
        condition: service_healthy
    healthcheck:
      test: ["CMD", "node", "-e",
        "require('http').get('http://localhost:4000/health', (r) => { process.exit(r.statusCode === 200 ? 0 : 1) })"]
      interval: 30s
      timeout: 10s
      retries: 3
      start_period: 40s
    networks:
      - roman-network

  # === MongoDB ===
  mongo:
    image: mongo:7
    container_name: roman-mongodb
    restart: unless-stopped
    volumes:
      - mongo-data:/data/db
      - ./scripts/backup.sh:/scripts/backup.sh
    environment:
      MONGO_INITDB_DATABASE: roman_empire
    healthcheck:
      test: ["CMD", "mongosh", "--eval", "db.runCommand('ping').ok"]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - roman-network

  # === Redis Cache ===
  redis:
    image: redis:7-alpine
    container_name: roman-redis
    restart: unless-stopped
    command: redis-server --appendonly yes
    volumes:
      - redis-data:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 3s
      retries: 3
    networks:
      - roman-network

  # === Nginx Reverse Proxy ===
  nginx:
    image: nginx:alpine
    container_name: roman-nginx
    restart: unless-stopped
    ports:
      - "80:80"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
    depends_on:
      - api
    networks:
      - roman-network

# Volumes
volumes:
  mongo-data:
    driver: local
  redis-data:
    driver: local

# Network
networks:
  roman-network:
    driver: bridge

# Commands:
# docker-compose up -d        # Run
# docker-compose logs -f api  # API logs
# docker-compose down         # Stop
# docker-compose ps           # Status

Spotted a mistake in this lesson?

Useful articles