Skip to content

Performance issues in production environments #22

Description

@nikita-stena

Hello! In the RedisCacheEngine class's clear method, line 162 uses the keys method to retrieve all keys by mask. Keys isn't performant; you should use ->scan to search by mask.
The main reasons against Keys are:

  1. It locks Redis for the entire execution time.
  2. It's O(N) complex - the more keys, the longer the lock.
  3. With large databases, it can lead to timeouts and errors.
  4. It's a production anti-pattern.
    /**
     * @throws NotFoundExceptionInterface
     * @throws InvalidArgumentException
     * @throws RedisException
     * @throws ContainerExceptionInterface
     */
    #[\Override]
    public function clear(): bool
    {
        $keys = $this->redis->keys('cache:*');
        foreach ($keys as $key) {
            if (preg_match('/^cache:(?<key>.*)/', $key, $matches)) {
                $this->delete($matches['key']);
            }
        }
        return true;
    }

Activity

  1. byjg commented on Dec 20, 2025

    @byjg
    Owner

    You are correct. Redis Keys method is blocking, O(N) and should not be used in production.

    Fix is in #23

  2. byjg commented on Feb 22, 2026

    @byjg
    Owner

    Release 6.0.1 created.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions