Home > Software design >  Purge NGINX FastCGI cache across multiple AWS ECS Fargate tasks
Purge NGINX FastCGI cache across multiple AWS ECS Fargate tasks

Time:07-28

I have a PHP WordPress application hosted on AWS using ECS Fargate. The application consists of one ECS service tasks running multiple tasks for scale behind an ALB. Each task is using NGINX with FastCGI cache enabled, and whenever something is saved in the WordPress CMS/admin panel it's purging the NGINX cache using a WordPress plugin (NGINX Helper). I could write some custom PHP code for this also if needed.

The problem is that each ECS task has its own NGINX instance, so it's only purging the FastCGI cache for that specific ECS task. The WordPress application currently has a Dockerfile referencing a custom NGINX ECR repository, something like:

FROM xxxxxxxxxxxxx.dkr.ecr.us-east-1.amazonaws.com/my-nginx

I'm already also using CloudFront in my setup, but it didn't seem to cache the dynamic pages that often (got "Miss from cloudfront" most of the time except for simple pages and assets/images etc.), hence adding FastCGI cache.

  • Should I be able to rely on CloudFront alone when configured correctly for a high traffic PHP site, or is using NGINX FastCGI cache still a good idea here?
  • How can I purge the NGINX FastCGI cache across these multiple ECS tasks when saving in the WordPress CMS? There is already a /purge/ URL endpoint setup, so if I can trigger that for each running ECS task that would do it. Or I can write some custom PHP code that will purge the cache, so just need to trigger a PHP file on each running task. Can I solve this using the AWS PHP SDK or something like SQS?
  • Is it easier/recommended to use something like Cloudflare on top of everything instead?

Thanks!

CodePudding user response:

  • Should I be able to rely on CloudFront alone when configured correctly for a high traffic PHP site, or is using NGINX FastCGI cache still a good idea here?

No, CloudFront alone won't provide enough caching, as you have seen.

  • How can I purge the NGINX FastCGI cache across these multiple ECS tasks when saving in the WordPress CMS? There is already a /purge/ URL endpoint setup, so if I can trigger that for each running ECS task that would do it. Or I can write some custom PHP code that will purge the cache, so just need to trigger a PHP file on each running task. Can I solve this using the AWS PHP SDK or something like SQS?

What you need is a distributed caching mechanism, like Redis or Memcached, but I don't think FastCGI cache supports using something like that. You could look into writing some custom PHP code that sends a message to a SNS topic, and have all your servers listening on that topic (with some other custom PHP code) and trigger a cache reset when they get a message.

  • Is it easier/recommended to use something like Cloudflare on top of everything instead?

Cloudflare is absolutely better at caching WordPress sites than CloudFront, especially with APO. With APO Tiered Caching Cache Reserve enabled, you would see very few cache misses making it back to your WordPress server.

  • Related