What is horizontal scaling vs vertical scaling in the cloud?
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
Vertical scaling means giving one node more CPU, memory, or storage. Horizontal scaling means adding nodes or workers and distributing the workload.
For example, if one Spring Boot instance is CPU-bound, moving from four to eight vCPUs is vertical scaling. Running two four-vCPU instances behind a load balancer is horizontal scaling. The second design can keep serving if one instance fails, but only if health checks, capacity, and required state are available elsewhere.
Horizontal scaling is not automatically cheaper or limitless. More instances can increase database connections, cache traffic, coordination, and network cost. Also, 'stateless' is not the only possible design: shared sessions or partitioned state can work, though they add complexity. I choose after measuring the bottleneck and considering the availability target, state model, scaling speed, machine limits, and total cost.
What is horizontal scaling vs vertical scaling in the cloud
Direct Answer
Vertical scaling gives one machine more CPU, memory, or storage. Horizontal scaling adds more service instances and distributes work among them. Horizontal scaling can improve capacity and fault tolerance, but it is not always cheaper and usually requires stateless request handling or shared/externalized state; vertical scaling is often simpler until one machine's limits become important.
Code Example
# Vertical scaling: replace the instance with a bigger one (downtime to resize)
aws ec2 modify-instance-attribute \
--instance-id i-123 --instance-type m5.4xlarge # more CPU/RAM, one box
# Horizontal scaling: add more identical instances behind a load balancer
aws autoscaling set-desired-capacity \
--scaling when configured-group-name order-svc-asg --desired-capacity 6
# Interchangeable horizontal instances need stateless or deliberately shared/partitioned state — keep session outside the JVM:
# spring.session.store-type=redis (any instance can serve any request)
Common Mistakes & Pitfalls
- Calling horizontal scaling infinitely scalable: shared databases, queues, quotas, and coordination impose limits.
- Saying an application must have no state: state needs an explicit placement and consistency design.
- Assuming vertical resize always causes downtime: behavior depends on the service/platform.
- Assuming horizontal is cheaper: include load balancers, idle headroom, network, licenses, and operational cost.
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.