<p>That error means that Redis can't store anything else because it is out of memory.</p>
<p>You can either:</p>
<ol>
<li><p>Bump the memory available to that Redis instance</p>
</li>
<li><p>Change the Redis cache invalidation strategy to something like <a href="https://redis.io/docs/reference/eviction/" rel="nofollow noreferrer">allkeys-lru</a>. You're probably using the default <code>volatile-lru</code>, which is a pretty conservative strategy.</p>
</li>
</ol>
<p>Good read here, too: <a href="https://ma.ttias.be/redis-oom-command-not-allowed-used-memory-maxmemory/" rel="nofollow noreferrer">https://ma.ttias.be/redis-oom-command-not-allowed-used-memory-maxmemory/</a></p>

CC BY-SA 4.0
2023-02-20T17:56:01.100Z
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
CC BY-SA 4.0
2023-02-21T16:40:52.340Z
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
CC BY-SA 4.0
2023-02-20T17:48:09.007Z
2023-02-20T17:48:09.007Z
Brad Bell
<p>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 <a href="https://nystudio107.com/blog/the-craft-cache-tag-in-depth" rel="nofollow noreferrer">Andrew's blog post</a>. Everything seemed to be working well until the other day we had a massive spike in errors showing:</p>
<pre><code>Uncaught exception 'yii\db\Exception' with message 'Redis error: OOM command not allowed when used memory &gt; 'maxmemory'.
</code></pre>
<p>Using the Craft commands to flush caches seemed to have no effect but doing a <code>redis-cli -h redis flushall</code> brought everything back to normal.</p>
<p>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?</p>
<p>app.php</p>
<pre><code>return [
    '*' =&gt; [
        'id' =&gt; App::env('CRAFT_APP_ID') ?: 'CraftCMS',
        'modules' =&gt; [
            'my-module' =&gt; \modules\Module::class,
        ],
        //'bootstrap' =&gt; ['my-module'],
    ],
    'production' =&gt; [
       'components' =&gt; [
          'mutex' =&gt; [
              'mutex' =&gt; [
                  'class' =&gt; yii\redis\Mutex::class,
                  'redis' =&gt; [
                      'hostname' =&gt; App::env('REDIS_HOSTNAME') ?: 'localhost',
                      'port' =&gt; 6379,
                      'password' =&gt; App::env('REDIS_PASSWORD') ?: null,
                      'database' =&gt; 0,
                  ],
              ],
          ],
      ],
    ]
];
</code></pre>
<p>app.web.php</p>
<pre><code>return [
    '*' =&gt; [
       //
    ],
    'production' =&gt; [
        'components' =&gt; [
            'session' =&gt; 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' =&gt; App::env('REDIS_HOSTNAME') ?: 'localhost',
                    'port' =&gt; 6379,
                    'password' =&gt; App::env('REDIS_PASSWORD') ?: null,
                    'database' =&gt; 1,
                ];

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

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

                return Craft::createObject($config);
            },
        ]
    ],
];
</code></pre>
<p>general.php</p>
<pre><code>return [
    '*' =&gt; [
        // ...

        'headlessMode' =&gt; true,

        'enableGql' =&gt; true,

        'enableGraphqlCaching' =&gt; true,

    ],
];
</code></pre>

<cache><redis>
Redis setup for a Craft 4 site - error OOM command not allowed when used memory > 'maxmemory'
# Redis setup for a Craft 4 site - error OOM command not allowed when used memory > 'maxmemory'

(Asked by: username_0 on 2023-02-19)

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,

    ],
];

**Tags:** cache, redis


---

## 1 answers

### Answer by username_1 on 2023-02-20. Score: 1 | Accepted answer

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](https://redis.io/docs/reference/eviction/). 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/>

> **Comments:**
> **username_0**: 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.  
> **username_1**: 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.  

---

