Ravindra BagaleCourses & study guides मराठी

Chapter 17: Interview Questions and Job Readiness

DevOps

17.9 Scenario questions

Why: the problem

This is the lesson panels like most: the full scenario round. The interviewer says: "It is Friday 8 p.m. Customers on our Myntra-like site get 502 Bad Gateway right after a deploy. You are on call. Talk me through it." They might add twists: "the rollback also fails", "only some users see it", "the database CPU is at 100 percent". There is no single answer. They grade your order, your commands, your communication and your follow-up. In this lesson you will rebuild a small version of that system on your laptop with Docker Compose: nginx in front, an app behind it, and a database. You will cause a 502, a slow database and a partial outage, and solve each one with a fixed incident method.

What we will build

You will run nginx, a Python app and Postgres with Docker Compose. You will break the app port to cause a 502, add a slow query to cause timeouts, and run two app copies where one is broken. Each time you will follow the same method: Detect, Triage, Mitigate, Fix, Follow up.

What: every term in plain English

  • 502 Bad Gateway — nginx reached for the app but got no valid answer: app down, wrong port or crash.
  • 504 Gateway Timeout — nginx waited but the app was too slow.
  • Upstream — The server behind nginx, here our app.
  • Mitigate — Stop the user pain fast, even before the real fix. Rollback is a mitigation.
  • Partial outage — Some users or some servers fail, others are fine.
  • Incident commander — The person who leads the incident and keeps everyone in sync.
  • Status update — A short message every 15-30 minutes: impact, action, next update.
  • Postmortem — A written review with timeline, root cause and action items.
Make the lab Start the stack Check it works Break it: wrong port deploy Detect Triage: nginx error log
Incident method: Detect, Triage, Mitigate, Fix, Follow up

How: step by step

  1. Make the lab

    mkdir -p ~/iq/inc && cd ~/iq/inc
    

    What it does: Files from Example 1 go here.

    Output

    (no output)
    
  2. Start the stack

    docker compose up -d --build
    

    What it does: Builds the app and starts all three.

    Output

    ✔ Container inc-db-1   Started
    ✔ Container inc-app-1  Started
    ✔ Container inc-web-1  Started
    
  3. Check it works

    curl -s localhost:8080/health
    

    What it does: Healthy before we break it.

    Output

    ok db=up
    
  4. Break it: wrong port deploy

    APP_PORT=9000 docker compose up -d app
    

    What it does: Like a bad config in a new release.

    Output

    ✔ Container inc-app-1  Started
    
  5. Detect

    curl -s -o /dev/null -w "%{http_code}\n" localhost:8080/health
    

    What it does: Users now get 502.

    Output

    502
    
  6. Triage: nginx error log

    docker compose logs web --tail 3
    

    What it does: nginx tells you which upstream failed.

    Output

    connect() failed (111: Connection refused) while connecting to upstream, upstream: "http://172.20.0.3:5000/health"
    

Example 1: The lab stack used in all scenarios

Stack. Docker Compose, nginx, Flask, Postgres

Goal. The lab stack used in all scenarios.

compose.yaml

services:
  db:
    image: postgres:16
    environment: { POSTGRES_PASSWORD: labonly }
    healthcheck: { test: ["CMD", "pg_isready"], interval: 5s }
  app:
    build: ./app
    environment:
      APP_PORT: ${APP_PORT:-5000}
      DB_URL: postgresql://postgres:labonly@db:5432/postgres
    depends_on: { db: { condition: service_healthy } }
  web:
    image: nginx:1.27
    ports: ["8080:80"]
    volumes: ["./nginx.conf:/etc/nginx/conf.d/default.conf:ro"]
    depends_on: [app]

app/app.py

import os, time, psycopg
from flask import Flask
app = Flask(__name__)
@app.get("/health")
def health():
    with psycopg.connect(os.environ["DB_URL"], connect_timeout=2) as c:
        c.execute("select 1")
    return "ok db=up"
@app.get("/slow")
def slow():
    with psycopg.connect(os.environ["DB_URL"]) as c:
        c.execute("select pg_sleep(8)")
    return "done"
app.run(host="0.0.0.0", port=int(os.environ["APP_PORT"]))

app/Dockerfile

FROM python:3.12-slim
RUN pip install --no-cache-dir flask "psycopg[binary]"
COPY app.py /app.py
CMD ["python", "/app.py"]

nginx.conf

server {
  listen 80;
  location / {
    proxy_pass http://app:5000;
    proxy_read_timeout 5s;
  }
}

What each line does

  • healthcheck pg_isready — The app waits until Postgres is really ready.
  • APP_PORT default 5000 — We can break it by setting 9000.
  • connect_timeout=2 — Fail fast if the DB is gone.
  • /slow — A route with an 8 second query to create timeouts.
  • proxy_read_timeout 5s — nginx gives up after 5 seconds and returns 504.

Green output

ok db=up

Expected result. A tiny but real three-tier system you can break in many ways.

Example 2: Scenario 1: 502 after deploy, the full spoken answer

Stack. Docker Compose

Goal. Scenario 1: 502 after deploy, the full spoken answer.

Commands

# Detect
curl -s -o /dev/null -w "%{http_code}\n" localhost:8080/health    # 502
# Triage: from the edge inwards
docker compose ps
docker compose logs web --tail 3
docker compose logs app --tail 3
docker compose exec app sh -c "env | grep APP_PORT"
# Mitigate: roll back the config
APP_PORT=5000 docker compose up -d app
curl -s localhost:8080/health

What each line does

  • From the edge inwards — nginx first, then app, then DB. Never guess.
  • docker compose ps — Is every container Up?
  • web logs — Connection refused to app:5000 means the app is not on 5000.
  • app logs — Shows Running on port 9000.
  • Rollback — Put back the last good config. Users are happy again.

Green output

502
app-1  * Running on http://0.0.0.0:9000
APP_PORT=9000
ok db=up

Expected result. Say: Detect 502, nginx says connection refused, app listens on 9000, rolled back in 3 minutes, follow-up: add a smoke test on /health after each deploy.

Example 3: Scenario 2: 504 because the database is slow

Stack. Postgres, nginx

Goal. Scenario 2: 504 because the database is slow.

Commands

curl -s -o /dev/null -w "%{http_code} %{time_total}\n" localhost:8080/slow   # 504 after 5 s
docker compose logs web --tail 2
docker compose exec db psql -U postgres -c \
  "select pid, state, now()-query_start as age, left(query,40) from pg_stat_activity where state='active';"
docker stats --no-stream
# Mitigate: stop the slow query
docker compose exec db psql -U postgres -c "select pg_cancel_backend(pid) from pg_stat_activity where query like '%pg_sleep%' and pid != pg_backend_pid();"

What each line does

  • 504 after 5 s — Matches proxy_read_timeout. nginx gave up.
  • upstream timed out — nginx log confirms the app was slow.
  • pg_stat_activity — Shows running queries and how long they have run.
  • pg_cancel_backend — Stops one query without killing the DB.
  • Follow-up — Add an index, a query timeout, and an alert on p99 latency.

Green output

504 5.004
upstream timed out (110: Connection timed out) while reading response header from upstream
  pid | state  |   age    | left
 312  | active | 00:00:06 | select pg_sleep(8)

Expected result. You move from HTTP code to nginx log to the exact slow query.

Example 4: Scenario 3: only some users fail

Stack. Docker Compose

Goal. Scenario 3: only some users fail.

Commands

docker compose up -d --scale app=2
docker compose exec --index 2 app sh -c "kill 1"     # one copy dies
for i in $(seq 1 10); do curl -s -o /dev/null -w "%{http_code} " localhost:8080/health; done; echo
docker compose ps app
docker compose logs app --tail 5
docker compose up -d --scale app=2                   # replace the dead one

What you say

1. Errors are about half the requests, not all. So one backend is bad.
2. compose ps shows app-2 Exited.
3. Restore capacity first, then read app-2 logs for the reason.
4. In AWS: the ALB target group shows one unhealthy target.
5. Follow-up: health checks so the load balancer removes bad targets.

What each line does

  • --scale app=2 — Two copies behind nginx, like two EC2 behind an ALB.
  • kill 1 — Kills the main process of one copy.
  • Mixed 200 and 502 — The tell-tale sign of a partial outage.
  • Restore capacity — Bring back the copy.
  • Health checks — Let the balancer skip bad targets automatically.

Green output

200 502 200 502 200 200 502 200 502 200
inc-app-1  Up
inc-app-2  Exited (137)

Expected result. A partial outage story with a clear pattern and a clear fix.

Verify: did it work?

  1. All good again

    curl -s localhost:8080/health && docker compose ps --format "{{.Name}} {{.State}}"
    

    What it does: Every service running.

    Output

    ok db=up
    inc-app-1 running
    inc-db-1 running
    inc-web-1 running
    
  2. Write the timeline

    docker compose logs --since 30m --timestamps web | grep -E " (502|504) " | head -3
    

    What it does: Real timestamps for the postmortem.

    Output

    2026-10-25T20:01:14 ... "GET /health" 502
    

When it is red 1

host not found in upstream "app:5000"

Cause. nginx started before the app existed or the service name is wrong.

Fix, step by step

  1. Check the service name in compose.yaml.
  2. docker compose up -d web after app is up.

When it is red 2

psycopg.OperationalError: connection refused

Cause. App started before the DB was ready.

Fix, step by step

  1. Use depends_on with condition: service_healthy.
  2. Add retry on connect.

When it is red 3

Rollback also fails

Cause. The old version needs something the new deploy changed, like a DB migration.

Fix, step by step

  1. Use backward compatible migrations (expand, then contract).
  2. Roll forward with a hotfix if rollback is impossible.

Cleanup

  1. Stop the lab

    docker compose down -v
    

    What it does: Removes containers and the DB volume.

    Output

    ✔ Container inc-web-1 Removed
    

Ravindra Bagale's Tip

In an incident the order is: Detect, Triage, Mitigate, Fix, Follow up. Comfort users first, then find the cause.

Practice tasks

Medium Write status updates

Why: Communication is half the grade in incident rounds.

What to do: Write three status updates for Scenario 1 at 0, 15 and 30 minutes.

How:
  1. Impact, what we know, what we do, next update time.

Check: Each update is three lines and has a time.

Hard Add a smoke test to the deploy

Why: The best follow-up is automation.

What to do: Write a script that deploys and rolls back if /health is not 200.

How:
  1. Deploy, sleep 5, curl /health, if not 200 run the rollback command.

Check: A bad port triggers an automatic rollback.

Easy Say Incident scenarios in two lines

Why: Interviewers cut you off after 30 seconds. The short version must come first.

What to do: Write a two line answer about Incident scenarios for a fresher interview.

How:
  1. Line 1: the problem it solves.
  2. Line 2: the one command or file you used.
  3. Say it aloud with a timer.

Check: You finish inside 30 seconds and you name one real command.

Easy Run the commands, not just read them

Why: An interviewer can tell if you only read. Typing the command once gives you the real output to talk about.

What to do: Run every command in the How part of this lesson on your own lab machine.

How:
  1. Start a small Ubuntu VM, WSL or EC2 t3.micro.
  2. Run each step and save the output in notes.txt.
  3. Mark any output that looks different from the lesson.

Check: notes.txt has the real output of every step.

Medium Break it on purpose

Why: You must know the red error by face. In a live round 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.

How:
  1. Follow the error section.
  2. Copy the exact message into your notes.
  3. Write the fix in one line.

Check: You fixed it using only your notes.

Medium Answer in the 4-line shape

Why: A clear shape makes any answer sound senior: Situation, Check, Fix, Prevent.

What to do: Pick one interview question from this lesson and answer in four lines.

How:
  1. Situation: what broke.
  2. Check: the command you ran and what it showed.
  3. Fix: the change you made.
  4. Prevent: the alert, test or doc you added.

Check: Each of the four lines has a real noun, like a command, a file or a number.

Medium Mock round with a friend

Why: Speaking is a different skill from knowing. Practice it.

What to do: Let a friend ask you three questions from this lesson.

How:
  1. Friend picks random questions.
  2. You answer, friend times you.
  3. Swap and repeat.

Check: You answer all three without saying "I am not sure" twice.

Hard Whiteboard it top to bottom

Why: Many panels say "draw it". A neat top-to-bottom drawing shows you see the whole flow.

What to do: Draw the main flow of this lesson as boxes from top to bottom.

How:
  1. Start at the user or the trigger.
  2. Add each hop: server, container, pipeline, tool.
  3. Write the command you use to check each box beside it.

Check: Every box has one check command next to it.

Hard Write a mini runbook

Why: Good engineers leave notes for the next person. Interviewers love to hear that you do this.

What to do: Write a one page runbook for the main scenario in this lesson.

How:
  1. Title, symptoms, first 3 checks, fix, how to confirm, how to undo.

Check: A teammate can follow it alone at 2 a.m.

Homework: try at home

Try at home

At home, break the lab in a new way (wrong DB password, full disk on the DB volume, nginx typo). Run the full Detect, Triage, Mitigate, Fix, Follow up method and write a 10 line postmortem.

Recap

  • Use one method: Detect, Triage, Mitigate, Fix, Follow up.
  • Triage from the edge inwards: nginx, app, DB.
  • 502 is a dead or wrong upstream; 504 is a slow one.
  • Mixed 200 and 502 means a partial outage.
  • Roll back first, then find the root cause, then automate the check.

FAQ

Should I always roll back?

If a deploy is the likely cause and rollback is safe, yes. Restore users first.

What if I do not know the answer in the round?

Say what you would check next and why. Panels value the method.

How long should a scenario answer be?

Two to four minutes, with pauses for their questions.

Do I need to mention communication?

Yes. Say who you inform and how often.

Can I use this lab in my portfolio?

Yes. A README with these three incidents is a strong project.

Interview questions

  1. Users see 502 after a deploy. Walk me through it.

    Confirm with curl, check nginx or ALB logs, check app container or pod status and logs, compare config with last release, roll back, confirm, then postmortem and add a smoke test.

  2. The site is slow, not down. What do you check?

    Latency graphs, upstream timeout logs, DB slow queries, CPU and memory, recent changes.

  3. Only some users fail. Why?

    One bad backend, one AZ issue, a cache node, or a canary version. Check per-target health.

  4. Rollback is not possible. What then?

    Roll forward with a small hotfix, or turn off the feature flag.

  5. What is a blameless postmortem?

    A review that fixes the process, not the person, with clear action items.

  6. How do you prevent the same incident?

    Automated test or smoke check, alert on the early signal, runbook, and safer deploy like canary.

Quick quiz

Q1. 502 vs 504?

502: no valid answer from upstream. 504: upstream too slow.

Q2. Where do you start triage?

At the edge: load balancer or nginx, then inwards.

Q3. What is mitigation?

Stopping user pain fast, such as rollback.

Q4. Half the requests fail. What does it suggest?

One of the backends is bad.

Q5. What goes in a postmortem?

Timeline, impact, root cause, action items, no blame.

Did you understand? Let us try it once more.

Ravi Sir's Cards for this lesson

मराठी cards