Symfony Caching
Symfony’s application cache (cache.app) uses the filesystem by default. As explained in the Storage documentation, the filesystem is readonly on AWS Lambda except for /tmp, so that is where the cache is written.
However, the /tmp directory isn’t shared across Lambda instances. If your Lambda function scales up or is redeployed, the cache will be empty in new instances.
If you want the cache to be shared across all Lambda instances, for example if your application caches a lot of data or if you use it for locking mechanisms (like API rate limiting), you can instead use the database, DynamoDB or Redis.
The database is the simplest option if your application already uses one. DynamoDB is “pay per use”. Redis (ElastiCache) requires a VPC and is billed per hour even when idle, but offers lower latency.
Database Cache
If your application uses Doctrine, store the application cache in the database with the cache.adapter.doctrine_dbal adapter:
framework:
cache:
app: cache.adapter.doctrine_dbalThe adapter uses Doctrine’s default connection and stores items in a cache_items table, which make:migration adds to your Doctrine migrations. Without Doctrine, use the cache.adapter.pdo adapter, see the Symfony documentation .
DynamoDB Cache
A Symfony bundle is available to use AWS DynamoDB as a cache store: rikudou/psr6-dynamo-db-bundle . Install the bundle with:
composer require rikudou/psr6-dynamo-db-bundleThe Symfony Flex recipe enables the bundle in the prod environment only, with replace_default_adapter: true. Services that are autowired with a cache interface (CacheInterface, AdapterInterface, CacheItemPoolInterface) then use DynamoDB, and so do sessions, as long as framework.session.handler_id is not set in config/packages/framework.yaml. Cache pools that use cache.app directly, like the rate limiter, still use the filesystem.
Next, deploy the DynamoDB table and allow your functions to access it. The bundle reads the table name from the DYNAMODB_CACHE_TABLE environment variable: set it in serverless.yml, as shown in that section.