Skip to content

SessionManager clones cache store, resulting in unnecessary redis connections. #58377

@TheLevti

Description

@TheLevti

Laravel Version

12.45.2

PHP Version

8.4.16

Database Driver & Version

latest phpredis with aws redis serverless 7.x

Description

We have tight infra and performance constraints and specific applications limit the amount of max open connections to redis to 1. Now thats not a problem, we just configured a single redis connection (default), and updated all config/ files to use the default redis connection when redis is used by any of the laravel components.

Now the problem appears specifically only with the Sessions component. It has code that for some unknown reason clones the store, which in turn clones the redis store. Like in many cases session is checked before anything else and that means that a session will clone a redis store that has no connection established yet. Then any other component that needs to use redis (e.g. cache), will also establish a new connection, because session manager took off with a clone. This is a waste of resources in high performance applications.

Can someone explain why we do the following? And if there is no real reason, can I propose a fix for it? Open for suggestions.

// SessionManager::createCacheHandler()
clone $this->container->make('cache')->store($store),

->

// Illuminate\Cache\Repository::__clone()
$this->store = clone $this->store

Image

Screenshot shows on the left the session being retrieved from redis and then we fail establishing a connection to redis (under sendRequest to get the http cache) even though we already have one.

Steps To Reproduce

Set redis.pconnect.connection_limit=1 in your redis.ini

Load any endpoint that reads/writes sessions and also does some other redis operations in the controller, like using the Cache component. Ensure session connection and cache connection are equal.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions