Chapter 4: Linux Ops Refresh for DevOps
4.7 Worked triage story — peak traffic
Why: the problem
Think of BookMyShow at 10 a.m. when tickets for a big cricket match open. Ten lakh people press refresh at the same second. The booking page becomes slow, then some users get 502 errors. The war room is full of people with ideas. "Add more servers!" "Restart the database!" "Roll back the release!" Each idea might help or might make it worse. A good on-call engineer follows a fixed order instead: confirm what users see, check the three basics, read the logs for the error window, look at resources, then make one change and measure again. This lesson is a full worked incident from start to end. You will create a real slow and failing setup on one server with nginx, a small app and a load test tool. Then you will walk the triage steps in order, write down evidence at each step, fix the real cause, and prove it with numbers. This is exactly how you will answer the interview question "tell me about an outage you handled".
What we will build
On one Ubuntu 24.04 VM you will run nginx in front of a small Python app that has only a few worker threads. You will use the tool hey to send 2000 requests with 100 users at once, like a ticket sale. You will see slow replies and 502 and 504 errors. Then you will triage in order: user view, three checks, logs, resources, config. You will find two real causes: the app has too few workers, and nginx has a short timeout. You will fix both, run the same load again and compare before and after numbers. Finally you will write a short incident note.
What: every term in plain English
- Incident — Any event where users are hurt: errors, slowness or downtime.
- Peak traffic — The busiest moment, like a sale or ticket launch.
- Load test — Sending many fake requests on purpose to see how a system behaves.
- hey — A small load test tool. You say how many requests and how many users at once.
- Latency — How long one request takes. p95 means 95 out of 100 requests were faster than this.
- Upstream — The app behind nginx. nginx is the front door, upstream is the room.
- Worker — A thread or process in the app that handles one request at a time.
- 504 — Gateway timeout. nginx waited too long for the app.
- Incident note — A short written record: what happened, when, why, what fixed it, what we will do next.
How: step by step
-
Log in to the lab VM
ssh web1What it does: Use the EC2 from lesson 4.6 or any Ubuntu 24.04 VM.
Output
ubuntu@ip-10-0-1-20:~$ -
Install tools
sudo apt-get install -y -qq nginx python3 heyWhat it does: nginx, Python and the hey load tool.
Output
Setting up hey (0.1.4-3) ... -
Start the slow app
sudo systemctl start bookingWhat it does: The unit from Example 1. Each request takes 0.3 seconds and only 2 workers exist.
Output
(no output) -
Confirm the user view
curl -s -o /dev/null -w "%{http_code} %{time_total}s\n" localhost/bookWhat it does: One request alone is fine.
Output
200 0.305s -
Create the peak
hey -n 2000 -c 100 http://localhost/bookWhat it does: 2000 requests, 100 at once, like a ticket launch.
Output
Status code distribution: [200] 610 responses [502] 120 responses [504] 1270 responses -
Triage in order
bash three-checks.sh nginx 80; sudo tail -n 3 /var/log/nginx/error.log; top -b -n 1 | head -5What it does: Basics, then logs, then resources.
Output
upstream timed out (110: Connection timed out) -
Fix and measure again
hey -n 2000 -c 100 http://localhost/bookWhat it does: Same test after the fix.
Output
Status code distribution: [200] 2000 responses
Example 1: Build the slow booking app
Stack. Ubuntu 24.04, nginx 1.24, Python 3.12, systemd
Goal. Build the slow booking app.
/opt/booking/app.py
import os, time
from concurrent.futures import ThreadPoolExecutor
from http.server import BaseHTTPRequestHandler, HTTPServer
from socketserver import ThreadingMixIn
WORKERS = int(os.environ.get("WORKERS", "2"))
pool = ThreadPoolExecutor(max_workers=WORKERS)
class H(BaseHTTPRequestHandler):
def do_GET(self):
fut = pool.submit(time.sleep, 0.3) # pretend to call the seat database
fut.result()
self.send_response(200); self.end_headers()
self.wfile.write(b"seat booked\n")
def log_message(self, *a):
pass
class S(ThreadingMixIn, HTTPServer):
daemon_threads = True
print(f"booking up with {WORKERS} workers", flush=True)
S(("127.0.0.1", 5000), H).serve_forever()
/etc/systemd/system/booking.service
[Unit]
Description=Booking API
After=network.target
[Service]
User=www-data
Environment=WORKERS=2
ExecStart=/usr/bin/python3 /opt/booking/app.py
Restart=on-failure
[Install]
WantedBy=multi-user.target
/etc/nginx/sites-available/booking
server {
listen 80 default_server;
location / {
proxy_pass http://127.0.0.1:5000;
proxy_read_timeout 2s;
}
}
Turn it on
sudo mkdir -p /opt/booking && sudo cp app.py /opt/booking/
sudo cp booking.service /etc/systemd/system/
sudo cp booking /etc/nginx/sites-available/booking
sudo ln -sf /etc/nginx/sites-available/booking /etc/nginx/sites-enabled/default
sudo systemctl daemon-reload && sudo systemctl enable --now booking
sudo nginx -t && sudo systemctl reload nginx
curl -s localhost/book
What each line does
WORKERS = 2— Only two requests can be served at the same moment. This is the hidden bottleneck.time.sleep(0.3)— Pretends each booking needs 0.3 seconds of database work.ThreadingMixIn— Accepts many connections, but they all wait for the 2 workers.Environment=WORKERS=2— The worker count comes from the unit, so we can change it without code.proxy_read_timeout 2s— nginx waits only 2 seconds for the app. Too short for a queue.nginx -t— Test config before reload, every time.
Green output
Created symlink /etc/systemd/system/multi-user.target.wants/booking.service
nginx: configuration file /etc/nginx/nginx.conf test is successful
seat booked
Expected result. Works for one user. Now we hit it like a ticket sale.
Example 2: Run the peak and collect evidence in order
Stack. hey, nginx logs, ss, top
Goal. Run the peak and collect evidence in order.
Step 1: user view under load
hey -n 2000 -c 100 http://localhost/book | sed -n '1,12p;/Status code/,$p'
Step 2: three checks
pgrep -a python3; ss -tlnp | grep -E ":80 |:5000 "; df -h / | tail -1
Step 3: logs for the window
sudo awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c
sudo grep -c "upstream timed out" /var/log/nginx/error.log
sudo tail -n 1 /var/log/nginx/error.log
Step 4: resources during load
hey -n 2000 -c 100 http://localhost/book >/dev/null &
sleep 3; uptime; nproc; free -m | head -2; ss -tn state established '( dport = :5000 )' | wc -l
wait
What each line does
hey -n 2000 -c 100— The peak. Total requests and users at once.Status code distribution— The user view in numbers: how many got 200, 502, 504.three checks green— Process, ports and disk are fine. The server is not down, it is overloaded.upstream timed out— nginx gave up after 2 seconds waiting for the app.load average low, CPU idle— The CPU is not the problem. Requests are waiting in a queue.ss ... dport = :5000— About 100 connections are waiting on the app at once.
Green output
Summary:
Total: 18.4 secs
Slowest: 2.0021 secs
Fastest: 0.3010 secs
Average: 1.7820 secs
Requests/sec: 108.7
Status code distribution:
[200] 610 responses
[502] 120 responses
[504] 1270 responses
1544 /usr/bin/python3 /opt/booking/app.py
LISTEN 0 511 0.0.0.0:80 0.0.0.0:* users:(("nginx",pid=1890,fd=6))
LISTEN 0 5 127.0.0.1:5000 0.0.0.0:* users:(("python3",pid=1544,fd=3))
/dev/root 30G 9.1G 21G 31% /
610 200
120 502
1270 504
1270
2026/10/25 10:02:14 [error] 1891#1891: *4120 upstream timed out (110: Connection timed out) while reading response header from upstream, upstream: "http://127.0.0.1:5000/book"
10:02:40 up 3 days, load average: 0.31, 0.20, 0.10
2
Mem: 3911 980 2400
101
Expected result. Evidence: basics green, CPU idle, 1270 timeouts, 100 connections queued. The app has too few workers, and Send-Q of 5 on port 5000 shows a tiny listen queue that causes the 502s.
Example 3: Fix the real causes and measure again
Stack. systemd override, nginx, hey
Goal. Fix the real causes and measure again.
Fix 1: more workers
sudo systemctl edit booking
# in the editor add these two lines, save and exit:
# [Service]
# Environment=WORKERS=40
sudo systemctl restart booking
journalctl -u booking -n 1 --no-pager
Fix 2: a sane timeout
sudo sed -i 's/proxy_read_timeout 2s;/proxy_read_timeout 15s;/' /etc/nginx/sites-available/booking
sudo nginx -t && sudo systemctl reload nginx
Measure with the same test
hey -n 2000 -c 100 http://localhost/book | sed -n '1,8p;/Latency distribution/,/99%/p;/Status code/,$p'
What each line does
systemctl edit booking— Creates an override file. The original unit stays untouched.Environment=WORKERS=40— The override wins over WORKERS=2 in the unit.booking up with 40 workers— The journal proves the new value is live.proxy_read_timeout 15s— Gives the app time during a short peak, instead of failing at 2 seconds.Same hey command— Always compare with the exact same test, or the numbers mean nothing.95% in 0.31 secs— Most users now wait just over the 0.3 second database time.
Green output
Oct 25 10:06:01 ip-10-0-1-20 python3[1702]: booking up with 40 workers
nginx: configuration file /etc/nginx/nginx.conf test is successful
Summary:
Total: 15.6 secs
Slowest: 0.6120 secs
Fastest: 0.3008 secs
Average: 0.3150 secs
Requests/sec: 128.2
Latency distribution:
50% in 0.3040 secs
95% in 0.3110 secs
99% in 0.4010 secs
Status code distribution:
[200] 2000 responses
Expected result. Before: 610 of 2000 succeeded. After: 2000 of 2000, and p95 dropped from 2 seconds to 0.31. One measured change at a time.
Example 4: Write the incident note
Stack. Markdown
Goal. Write the incident note.
incident-2026-10-25.md
# Booking 5xx during ticket launch - 25 Oct 2026
When: 10:00 to 10:07 IST (7 minutes)
Impact: 1390 of 2000 booking requests failed (502/504)
Timeline
- 10:00 traffic peak, 100 users at once
- 10:02 on-call ran three checks: all green
- 10:03 nginx error.log: 1270 "upstream timed out"
- 10:04 resources: CPU idle, 100 connections queued on :5000
- 10:06 raised WORKERS 2 to 40, timeout 2s to 15s
- 10:07 retest: 2000/2000 OK, p95 0.31s
Root cause
App had 2 workers. Requests queued, nginx timed out at 2s.
Actions
1. Load test before every ticket launch (owner: Ravi)
2. Alert when 5xx > 1% for 2 minutes (owner: Priya)
3. Keep WORKERS in config, reviewed in each release
Save it next to the evidence
mkdir -p ~/incidents/2026-10-25
cp incident-2026-10-25.md ~/incidents/2026-10-25/
bash triage.sh && mv triage-*.txt ~/incidents/2026-10-25/
ls ~/incidents/2026-10-25
What each line does
When and Impact— Times in IST and numbers, not "some users".Timeline— Each step with a time. Shows the order you followed.Root cause— One or two plain lines. The real cause, not the symptom.Actions— Each with an owner, so it really happens.triage.sh— The report script from lesson 4.5 saves the system picture next to the note.
Green output
saved triage-ip-10-0-1-20-2026-10-25-1008.txt
incident-2026-10-25.md triage-ip-10-0-1-20-2026-10-25-1008.txt
Expected result. A clear note with numbers. This is also your answer to "tell me about an outage you handled".
Verify: did it work?
-
Error rate is zero
sudo tail -n 2000 /var/log/nginx/access.log | awk '$9 ~ /^5/' | wc -lWhat it does: No 5xx lines in the last 2000 requests.
Output
0 -
Override is active
systemctl cat booking | tail -3What it does: Shows the override file and the new value.
Output
# /etc/systemd/system/booking.service.d/override.conf [Service] Environment=WORKERS=40
When it is red 1
hey: command not found
Cause. The tool is not installed or not in the Ubuntu version you use.
Fix, step by step
- sudo apt-get install -y hey on Ubuntu 22.04 and newer.
- Or use ab from apache2-utils: ab -n 2000 -c 100 http://localhost/book
- Never run load tests against a live site you do not own.
When it is red 2
upstream prematurely closed connection while reading response header from upstream
Cause. The app closed the connection, often when its listen queue overflowed or it crashed.
Fix, step by step
- Check journalctl -u booking for crashes.
- Check ss -tlnp: a tiny Send-Q backlog means the queue is too small.
- Raise workers or backlog, then load test again.
When it is red 3
Still slow after adding workers
Cause. The real limit is somewhere else, like the database or CPU.
Fix, step by step
- Run uptime and top during the test.
- If CPU is at 100%, add CPU or servers instead of workers.
- If CPU is idle, look at the database or external API times.
Cleanup
-
Stop the lab app
sudo systemctl disable --now booking && sudo rm -rf /etc/systemd/system/booking.service /etc/systemd/system/booking.service.d && sudo systemctl daemon-reloadWhat it does: Removes the app and its override.
Output
Removed "/etc/systemd/system/multi-user.target.wants/booking.service".
Ravindra Bagale's Tip
In a war room everyone gives ideas. You hold the order: user view, basics, logs, resources. One change, then measure. Keep that in mind!
War room मध्ये सगळे ideas देतात. तुम्ही क्रम धरा: user view, basics, logs, resources. एक change, मग measure. लक्षात ठेवा!
War room में सब ideas देते हैं. तुम क्रम पकड़ो: user view, basics, logs, resources. एक change, फिर measure. याद रखो!
Practice tasks
Easy Count the damage
Why: Numbers make incidents clear.
What to do: From access.log, count requests per status in the incident window.
- awk and uniq -c
Check: You can say how many users were hit.
Medium Change one thing
Why: Two changes at once hide which one worked.
What to do: Apply only the timeout fix first and measure, then the workers fix.
- Run hey after each.
Check: You can say how much each fix helped.
Hard Add an alert
Why: The next incident should page you early.
What to do: Write a one-line cron check that prints ALERT when 5xx in the last 1000 lines is above 10.
- tail, awk, wc -l, if.
Check: ALERT prints during a load test.
Easy Say peak traffic triage in two lines
Why: If you can say it simply, you understand it. Juniors and interviewers both want the short version first.
What to do: Write two short lines about peak traffic triage for a fresher who knows only basic Linux commands.
- Write the problem it solves.
- Write the one command you use for it.
- Read it aloud to a friend.
Check: A fresher understands it without asking a question.
Easy Make a safe practice server
Why: Ops commands can stop services or fill disks. Practice on a machine you can throw away.
What to do: Start a throwaway Ubuntu machine and do this lesson there.
- docker run -it --rm --name ops-lab ubuntu:24.04 bash
- Or launch a t3.micro Ubuntu 24.04 EC2 with port 22 open only to your IP.
- Delete it when you are done.
Check: All practice happens on the lab machine, not on a real server.
Medium Repeat it on Amazon Linux
Why: Companies use both Ubuntu and Amazon Linux. Package names and log files differ a little.
What to do: Do the main steps of this lesson again on Amazon Linux 2023.
- docker run -it --rm amazonlinux:2023 bash
- Install tools with dnf instead of apt.
- Note every difference in your notes.
Check: You can list two differences between Ubuntu and Amazon Linux for this lesson.
Medium Break it on purpose
Why: You must know the red message by face. In a real outage there is no time to read slowly.
What to do: Cause one error from the "When it is red" part of this lesson and fix it.
- Follow the error section.
- Copy the exact message into your notes.
- Write the fix in one line.
Check: You fixed it using only the steps, without searching the web.
Medium Write a one-page runbook
Why: At 2 a.m. nobody remembers the right command. A short page saves the on-call person.
What to do: Write a runbook page for this lesson.
- Title, when to use, exact command, how to check, how to undo.
Check: A teammate can follow it alone.
Hard Explain it like an interview
Why: Interviewers want real stories, not definitions.
What to do: Record a 60 second answer about this lesson.
- Say the problem, the command you ran, and one error you hit.
Check: Your answer has a real command and a real error message.
Hard Draw the flow top to bottom
Why: A drawing shows the order of checks and what happens on failure.
What to do: Draw this lesson as boxes from top to bottom.
- Start at the user complaint, show each check as a box, end at the fix.
Check: Each box matches a real command you ran.
Homework: try at home
Try at home
At home, rebuild this lab and run it with WORKERS set to 2, 10 and 40. Write a small table of success count and p95 latency for each. Then write a five line incident note in your own words.
Recap
- Follow a fixed order: user view, basics, logs, resources, fix, measure.
- Green basics with errors means overload or a dependency.
- Read nginx error.log for upstream timeouts and closed connections.
- Make one change at a time and test with the same load.
- Write an incident note with times, numbers and owners.
FAQ
Is it safe to load test production?
Only with approval, at a quiet time, and with limits. Usually test a copy.
Why did we see both 502 and 504?
504 is nginx giving up after the timeout. 502 is the app closing or refusing connections when its queue was full.
Should I just add more servers?
Only if the servers are busy. Here the CPU was idle, so more servers would hide the real bug.
What is a postmortem?
A longer incident note reviewed by the team, without blame.
Which tools do companies use for this?
Grafana and Prometheus or CloudWatch for graphs, and k6, JMeter or Locust for load tests.
Interview questions
-
Tell me about an outage you handled.
During a peak, bookings returned 502 and 504. Basics were green, nginx logs showed upstream timeouts, CPU was idle and connections were queued. The app had 2 workers. I raised workers and fixed the timeout, retested with the same load, and success went from 30% to 100%.
-
How do you approach a slow application?
Measure user latency first, then check basics, logs and resources, find whether the limit is CPU, memory, disk, network or a dependency, then change one thing and measure.
-
Difference between 502 and 504?
502: bad response or connection refused from upstream. 504: upstream did not answer in time.
-
What is p95 latency and why use it?
The time 95% of requests are under. Averages hide the slow users, p95 shows them.
-
What goes into a good incident report?
Impact with numbers, timeline with times, root cause, fix, and follow-up actions with owners.
Quick quiz
Q1. What is the first step in an incident?
Confirm what users see, with numbers.
Q2. What does a 504 mean?
nginx waited too long for the app.
Q3. The CPU is idle but requests are slow. What does that suggest?
Requests are waiting in a queue, like too few workers or a slow database.
Q4. Why use the same load test after the fix?
To compare before and after fairly.
Q5. What does p95 mean?
95% of requests were faster than this time.
Did you understand? Let us try it once more.
Revision card
Interview card