Craft CMS reference

Redis cache and session configuration and the background queue for Craft CMS on TurboStack.

Reference for running Craft CMS on TurboStack: how to point Craft's cache and sessions at the platform's Redis, and how to run the background queue. For setup see Deploy Craft CMS; for tuning see Craft CMS best practices.

File layout

Your Craft codebase lives under your system user's home directory. ~ is that home directory (for example /var/www/prod/).

Path What it is
~/public_html/ Web document root
~/craftcms/ The Craft codebase
~/craftcms/config/ Configuration files (general.php, app.php, project/)
~/craftcms/storage/ Runtime data, caches and logs

For ownership and permissions, see Application file layout and permissions.

Redis cache and sessions

TurboStack provisions Redis, but Craft only uses it once you point its cache and session components at it in config/app.php. Install the Redis driver first:

composer require yiisoft/yii2-redis

Then configure the components. This uses the cache instance for the data cache and the persistent instance for sessions, each on its own database index:

<?php
use craft\helpers\App;

return [
    'components' => [
        // Connection to the Redis cache instance (unix socket)
        'redis' => [
            'class' => yii\redis\Connection::class,
            'unixSocket' => '/var/run/redis/redis.sock',
            'database' => 0,
        ],
        'cache' => [
            'class' => yii\redis\Cache::class,
            'defaultDuration' => 86400,
            'keyPrefix' => 'craft',
        ],
        // Sessions on the persistent instance, with its own database index
        'session' => function() {
            return Craft::createObject([
                'class' => yii\redis\Session::class,
                'as session' => craft\behaviors\SessionBehavior::class,
                'redis' => [
                    'class' => yii\redis\Connection::class,
                    'unixSocket' => '/var/run/redis-persistent/redis.sock',
                    'database' => 1,
                ],
            ]);
        },
    ],
];
  • unixSocket replaces hostname and port; leave those out, because they are ignored when a socket path is set.
  • Give each component its own database index. Craft warns that sharing an index lets one component flush another's data.
  • The session component may also live in config/app.web.php, which Craft loads only for web requests. Either file works.

Background queue

Craft processes long tasks (image transforms, search indexing, emails) through a queue. By default it runs the queue on web requests, which adds load to PHP. On a busy site, turn that off and run the queue as a persistent worker instead.

Disable the web-triggered runner in config/general.php:

'runQueueAutomatically' => false,

Then run Craft's queue as a user system service so it restarts on failure and survives logout. Keep the number of workers within the host's processor budget (see Scale throughput with more instances).