Search code examples
amazon-web-servicescodeignitersymfonytwigtwig-extension

Twig Template Cache on EC2(AWS Auto Scaling)


I am developing by applying Twig Template to CodeIgniter4.

The server environment is AWS EC2 and is auto-scaled.

This is the first development in this environment.
I heard that you should be careful about creating and referencing local files.
I understand this is because it runs on multiple instances.

So I was concerned about the Twig template cache file.

If the file does not exist, a cache is created.
And if the template contents are different, the cache is updated.
At first, I thought it was okay, but I'm not sure.

I'm not sure, but should I cache the template cache in Redis?
(I'm not sure about the Twig template cache settings for Redis, so please let me know if you know it.)

What are some best practices for template caching in an autoscale environment?


Solution

  • If you do not want to make any changes to your app you could take a look at mounting an EFS mount to all your autoscaled instances (this could be done as either lifecycle hook or part of the AMI used for the autoscaling.

    If you then create a symlink between all the EFS mount and the folder your twig cache is generated then all your servers would share the same file cache.