Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Afzal Raza's avatar

How to implement connection pooling with Redis in Laravel using Predis?

I'm using predis here is my configuration 'redis' => [

    'client' => env('REDIS_CLIENT', 'predis'),

    'options' => [
        'cluster' => env('REDIS_CLUSTER', 'redis'),
        'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
    ],

    'default' => [
        'url' => env('REDIS_URL'),
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', '6379'),
        'database' => env('REDIS_DB', '0'),
        'persistent' => true,
        'timeout' => 5,
        'read_timeout' => 60,
    ],

    'cache' => [
        'url' => env('REDIS_URL'),
        'host' => env('REDIS_HOST', '127.0.0.1'),
        'password' => env('REDIS_PASSWORD', null),
        'port' => env('REDIS_PORT', '6379'),
        'database' => env('REDIS_CACHE_DB', '1'),
    ],

],

it is not setting the idle time id=4 addr=127.0.0.1:55137 fd=9 name= age=750 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 argv-mem=0 obl=0 oll=0 omem=0 tot-mem=20552 events=r cmd=eval user=default id=15 addr=127.0.0.1:55172 fd=8 name= age=696 idle=688 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=0 argv-mem=0 obl=0 oll=0 omem=0 tot-mem=20504 events=r cmd=get user=default id=34 addr=127.0.0.1:56356 fd=10 name= age=0 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 argv-mem=10 obl=0 oll=0 omem=0 tot-mem=61466 events=r cmd=client user=default

0 likes
1 reply
LaryAI's avatar
Level 58

To implement connection pooling with Redis in Laravel using Predis, you need to ensure that your Redis connections are persistent. This can be achieved by setting the persistent option to true in your Redis configuration. From your provided configuration, it looks like you have already set the persistent option to true.

However, to ensure that the idle time is properly managed, you might need to adjust the timeout and read_timeout settings. Additionally, you can use the persistent_id option to group connections together, which can help with connection pooling.

Here is an updated version of your Redis configuration with some additional options:

Explanation:

  1. Persistent Connections: The persistent option is set to true to enable persistent connections.
  2. Persistent ID: The persistent_id option is added to group connections together. This helps in managing connection pools.
  3. Timeouts: The timeout and read_timeout options are set to ensure that connections do not hang indefinitely.

Additional Tips:

  • Redis Server Configuration: Ensure that your Redis server is configured to handle persistent connections and has appropriate settings for timeout and tcp-keepalive.
  • Monitoring: Use Redis monitoring tools to observe the behavior of your connections and adjust settings as needed.

By following these steps, you should be able to implement connection pooling with Redis in Laravel using Predis effectively.

Please or to participate in this conversation.