Load Balancing

With NATS, it is possible to run your installation across multiple servers in order to reduce any strain that may arise from running your NATS install on a single server. To do this, you must purchase an extra license for each additional server.

There are many possible ways to set this up; here is an example:

In this example, SERVER1 will contain your Main install of NATS while SERVER2 will contain your replicated version of NATS.

  • The nats/templates_c and nats/logs directory should not be replicated from SERVER1 to SERVER2. Everything else in the nats/ directory should be replicated from SERVER1 to SERVER2.

  • If possible, have nats/logs on SERVER2 replicated to some location on SERVER1 (like nats/logs2). This makes it easier for TMM Techs to debug any issues that may arise without needing you to copy files every time. In this example, 'logs2' would be another directory that shouldn't be replicated to SERVER2.

  • NATS has functions that leverage PHP's Memcache and Memcached for caching, helping you speed up the performance of your sites by alleviating database load.

  • In addition to that, you can use Memcache and Memcached for session storage, storing information in the server's memory (RAM), as opposed to the database. This provides a faster way to retrieve a surfer's session information, even if they switch servers.

  • In this setup, both installs of NATS are connected to the same MySql database. The slave database(a replication of the master database) is used by NATS to handle some reporting and otherwise intensive lookups that may strain the database, leaving the master free to handle inserts.

  • It is important that there is some way to force the main server, so the admins can always get the master (and not the replicated server) to perform administrative functions. Usually, a special domain is set aside for just admins.

  • It is important that the NATs cron is set up on the master server and only the master server.

  • When we request access, we will need access to the master and not one of the replicated servers.

circle-info

Please Note: This method can be expanded by adding SERVER3, SERVER4, etc, with the same setup as SERVER2.

Additional Notes

The following tips can help your load-balanced setup run as smoothly as possible:

  • A database user will need permission to create temporary tables on both master and slave databases.

  • Make sure there is a sync in place to automatically push changes from the master to all other servers.

  • Exclude the logs and templates_c directories from the sync.

  • Make sure there is a way to manually trigger the sync for when you need to make changes.

  • Make sure there is one non-load balanced subdomain (for example, admin.sitename.com) that you can use.

  • Have subdirectories in the logs folder on the master for each other server you have set up, and have the logs from those servers copied over automatically.

After moving your servers to a load-balanced setup, you may need to update your config.php file to allow your new IP in order to correctly receive the traffic directed from your load-balanced servers. To do so, add the following code to the nats/config.php file on all of the servers you are using for your load-balancing setup:

Replace "123.123.123.12" in the above example with the IP for your new server. In the above code block, the HTTP_X_FORWARDED_FOR is the value that is being sent from your load balancer.

circle-info

Please Note: If your load balancer configuration changes for any reason, you must update this value so that NATS will know what to look for.

AWS Load Balancing

NATS supports specifying a CIDR address (ex. 10.0.2.0/24) in the LB_ALLOWED_IPS setting. When using AWS load balancers, the IP address of the load balancer can fluctuate between any IP address in your VPC range. So you must set LB_ALLOWED_IPS to the CIDR address range of your VPC or specify every individual address.

Last updated

Was this helpful?