6. Vertical vs Horizontal Scaling
Practice: five levels
Assignments
Five levels. Do them in order. Answers are in "Solutions" at the end, so try first.
Level 1: Think (no code)
A. Scale up or scale out first? One line each.
- A small company's Postgres database is at 85% CPU on 4 cores.
- A stateless API is at 80% CPU on 10 servers and traffic grows 20% a month.
- A video-resizing service where each job is independent.
- A single-server app that keeps logged-in sessions in memory, before a big launch next week.
B. "If I kill this server right now, does any user lose anything?" Answer for each:
- An app server with sessions in Redis and uploads sent straight to object storage.
- An app server that saves uploaded profile photos to its own disk.
- A worker that has taken a job off a queue but hasn't finished it (and hasn't acknowledged it).
Level 2: Calculate
Book Fest assumptions: normal peak 2,000 requests a second, sale peak 20,000. One server handles 500 a second; run at 60%; keep 1 spare.
- How many servers for a normal evening? For the sale?
- 10% of every request's time is spent waiting on one shared lock. With 10 servers, what's the best possible speed-up? With 100? What's the limit?
- The sale peak lasts 2 hours a day; the rest of the day needs the normal fleet. Compare server-hours a day for "always run the sale fleet" vs autoscaling.
- New servers take 3 minutes to start. The sale starts with no warning at 20,000 a second while 7 servers (3,500 a second) are running. Roughly how many requests are dropped before new servers are ready?
Level 3: Build
Write a Python autoscaler simulation:
- Input: requests a second for each minute; per-server capacity; target utilisation; minimum and maximum servers; a warm-up delay before new servers serve; optional scheduled minimum from a given minute on.
- Each minute: decide how many servers you want, order new ones (ready after warm-up), remove extras only when nothing is starting, and count requests above capacity as dropped.
Check that:
- Steady 2,000 a second settles at 7 servers and drops nothing.
- A sudden jump to 20,000 drops requests while servers warm up, then settles at 67.
- Scheduling 67 servers 5 minutes before the jump drops nothing.
Level 4: Debug
The autoscaler keeps adding and removing a server every minute, even though traffic is steady. Deploy logs are a mess and users see occasional errors when servers are removed.
The steady load is worth about 2.6 servers of CPU (so 4 servers ≈ 65%, 5 servers ≈ 52%).
- Find the bug. Walk through the first four minutes starting at 4 servers.
- Fix it.
Level 5: Design
The bookstore today: one large server runs the app and the database, keeps sessions in memory, and stores uploaded book covers on its local disk. The Book Fest is in 3 weeks, at 10 times normal traffic.
Deliver:
- What you change so the app tier can scale out (list each kind of state and its new home).
- The new layout of the system.
- How you scale the database for the sale, step by step.
- Your autoscaling plan for sale day (scheduled and reactive), including min, max and the metric.
- Two risks on sale day and how you'd test for them beforehand.