Handle growing traffic with load balancing, horizontal scaling, and caching.
Lesson goal
Locate a capacity bottleneck and choose between a larger server, more application instances, or a cache.
Before you begin — Application Foundations.
Suppose more people start opening their profiles at the same time. The application runs code for every request, and the database supplies each profile. Which component runs out of capacity first determines which change will help.
Vertical scaling gives one machine more resources, such as CPU or memory. If the application server spends most of its time doing computation, more CPU can let it handle more requests. The capacity is still concentrated on that machine.
Horizontal scaling adds instances that share the work. Two application servers can handle requests concurrently if either can access the necessary data. If every request still goes to one overloaded database, adding application instances leaves that shared limit in place.
A load balancer chooses an application instance for each incoming request or connection. With equally sized servers doing similar work, round robin sends traffic to each in turn. Least connections favors the server with fewer active connections. IP hashing uses the client's address to select a server, which can keep a client's requests together while the server pool remains stable.
When configured with health checks, the load balancer removes an unresponsive instance from rotation once it detects the failure. The remaining instances now share its work. Whether users experience an outage depends in part on how much spare capacity those instances have.
A cache holds a reusable result. Suppose the same profile is requested repeatedly. The first lookup misses the cache, so the application reads the profile from storage and can save a copy. A later lookup can use that copy, reducing the work sent to the database.
The author might edit the profile between those two reads. Invalidation removes an entry after a change, and a time to live (TTL) limits how long the cache can reuse the entry before it expires. The design needs a rule for how long users may see an old display name. You will examine what happens when updates and reads overlap in Caching and Content Delivery in Practice.
Both application servers have spare CPU. Database reads are at capacity, and many requests ask for the same profiles. What would you expect from adding a third application server?
Each profile lookup would still need a database read. Measure how often the same profiles are requested and decide how fresh their values need to be. A cache could remove repeated reads if those values can safely be reused.