symfonycachingredisstatelesshorizontal-scaling

Remove the Symfony cache folder - for stateless horizontal scaling


I wonder if it's possible to save all cache data of an Symfony application in a Redis container like AWS ElastiCache.

I read about the RedisAdapter you can choose in the framework:

cache:    
     app: cache.adapter.redis
     default_redis_provider: "redis://%env(REDIS_HOST)%:%env(REDIS_PORT)%"

That work if you want to save and read in/from the Redis cache.

But the rest of the data of Symfony is stored into the cache directory anyway. Is there a possibility to make Symfony complete stateless?


Solution

  • You can change the system cache in the same configuration:

    framework:
        cache:
            app: cache.adapter.redis
            system: cache.adapter.redis
            default_redis_provider: "redis://%env(REDIS_HOST)%:%env(REDIS_PORT)%"
    

    This should store all framework-related caches, like translations, templates, compiled annotations, etc. in Redis. You might also have to change other app-related caches like Doctrine and other 3rd party tools.

    You might also want to look at the Chain Adapter which saves all entries in all chained adapters, but fetches them one by one going from first to last, making it a good way to provide a fallback, in case your Redis cluster is down or when you experience some downtime. If you use a Docker based setup, you should be able to warm up all the caches during build time and then Symfony should not need to write any new caches, making it easy to spin up new instances, even when you don't use the Redis cache or use it in a chain.