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

Rem000's avatar

Horizon redis instance

Hi guys, i need some help with Laravel Horizon.

How can I access an Redis instance that uses Horizon?

In \vendor\laravel\horizon\src\HorizonServiceProvider.php i see this code:

/**
     * Register the custom queue connectors for Horizon.
     *
     * @return void
     */
    protected function registerQueueConnectors()
    {
        $this->app->resolving(QueueManager::class, function ($manager) {
            $manager->addConnector('redis', function () {
                return new RedisConnector($this->app['redis']);
            });
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        if (! defined('HORIZON_PATH')) {
            define('HORIZON_PATH', realpath(__DIR__.'/../'));
        }

        $this->app->bind(Console\WorkCommand::class, function ($app) {
            return new Console\WorkCommand($app['queue.worker'], $app['cache.store']);
        });

        $this->configure();
        $this->offerPublishing();
        $this->registerServices();
        $this->registerCommands();
        $this->registerQueueConnectors();
    }

and \vendor\laravel\horizon\src\Connectors\RedisConnector.php

use Illuminate\Queue\Connectors\RedisConnector as BaseConnector;
use Illuminate\Support\Arr;
use Laravel\Horizon\RedisQueue;

class RedisConnector extends BaseConnector
{
    /**
     * Establish a queue connection.
     *
     * @param  array  $config
     * @return \Laravel\Horizon\RedisQueue
     */
    public function connect(array $config)
    {
        return new RedisQueue(
            $this->redis, $config['queue'],
            Arr::get($config, 'connection', $this->connection),
            Arr::get($config, 'retry_after', 60),
            Arr::get($config, 'block_for', null)
        );
    }
}

but unfortunately I couldn’t get the instance from there...

Ultimately, I want to use standard methods \php\lib\php.jar!\stubs\redis\Redis.php, such as 'exists', 'hGet', 'hGetAll' and so on with connection that uses Horizon.

0 likes
8 replies
bobbybouwmann's avatar

Well, Redis probably runs on the same server. It depends on the config you have set up for Redis inside your .env.

Connection to a Redis instance on a server can be done using the redis-cli

$ redis-cli

If you have set a password or it's running on a different server you can connect to it like so

redis-cli -h 127.0.0.1 -p 6379 -a 'thisizmy!PASS' 

-h is for the IP address -p is for the port -a is for the password of the Redis instance

Once you're in you can perform all Redis commands like FLUSHALL and FLUSHDB

Rem000's avatar

It's all clear, but i need some manipulation with data in my controllers.

I'm use phpredis with igbinary ext.

And main problem is as follows:

dd(Redis::info('keyspace'))
array (
  'db0' => 'keys=27,expires=13,avg_ttl=154751004',
)

dd(Redis::exists('horizon:masters'))
0

in cli key exist:

www-data@web-app5-test:~/$ redis-cli -p 6379 
127.0.0.1:6379> KEYS '*'
 1) "horizon:snapshot:queue:check"
 2) "horizon:metrics:snapshot"
 3) "horizon:masters"
 4) "horizon:supervisor:web-app5-test-aZHe:supervisor-1"
 5) "horizon:failed_jobs"
 6) "horizon:snapshot:job:App\Jobs\PayBountyToUser"
 7) "horizon:last_snapshot_at"
 8) "horizon:job_id"
 9) "horizon:27"
10) "horizon:25"
11) "horizon:measured_jobs"
12) "horizon:29"
13) "horizon:snapshot:queue:fns"
14) "horizon:failed:check"
15) "horizon:snapshot:queue:pay"
16) "horizon:master:web-app5-test-aZHe"
17) "horizon:failed:checkReceipt:1"
18) "horizon:supervisor:web-app5-test-aZHe:supervisor-2"
19) "horizon:snapshot:job:App\Jobs\GetReceiptFromFNS"
20) "horizon:failed:checkReceipt:3"
21) "horizon:snapshot:job:App\Jobs\CheckUserByReceipt"
22) "horizon:recent_failed_jobs"
23) "horizon:supervisors"
24) "horizon:supervisor:web-app5-test-aZHe:supervisor-3"
25) "horizon:failed:checkReceipt:2"
26) "horizon:measured_queues"
27) "horizon:monitor:time-to-clear"
127.0.0.1:6379>

how it's possible?

Rem000's avatar

Number of keys matches when accessing through facade Redis:: and through redis-cli.

Default DB for facade is 0. Is't it?

I tried to use the command Redis::select(0) statically, but that did't help.

bobbybouwmann's avatar

The default database is converted in config/database.php at the bottom in the redis section.

However, if you can see the same amount of keys should be able to get the data as well.

Rem000's avatar

Bobby, thank you for showing interest in the issue, but you are saying obvious things. :)

I've next configurations:

config/database.php

'redis' => [

        'client' => 'phpredis',

        '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' => 6379,
            'database' => 0,
        ],

        'queues' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => 6379,
            'database' => 0,

            //'persistent' => env('REDIS_PERSISTENT', false),
            //'read_timeout' => 60,
            //'timeout' => 5
        ],

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

    ],

config/queue.php

'default' => 'redis',

'connections' => [

        /*            */

        'redis' => [
            'driver' => 'redis',
            'connection' => 'queues',
            'queue' => 'default',
            'retry_after' => 75,
            'block_for' => 5,
        ],

    ],

config/horizon.php

'use' => 'queues',

'environments' => [
        'production' => [
            'supervisor-1' => [
                'connection' => 'redis',
                'queue' => ['fns'],
                'balance' => 'simple',
                'processes' => 32,
                'tries' => 3,
            ],
            'supervisor-2' => [
                'connection' => 'redis',
                'queue' => ['check'],
                'balance' => 'auto',
                'processes' => 32,
                'tries' => 2,
            ],
            'supervisor-3' => [
                'connection' => 'redis',
                'queue' => ['pay'],
                'balance' => 'simple',
                'processes' => 32,
                'tries' => 1,
            ],
        ],

        'local' => [
            'supervisor-1' => [
                'connection' => 'redis',
                'queue' => ['default'],
                'balance' => 'simple',
                'processes' => 3,
                'tries' => 1,
            ],
        ],
    ],

config/app.php

'aliases' => [

    /*             */

        'RedisManager' => Illuminate\Support\Facades\Redis::class,
    
    /*             */

    ],

Any idea why Redis facade does not see the key??

steekam's avatar

@rem000 If you are still looking for a solution, this might be it. Specify horizon in the connection function. If you test fetching all keys, it will return all horizon specific keys. I also think any key-related query is prefixed with the horizon redis prefix by default. I am still unsure of how this works since I can't find a "horizon" connection specified in any config.


Redis::connection("horizon")->keys("*");

1 like

Please or to participate in this conversation.