Monday, 16 October 2017

Wordpress AWS Part 8 - 4000 Concurrent Connections

Wordpress AWS Part 8 - 4000 Concurrent Connections


From my personal observation, an EC2 instance (with 8GB RAM) can only support up to 1000 concurrent connections. We can do a number of things to make it support 4000 concurrent connections.

1. Install the "WP Super Cache" Wordpress plugin.
The installation of this plugin makes some changes to the "wp-config.php" file. After the installation of this plugin, we need to find out what changes it made, do the same changes in the local copy, and re-deploy as a newer version of the application.
For the curious, it adds these 2 lines in the "wp-config.php", near the begining of the file.
define('WP_CACHE', true); //Added by WP-Cache Manager
define( 'WPCACHEHOME', '/wpfiles/plugins/wp-super-cache/' ); //Added by WP-Cache Manager

2. Make RDS support more connections.
The smallest RDS database can support only up to 66 concurrent connections. We need to use "Paramater Groups" to adjust the "max_connections" to 5000. The idea is to create a "Parameter Group" that contains the parameter "max_connections" with the value "5000", then make the DB instance use the "Parameter Group" that we have created.

A parameter group...
... with the parameter that we want.
The parameter group is assigned to our database instance.

3. Configure Beanstalk to use "General Purpose (SSD)" for more IOPS.

"Beanstalk", the app environment, "Configuration", "Instances", select "General Purpose (SSD)".

4. Configure Beanstalk to keep all EC2 instances in the same zone.

5. Configure Beanstalk to have the same number of minumum and maximum number of EC2 instances.
Creating an EC2 instance takes time. If we know we are going to be hit by 4000 concurrent connections, we can "pre-create" the EC2 instances by setting the minumum the same as the maximum.
Pick only 1 zone.
Min instance count = Max instance count.

6. Increase PHP memory limit.
Increase PHP memory limit.

7. Use TCP network load balancer.
Beanstalk gives us a "Classic Load Balancer". A TCP network load balancer is better. It can be easily created in "Services", "EC2", "Load Balancers", "Create Load Balancer".
Create a TCP network load balancer.
After creating it, configure it to direct port 80 to all our EC2 instances.
Note the TCP network load balancer's public address, update the DNS records so that the Wordpress site points to our new TCP load balancer. Don't forget the security groups and inbound rules too.

8. Configure EC2 instances to be able to take more connections.
Due to ulimits and stuff, the EC2 instance can take up to 1000 connections, even when it has lots of free CPU and RAM. The default Apache configuration also limits the number of connections. These commands can be issued to the EC2 instance in order to make it take more connections.
sudo sysctl net.core.somaxconn=1000000
sudo ifconfig eth0 txqueuelen 10000
sudo sysctl net.core.netdev_max_backlog=10000
sudo sysctl net.ipv4.tcp_max_syn_backlog=8192

sudo vi /etc/httpd/conf.modules.d/00-mpm.conf
MinSpareServers 10
MaxSpareServers 20
MaxRequestWorkers 2500
ServerLimit       2500

sudo vi /etc/security/limits.conf
*       soft  nofile  20000
*       hard  nofile  20000

sudo service httpd restart
(These commands should be incorporated into the "ebextensions" eventually. Or else when Beanstalk re-creates the EC2 instance for whatever reason, the modifications will be lost.)

Part 1 - Overview
Part 2 - RDS
Part 3 - EFS
Part 4 - Key Pairs
Part 5 - Beanstalk
Part 6 - ebextensions Folder
Part 7 - Deploy Wordpress
Part 8 - 4000 Concurrent Connections