Anil Kumar
Anil Kumar

Reputation: 488

Invalidate Varnish cache on multiple instances

We are running our Magento store on AWS and we have autoscaling setup behind an ELB. At any time, we have at least 2 web servers running. Each web-server instance has it's own Varnish Cache server in front of it handling incoming requests.

Now, the problem is once we change some static content like CSS, Images etc., we have to SSH into each frontend EC2 Instance and ban the cached objects from Varnish. This is really troublesome as we first have to get the IPs of the instances manually from the AWS Console and then SSH into them which takes a lot of time and not very efficient if we have more than 2-3 servers at any time.

I looked into installing PageCache extension by Phoenix Media but the problem with their module is that we still have to manually enter a static list of IPs of varnish instances in the configuration which we don't know beforehand. This won't work in our case because the autoscaling servers come and go.

Is there a way that I can setup cache invalidation for all the running frontend servers at once? I have added the required piece of code in default VCL file which allows purging/ban from any instance in the same subnet. For all admin tasks, we have a dedicated EC2 and I plan to use the same to issue cache invalidation requests to all the frontend servers.

I was thinking, may be I could write a shell script and get the list of IPs of all frontend instances using AWS API and issue the ban command to all of them. Will this work? How to get it setup?

Also, can something be done to automate the cache invalidation of all the files which got changed during the latest code pull from repository?

What would be the most efficient way to work around this?

Upvotes: 2

Views: 1472

Answers (1)

slayedbylucifer
slayedbylucifer

Reputation: 23512

This is how I would do:

  1. Get managed nodes of the elb (describe-load-balancers) and pull the Instance IDs of those nodes
  2. Save above Instances IDs in an array
  3. Loop over the array of instance IDs and pull the Private Ip address of instances.
  4. Save these private IP addresses in another array
  5. Loop over this array and then SSH into these instances and execute your cache invalidation script/commands.

I am providing below a brief way of pulling the Private IP address of an ELB managed nodes. This is only to make you familiar with the particular AWS APIs that you should be working with:

I am using AWS CLI for this purpose:

Run below command to pull Instance IDs:

# aws elb describe-load-balancers --load-balancer-names my-elb | jq '.LoadBalancerDescriptions[].Instances[].InstanceId'
"i-12345678"
"i-87654321"

Once you have the instance IDs handy, run below command to pull the Private IP address of Individual Instances:

# aws ec2 describe-instances --instance-ids i-12345678 i-87654321 | jq '.Reservations[].Instances[].PrivateIpAddress'
"10.0.1.1"
"10.0.1.2"

Now you have the IP address of the ELB managed Nodes. So, loop over this list, ssh into the ode and run your varnish commands.

You need to do a bit of scripting here. So write a script and post another question in case you face any issues.

The AWS APIs that I used above are describe-load-balancers and describe-instances. AND I am using jq json parser to pull only the information that I want.

Upvotes: 4

Related Questions