1

I have set up Craft 4 to use Redis for all its cache (due to hosting requirements) I have worked my way through the docs and some SE posts plus Andrew's blog post. Everything seemed to be working well until the other day we had a massive spike in errors showing:

Uncaught exception 'yii\db\Exception' with message 'Redis error: OOM command not allowed when used memory > 'maxmemory'.

Using the Craft commands to flush caches seemed to have no effect but doing a redis-cli -h redis flushall brought everything back to normal.

We are using the site headless and caching graphql queries, it does get a decent amount of traffic. Is there something I have missed in my configs?

app.php

return [
    '*' => [
        'id' => App::env('CRAFT_APP_ID') ?: 'CraftCMS',
        'modules' => [
            'my-module' => \modules\Module::class,
        ],
        //'bootstrap' => ['my-module'],
    ],
    'production' => [
       'components' => [
          'mutex' => [
              'mutex' => [
                  'class' => yii\redis\Mutex::class,
                  'redis' => [
                      'hostname' => App::env('REDIS_HOSTNAME') ?: 'localhost',
                      'port' => 6379,
                      'password' => App::env('REDIS_PASSWORD') ?: null,
                      'database' => 0,
                  ],
              ],
          ],
      ],
    ]
];

app.web.php

return [
    '*' => [
       //
    ],
    'production' => [
        'components' => [
            'session' => function() {
                // Get the default component config:
                $config = craft\helpers\App::sessionConfig();

                // Replace component class:
                $config['class'] = yii\redis\Session::class;

                // Define additional properties:
                $config['redis'] = [
                    'hostname' => App::env('REDIS_HOSTNAME') ?: 'localhost',
                    'port' => 6379,
                    'password' => App::env('REDIS_PASSWORD') ?: null,
                    'database' => 1,
                ];

                // Return the initialized component:
                return Craft::createObject($config);
            },

            'cache' => function() {
                $config = [
                    'class' => yii\redis\Cache::class,
                    'redis' => [
                        'hostname' => App::env('REDIS_HOSTNAME') ?: 'localhost',
                        'port' => 6379,
                        'password' => App::env('REDIS_PASSWORD') ?: null,
                        'database' => 2,
                    ],
                ];

                return Craft::createObject($config);
            },
        ]
    ],
];

general.php

return [
    '*' => [
        // ...

        'headlessMode' => true,

        'enableGql' => true,

        'enableGraphqlCaching' => true,

    ],
];

1 Answer 1

1

That error means that Redis can't store anything else because it is out of memory.

You can either:

  1. Bump the memory available to that Redis instance

  2. Change the Redis cache invalidation strategy to something like allkeys-lru. You're probably using the default volatile-lru, which is a pretty conservative strategy.

Good read here, too: https://ma.ttias.be/redis-oom-command-not-allowed-used-memory-maxmemory/

2
  • Thank you Brad, so would I be right in thinking that there isn't anything else I could set in the Craft configs, maybe like cache duration that would help? In other words this is purely a dev-ops/infrastructure issue.
    – Gareth
    Commented Feb 20, 2023 at 17:56
  • It'd be the best fix, I think. Even if you decrease the TTL for the items in Redis on the Craft/PHP side, so they expire faster, they could fill back up just as quickly as they expire, depending on the site and traffic patterns.
    – Brad Bell
    Commented Feb 21, 2023 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.