Escalabilidad
Escalado horizontal frente a vertical
Escrito por Darjus Kazakevic · revisado el
Scale up first, scale out when you must, and expect the bottleneck to move.
Scalability means that adding resources adds capacity in proportion. Vertical scaling (scaling up) moves to a bigger machine. It is simple but limited: there is a largest machine, the price rises faster than the power, and it is still one machine that can fail. Horizontal scaling (scaling out) adds machines behind a load balancer. It has no fixed ceiling, but any instance must be able to serve any request.
The cost is that every step reveals the next bottleneck. Once the app tier scales, the database becomes the limit, and you reach for replicas, caches and shards. Amdahl's law sets the ceiling: if 5% of the work is serial, no number of machines makes it more than 20 times faster.
En esencia — Adding a machine should add capacity in proportion: scale up while it is cheap, scale out with interchangeable instances, and expect the limit to move to the data.
Cuándo recurrir a él
Recurre a él cuando
- Load is growing past what one machine can serve
- Traffic swings by hour or season, so capacity should follow it
- You need several instances anyway, for availability
Reconsidéralo cuando
- A bigger instance still handles the load cheaply — scale up first
- The bottleneck is serial, like one primary, so more app servers won't help
- The service keeps state in memory that you cannot move out yet
Conceptos clave
- Vertical scaling (Scaling up)
- Move the workload to a machine with more CPU, memory or faster disks. It needs no code change, so it is the first move. The largest cloud instances have hundreds of cores and terabytes of memory.
- Horizontal scaling (Scaling out)
- Add more identical instances and spread requests across them with a load balancer. Capacity grows with the instance count, as long as nothing pins a request to one instance.
- The serial part (Amdahl's law)
- The work that cannot run in parallel, such as one database primary or a global lock. It sets the maximum speed-up, whatever you add elsewhere.
- Scaling the data tier
- State must scale too. Read replicas spread reads, a cache absorbs repeated reads, and sharding (splitting data by key across databases) spreads writes. Each step is harder to undo than the last.
Reglas de diseño
- Scale up first while it is cheap
- A bigger machine needs no code change. Move to many small machines when the largest instance is near its limit, costs too much, or is too big a single failure.
- Find the next bottleneck before buying more
- Each step moves the limit, usually to the database. Measure where requests wait (CPU, locks, connections) before adding more of a resource that is already idle.
- Move state out before adding instances
- Scaling out needs any instance to serve any request. Sessions, local files and private caches pin users to one box, so move them to shared stores first.
- Keep headroom at peak
- Run each tier at 50–70% of its limit at peak. The spare capacity absorbs a failed instance, a deploy and a sudden spike while the autoscaler starts new instances.
Errores habituales
- Sharding before trying a cache and read replicas. Sharding is the hardest step to undo, and a cache often removes most of the reads.
- Scaling the app tier while the database is the limit. More instances open more connections, and the database gets slower, not faster.
- Autoscaling on CPU for a service that waits on I/O. CPU stays low while requests queue; scale on in-flight requests or latency instead.
- No upper bound on the autoscaler. A retry storm or a bug scales to hundreds of instances and a very large bill.