Sub topic 12
Choosing a strategy for your workload
This chapter assumes you know the earlier material in this tutorial: latency, throughput and utilization, vertical and horizontal scaling, load balancing, stateless design, read replicas and sharding, and Amdahl's law. It pulls those pieces into one decision process.
Start from the measurement
Pick a strategy only after you know which resource is saturated. Scaling the wrong tier adds cost and changes nothing for users.
Check these in order:
- Is latency or error rate over target? If not, you do not need to scale yet.
- Which tier is the bottleneck: application servers, database, cache, or network?
- Which resource in that tier is saturated: CPU, memory, disk I/O, or connections?
- Is the load growing steadily, arriving in spikes, or both?
The answers narrow the options quickly. A saturated application tier with idle databases points to adding application servers. A database at its write limit points to a bigger machine first, and to sharding later.
Comparing the two approaches
| Criterion | Vertical scaling | Horizontal scaling |
|---|---|---|
| Application changes | Usually none | Stateless servers, shared session storage |
| Speed of change | Resize, often with a restart | Add instances, no downtime if load-balanced |
| Failure impact | One machine is a single point of failure | Losing one node loses a fraction of capacity |
| Upper limit | Largest available machine | Coordination and serial work (Amdahl's law) |
| Cost curve | Large machines cost more per unit of capacity | Roughly linear, plus load balancer and operations overhead |
| Operational complexity | Low | Higher: deployment, monitoring, consistency |
Neither column wins everywhere. The workload decides which row matters most.
A decision flow
Worked example
An online shop shows p95 latency of 900 ms against a 300 ms target during evening peaks. Metrics show the two application servers at 90% CPU, while the database sits at 30% CPU and 20% of its connections.
The bottleneck is the application tier. Sessions are stored in server memory, so the app is not yet stateless. The plan:
- Move sessions to a shared store such as Redis.
- Put the servers behind a load balancer using round robin or least connections.
- Let an autoscaler adjust the count, since evening peaks are predictable but vary in size.
On Kubernetes, the autoscaler can be this:
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70The minimum of 2 keeps the service available if one instance fails. The maximum of 10 caps cost and protects the database from a sudden flood of new connections.
Before committing, estimate the ceiling. If 5% of request handling is serial, such as a lock on a shared resource, Amdahl's law applies:
def amdahl_speedup(parallel_fraction, workers):
return 1 / ((1 - parallel_fraction) + parallel_fraction / workers)
print(round(amdahl_speedup(0.95, 16), 2)) # 9.14
print(round(amdahl_speedup(0.95, 1000), 2)) # 19.63Sixteen servers give about 9x, and no number of servers gives more than 20x. Here that is plenty, but if the serial share were 30%, adding servers would stop helping very early.
Common errors
- Scaling before measuring. Doubling the web tier does nothing if the database is the constraint. It can make things worse by opening twice as many database connections.
- Scaling out a stateful app. Users get logged out or lose carts when requests land on different servers.
- Treating autoscaling as capacity planning. New instances take time to start, so a sharp spike can still overload the system. Keep headroom, and set a sensible maximum.
- Ignoring the data tier. Application servers scale out easily. The database often does not, so check it before you promise growth.
- Choosing horizontal by default. For a small, steady workload, one larger machine with a standby can be cheaper and simpler than a cluster.
- Forgetting the next step. A vertical choice today is fine if you know what you will do when it runs out, such as replicas, then sharding.
A practical default is to scale the application tier horizontally once it is stateless, scale the database vertically for as long as it is affordable, and add replicas, caching, and then sharding as its limits approach.
Exercise
A service has three application servers at 40% CPU. The database primary is at 95% CPU, and 90% of its queries are reads. Sessions are already kept in a shared store. What would you do first, and why?
Show answer
Scale the data tier, not the application tier. Since most queries are reads, add read replicas or a cache and route reads to them. Adding application servers would increase database load and make the bottleneck worse. If writes later become the limit, scale the primary vertically, and consider sharding only when the largest machine is not enough.
Exercise
A workload has 20% serial work. Using Amdahl's law, what is the maximum speedup from adding servers, and what does that imply for choosing horizontal scaling?
Show answer
The maximum speedup is 1 / 0.20 = 5x, however many servers you add. Horizontal scaling will stop paying off well before that. You should reduce the serial portion, for example by removing a shared lock, or use a faster machine for that portion, before adding many servers.