Understanding Load Balancing
In this article, we’ll explore the potential benefits of implementing a load balancer in your web hosting setup for WordPress. But before we delve into whether a load balancer is necessary, let’s ensure we all have a clear understanding of its role. In its simplest form, a load balancer is a server that distributes network traffic among a series of backend servers.
Load balancers achieve this by utilizing various predefined patterns, known as load balancing algorithms. The two most common algorithms are round-robin and least connections:
- Round-Robin: Requests are distributed sequentially among the backend servers—one after the other.
- Least Connections: New requests are sent to the backend server with the fewest current connections.
By distributing network traffic to a pool of backend servers, load balancing significantly improves concurrency. In the case of WordPress, this means enhancing the number of users your site can handle simultaneously.
Traditional WordPress Hosting vs. Load-Balanced Setup
Let’s examine a typical server setup designed for WordPress:
This setup consists of:
- A single server with a fixed IP address
- Web server software, usually Nginx or Apache, installed to serve the website
- PHP and MySQL/MariaDB installed according to WordPress requirements
- Redis installed for object caching
- DNS management handled by the server provider or a third-party service like Cloudflare, pointing the domain name to the server IP address
- An SSL certificate installed on the server to ensure secure site traffic
Now, in a load-balanced setup, we extract the various server components to their own dedicated servers, resulting in a configuration like this:
In this load-balanced setup:
- A load balancer is implemented in front of one or more application servers, each with a copy of the application codebase installed.
- The DNS is updated to point the domain name to the load balancer via its public hostname or IP address.
- The SSL certificate is installed on the load balancer, which handles request routing among the available application servers. The application servers can read and write from the servers dedicated to managing the MySQL database and Redis object cache.
This load-balanced setup offers a couple of notable benefits:
- Reduced responsibilities for each server, making them more efficient. With fewer software packages installed, CPU cycles are reserved primarily for a single task. In a single server setup, Nginx, PHP, and MySQL all compete for CPU time to handle a request.
- Independent scalability for different parts of the system. If the database server becomes a bottleneck, you can increase its size or add additional replicas. Likewise, if the site experiences increased concurrent users and the application servers can’t keep up, you can add more application servers.
However, it’s important to consider the drawbacks of a load-balanced setup:
- It can be costly. Extracting each server component to its own server means a higher expense.
- It introduces complexity. Handling a single web request involves multiple moving parts, which increases as you add more servers.
To mitigate the complexity, managed services like DigitalOcean’s managed database servers and load balancers, as well as AWS’s managed database servers and load balancers, can be beneficial. However, such services come with additional costs, which brings us back to the expense factor.
Common Misconceptions About Load Balancing
Let’s address a few common misconceptions about load balancing through a performance lens.
Instant Performance Boost
Adding a load balancer to your hosting setup won’t immediately enhance application performance or throughput. When you distribute your application services across multiple servers, you introduce network latency. Even if your servers are in the same data center, multiple network connections must handle every request between each server. Typically, application servers need to communicate with the database and object cache servers.
To observe a performance gain from load balancing, your existing single server setup must be under strain. If your current server is operating comfortably at 20% utilization, load balancing will likely result in slightly slower request processing. However, when you reach a level of concurrent users that exceeds your single server’s capacity, throughput will decrease, and response times will rise.
We can test and analyze this using tools like Loader, an application load testing tool. With a suitable configuration, we can measure the impact of load balancing on your WordPress site’s performance.
Auto-scaling allows your server infrastructure to scale up or down based on concurrent requests. This approach helps manage costs by only deploying enough servers to handle the current workload.
One common example is auto-scaling application servers. You can configure your load balancing setup to create new application servers when the number of requests exceeds a certain threshold (e.g., more than x requests per application server). Similarly, when the number of requests drops, unnecessary application servers can be stopped and deleted. Without auto-scaling, your infrastructure must always be capable of handling peak traffic.
While load balancing isn’t synonymous with auto-scaling, it remains a crucial component of an auto-scaling infrastructure. Merely installing a load balancer won’t automatically expand your stack as the number of concurrent users grows.
High availability typically signifies 100% uptime—a common requirement for business-critical sites such as busy e-commerce stores. However, load balancing alone doesn’t guarantee high availability.
In a load-balanced configuration, there are still multiple potential points of failure. For instance, if an application server goes down, your site will likely continue functioning due to the availability of other app servers. However, the load balancer itself, database server, and object cache server remain vulnerable. This issue can be mitigated to some extent by opting for managed services that provide high availability options. Both Amazon Lightsail and DigitalOcean offer managed database servers with redundancy features, albeit at a higher cost.
ProgramMatek’s Approach to Load Balancing
At ProgramMatek, we handle high-traffic sites like deliciousbrains.com, and although we don’t implement a load balancer for this site, we segregate API traffic from the main web site traffic by using two domains: deliciousbrains.com and api.deliciousbrains.com. Each domain points to a separate application server running the same WordPress codebase. We have one managed MySQL database server and one managed Redis server, responsible for databases and object caching across both application servers.
While you can configure an external MySQL database via the SpinupWP dashboard, configuring an external Redis server currently requires manual setup. Nonetheless, we’re considering adding this feature to the SpinupWP dashboard as well. By managing our application servers through the SpinupWP dashboard, we ensure proper caching policies. Additionally, we offload media and asset files using WP Offload Media, serving them via a CDN.
We’ve chosen this configuration to enable independent scaling of our website and API, ensuring future flexibility. If either the website or API experiences a significant traffic surge, implementing a load balancer and additional application servers for either service wouldn’t be overly complicated.
Considering Load Balancing for Your WordPress Site
Load balancing plays a vital role in highly-trafficked WordPress sites. However, it shouldn’t be your first solution when preparing your WordPress site to handle substantial traffic.
First, ensure your site utilizes correct caching techniques by following our comprehensive guide: “WordPress Caching: All You Need To Know.” Implementing page caching and object caching can often resolve performance issues during peak traffic periods.
If caching isn’t a viable option or if you’re still experiencing performance problems, consider vertically scaling your server hardware. Sometimes, simply adding an extra CPU core or increasing memory capacity can have a significant impact on your site’s ability to handle heavy loads.
In many cases, the need to increase CPU cores or memory arises from specific components of your website, rather than the entire setup. In such instances, consider horizontally scaling your infrastructure. For example, if your PHP processing is fast but database queries are slow, move your database to a separate server. If you’re serving a large volume of media (images/videos), relocate them to an offsite location and deliver them via a CDN. The aim is to reduce the load on your web server by moving resource-intensive components to their dedicated environments. This is the setup we employ at ProgramMatek, and it has proven to be effective.
Only after exhausting these options should you consider load balancing as a potential solution.
Have you implemented load balancing for your WordPress site? Did you explore vertical or horizontal scaling before adopting load balancing? We’d love to hear about the challenges you faced in the comments below.