Ecommerce — Auto-scaling solution for Magento backends

Lê Cao Hoàng
Edumall Engineering
3 min readJun 28, 2019
High Level Design about Auto-Scaling Backend feature for Ecommerce Magento

Traffic flow from end-user

End-user (80/443) => CloudFlare (abc .com) => (80/443) Proxy NGINX=> (port 6081) Varnish => (8080) Web Server backend

  1. Problem
  • During development phase, we have investigated about Ecommerce Production Auto-scaling feature for Magento backend without any positive result. The main trouble was ELB — Varnish stack can not work completely due to 502 error (occurred sometime).
  • Varnish configuration customization is neccessary to avoid syntax error when registering backend from auto-scaling group.
  • Varnish configuration need Magento backend Private IP to register into default.vcl
  • Varnish configuration does not accept node name with dot (.) but accept minus (-).

2. Solution

Infrastructure team choose Scripting solution using Bash and AWS CLI to auto-detect and trigger action getting and registering EC2 (backend) instnce ID and Private IP.

Resource:

  • AWS IAM User Access andSecret Key: ecommerce-get-backend-private-ip-for-varnish
  • AWS CLI was installed on varnish servers.
  • Bash script was setup in Varnish cronjob (1 minute period)

get-backend-private-ip.sh

ecommerce-backend-update-script.sh

* * * * * sh /home/centos/varnish-backend-update/ecommerce-backend-update-script.sh

3. How it works ?

Block code was divided into 2 parts: Getting EC2 backend information and registering Varnish configuration.

3.1 Getting EC2 backend information

Using bash script get-backend-private-ip.sh to query EC2 information within auto-scaling group.

Note: ecommerce-auto-scaling-backend is name of backend auto-scaling group. This group uses Launch configuration with updated AMI from latest code version.

#!/bin/bash

## Getting backend EC2 instance ID and print out to data column

for i in `/usr/local/bin/aws autoscaling describe-auto-scaling-groups — auto-scaling-group-name ecommerce-auto-scaling-backend | grep -i instanceid | awk ‘{ print $2}’ | cut -d’,’ -f1| sed -e ‘s/”//g’`
do

## Getting Backend EC2 Private IP based on Instance ID
/usr/local/bin/aws ec2 describe-instances — instance-ids $i | grep -i PrivateIpAddress | awk ‘{ print $2 }’ | head -1 | cut -d”,” -f1 |sed ‘s/”//g’
done;

Output from the script will be saved into /home/centos/varnish-backend-update/ec2-instance-private-ip.txt for the next parts.

3.2 Varnish configuration Registration

3.2.1 Varnish folder structure

/etc/varnish/

| — default.vcl (backend node, traffic routing,…)

| — secret

| — varnish.param (port, cache…)

3.2.2 Varnish folder structure customization

Customize Varnish folder structure using directive “include” (similar to NGINX configuration). Because we have experienced the complicated structure when updating EC2 Private IP.

3.2.3 Update/Adding backend.vcl and init.vcl

Due to syntax policy, Varnish does not accept (dot) when registering node name. However, we can use minus (-) to resolve this minor syntax problem.

var1=$(cat /home/centos/varnish-backend-update/backend-node-name.txt)
set $var1
for i in `cat /home/centos/varnish-backend-update/ec2-instance-private-ip.txt`
do
#for j in `cat /home/centos/varnish-backend-update/backend-node-name.txt`
#do
printf “backend node-$1 {\n.host = \”$i\”;\n.port = \”8080\”;\n.connect_timeout = 300s;\n.first_byte_timeout = 300s;\n.between_bytes_timeout = 30s;\n.probe = {\n.url = \”/health_check.php\”;\n.timeout = 2s;\n.interval = 5s;\n.window = 10;\n.threshold = 5;\n}\n}\n” >> /etc/varnish/backend.vcl
shift
done;

for j in `cat /home/centos/varnish-backend-update/backend-node-name.txt`
do
echo “edumallcluster.add_backend(node-$j);” >> /etc/varnish/init.vcl
done;

3.2.4 Post-configuration

Finally, reloading varnish to get latest code version.

Note: Dont restart varnish to avoid 503 error (downtime website)

# Author: HoangLC — HoangLC@topica.edu.vn
# MIT license

--

--