CD2: Running many containers together

Lesson 4 of 5 · reading 15–25 minutes; practice and project time is on the module page

Several copies behind one address

🎯You'll be able to

Run several copies of an app behind a load balancer, and explain how new copies are found and why the app must not keep state in memory.

🏠The everyday picture

A railway station has one "Tickets" sign and several counters under it. Passengers queue at the sign and go to whichever counter is free. If a counter closes, the queue moves to the others. A counter can't keep your half-finished booking in its drawer, because next time you may reach a different one.

📘What it really is

📝A worked example

Input: after Project 1, run node tools/load.js http://localhost:8080/api/routes 20 10 with 2 copies, then set app_count = 3, terraform apply, and run it again.

Expected output: the load tool prints per_second, p95 and failed: 0 each time, and the x-served-by header shows 2, then 3 different copies. On the 4-core test machine, nginx with RouteRani served about 1,100 to 1,160 requests per second (measured 1 November 2026); your numbers depend on your computer, and more copies help only while the computer has spare CPU.

🌍Where it's used

📝Case scenario

A shopping site stored each user's cart in the memory of the copy that served them. When they went from one copy to four for a festival sale, users' carts kept "emptying" as requests landed on different copies. Moving carts into Redis made any copy able to serve any request, and the team could add copies freely.

🖱️Try it

With 2 or more copies running, stop one with docker stop while node tools/load.js http://localhost:8080/api/routes 20 10 runs. Success: failed stays at 0 because nginx retries on the other copy, and docker ps shows the remaining copy still healthy.

⚠️Common mistakes

🛠️For your project

Project 1 test 5 needs at least two healthy copies answering through the load balancer, and Project 3's autoscaler changes the count.

✅Quick check

Why does RouteRani's nginx use a variable in proxy_pass?

Show the answer

So it looks the name app up again regularly and finds copies added or removed later.

Why must copies be stateless?

Show the answer

Any request can reach any copy, so state kept in one copy is missing on the others.

← PreviousCompose: the whole app in one file
ProLearners Assist