惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
Martin Fowler
Martin Fowler
B
Blog RSS Feed
D
DataBreaches.Net
L
LangChain Blog
月光博客
月光博客
S
SegmentFault 最新的问题
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
美团技术团队
Jina AI
Jina AI
博客园 - 司徒正美
雷峰网
雷峰网
Last Week in AI
Last Week in AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
IT之家
IT之家
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
小众软件
小众软件
罗磊的独立博客
博客园_首页
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
A
About on SuperTechFans
Engineering at Meta
Engineering at Meta

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Deploy Your Apps with 0 downtime Part 1 (Blue-Green Deplo...
satriaherman · 2026-05-21 · via DEV Community

Overview

Have you ever experienced your app can't be accessed whenever you release a new version of your app?. In this series, we'll explore how to deploy updates with zero downtime, starting with one of the most reliable strategies — Blue-Green Deployment.

When we deploy an application on Cloud provider like AWS, Azure or GCP, we usually do like this:

  • Shutting down the app
  • compiled new version code
  • Start app again

Looks like there's no problem with steps above until we noticed that our app is getting down or can't be accessed during the deployment process. we call this situation as downtime.

In this article we will learn how to prevent that using a one of deployment strategies called Blue-Green Deployment.

What it is Blue-Green Deployment?

Blue-Green Deployment is a strategy which we will switch the traffic from previous version instances(blue) to another identical new release instances(green) which are running on production environment. To achieve this goal, load balancer is playing crucial function to switching traffic

In this tutorial, we will use docker as a container manager and nginx as load balancer. We will deploy nextjs app and simulate it to achieve 0 downtime by using blue green deployment

Before we started, make sure you already install software bellow:

Notes: make sure you are familiar with linux environment

Install Docker and Nginx

open wsl and type bellow to install nginx

sudo apt install nginx

Enter fullscreen mode Exit fullscreen mode

after nginx installed we will install docker,nodejs and git

sudo apt install docker nodejs git

Enter fullscreen mode Exit fullscreen mode

after you installed the tools above, please create directory following image bellow

Setup Docker

We will use docker compose to run the app and nginx inside the container. so on Dockerfile, write this code

FROM node:20-alpine

WORKDIR /app

COPY package*.json ./

RUN npm install

COPY . .

RUN npm run build

EXPOSE 3000

CMD ["npm", "start"]

Enter fullscreen mode Exit fullscreen mode

Before Implement Blue Green Deployment

Before we start to setup the blue green deployment. i will show you the deployment before implement Blue Green deployment. i will write the docker-compose.yml like this.

services:
  app:
    build: ./app
    container_name: app
    ports:
      - "5001:3000"


  nginx:
    image: nginx:latest
    container_name: nginx-bluegreen
    ports:
      - "80:80"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/active_backend.conf:/etc/nginx/conf.d/active_backend.conf

Enter fullscreen mode Exit fullscreen mode

and for nginx.conf

events {}

http {



    server {
        listen 80;

        location / {
            proxy_pass http://host.docker.internal:5001;

            proxy_http_version 1.1;

            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";

            proxy_set_header Host $host;
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

and then i will write the deployment script on deploy.sh

#!/bin/bash

set -e

echo "================================="
echo "Starting Deployment..."
echo "================================="

echo ""
echo "[1/5] Building container..."

docker compose build app

echo ""
echo "[2/5] Stopping old container..."

docker compose stop app

echo ""
echo "[3/5] Removing old container..."

docker compose rm -f app

echo ""
echo "[4/5] Starting new container..."

docker compose up -d --no-deps app

echo ""
echo "[5/5] Running health check..."

sleep 5

STATUS=$(curl -s -o /dev/null -w "%{http_code}" http://localhost/api/health-check)

if [ "$STATUS" != "200" ]; then
    echo "Deployment failed!"
    exit 1
fi

echo ""
echo "================================="
echo "Deployment Success!"
echo "================================="

Enter fullscreen mode Exit fullscreen mode

the i will running the container using docker compose

docker compose up

Enter fullscreen mode Exit fullscreen mode

and now when i visit http://localhost the page will showing

now i will run ./deploy.sh to simulate the deployment process. when i refresh the page many times, i got the 502 bad gateway we called this as downtime

the downtime occur because i need to stop the container while i building new image version. so, the page will unavailable for some time. to prevent the downtime we need to create 2 app, one for active app and one for standby app. so when new release detected, we will rebuild the standby app, do the healthy check to make sure the app is healthy, then when app is detected as healthy app, we switch the traffic to new release app. here's the ilustration

Implement Blue Green

To Implement the Blue Green deployment, we will change our deployment mechanism like this:

  • deploy 2 services called app_blue and app_green
  • configure the nginx to route the traffic to app_blue
  • write the current active app to "blue"
  • new deployment detected
  • rebuild app_green
  • do health check the app_green
  • if healthy switch traffic to app_green
  • write the current active app to "green"

Create Healthy Check Route

Inside the nextjs app, we will create a route to indicates that the application is running well and healthy

open app/api/health-check/route.ts and write the code below:

export async function GET() {
  return Response.json({
    ok: true
  })
}

Enter fullscreen mode Exit fullscreen mode

run your app

npm run dev

Enter fullscreen mode Exit fullscreen mode

then test the script by running

curl http://localhost:3000/api/health

Enter fullscreen mode Exit fullscreen mode

it should return

{"ok":true}

Enter fullscreen mode Exit fullscreen mode

after that, we will setup the docker compose to running 2 nextjs app and nginx as load balancer.

services:
  app_blue:
    build: ./app
    container_name: app-blue
    ports:
      - "5001:3000"

  app_green:
    build: ./app
    container_name: app-green
    ports:
      - "5002:3000"

  nginx:
    image: nginx:latest
    container_name: nginx-bluegreen
    ports:
      - "80:80"
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/active_backend.conf:/etc/nginx/conf.d/active_backend.conf

Enter fullscreen mode Exit fullscreen mode

on code above, we created 2 services for apps that running on port 5001and 5002. and we created nginx services as a load balancer that will switch the traffic everytime when the new deployment found.

Setup Nginx

open nginx folder on the root folder and create nginx.conf

events {}

http {

    include /etc/nginx/conf.d/active_backend.conf;

    server {
        listen 80;

        location / {
            proxy_pass http://active_backend;

            proxy_http_version 1.1;

            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";

            proxy_set_header Host $host;
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

In nginx.conf, we forward all requests to the active backend defined in active_backend.conf, which is imported from the nginx folder.

on active_backend.conf we will write this configuration file

upstream active_backend {
    server host.docker.internal:5001;
}

Enter fullscreen mode Exit fullscreen mode

on active_backend.conf we created the upstream named active_backend. the active_backend is route all request to localhost:5001 that running under the docker container. so we must define host.docker.internal:5001

Store Current Active App

To store current active app. we will create new file called active_slot which is contain txt content

green

Enter fullscreen mode Exit fullscreen mode

Deployment Script

We will create file deploy.sh to simulate our deployment script including :

  • check the active app
  • rebuild the standby app
  • hit the health check of new release app
  • switching the traffic to new release app
#!/bin/bash

set -e

ACTIVE=$(cat active_slot)

if [ "$ACTIVE" = "blue" ]; then
    TARGET="green"
    TARGET_PORT=5002
    TARGET_SERVICE="app_green"
else
    TARGET="blue"
    TARGET_PORT=5001
    TARGET_SERVICE="app_blue"
fi

echo "============================"
echo "Current Active : $ACTIVE"
echo "Deploy Target  : $TARGET"
echo "============================"

echo ""
echo "[1/5] Rebuilding $TARGET_SERVICE..."

docker compose build $TARGET_SERVICE
docker compose up -d --no-deps $TARGET_SERVICE

echo ""
echo "[2/5] Waiting app startup..."

sleep 10

echo ""
echo "[3/5] Running health check..."

for i in {1..20}
do
    STATUS=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:$TARGET_PORT/api/health-check)

    if [ "$STATUS" = "200" ]; then
        echo "Health check success!"
        break
    fi

    echo "Retry $i..."
    sleep 3
done

if [ "$STATUS" != "200" ]; then
    echo "Health check failed!"
    exit 1
fi

echo ""
echo "[4/5] Switching nginx traffic..."

cat > nginx/active_backend.conf <<EOF
upstream active_backend {
    server host.docker.internal:$TARGET_PORT;
}
EOF

docker exec nginx-bluegreen nginx -s reload

echo "$TARGET" > active_slot

echo ""
echo "[5/5] Deployment success!"
echo ""
echo "Active slot now: $TARGET"

Enter fullscreen mode Exit fullscreen mode

look at

docker compose build $TARGET_SERVICE
docker compose up -d --no-deps $TARGET_SERVICE

Enter fullscreen mode Exit fullscreen mode

because we are running 2 services from same image we need to add

--no-deps

Enter fullscreen mode Exit fullscreen mode

options to prevent the docker restart the active app while it rebuild the image. so the active app can still running.

edit page.tsx and run the deploy.sh. now the downtime is gone while we deploy new version in our app