Laravel Horizon creates lots of redis client connections

283 Views Asked by At

I'm using Laravel Horizon on a single-node Redis instance. I need to process hundreds if not thousands of jobs per minute. Right now, I have a standalone Redis instance containing 72,000 keys, memory usage of 313 total memory and 2,652 connected clients, I'll attach my horizon config.

When developing locally, despite slimming down my horizon config I'm still getting 60 clients connected through my set up and wondered how I can reduce this down, we're considering a multi-node redis cluster but not sure if this will work, here's my horizon config:

<?php

use Illuminate\Support\Str;

return [

    /*
    |--------------------------------------------------------------------------
    | Horizon Domain
    |--------------------------------------------------------------------------
    |
    | This is the subdomain where Horizon will be accessible from. If this
    | setting is null, Horizon will reside under the same domain as the
    | application. Otherwise, this value will serve as the subdomain.
    |
    */

    'domain' => env('HORIZON_DOMAIN'),

    /*
    |--------------------------------------------------------------------------
    | Horizon Path
    |--------------------------------------------------------------------------
    |
    | This is the URI path where Horizon will be accessible from. Feel free
    | to change this path to anything you like. Note that the URI will not
    | affect the paths of its internal API that aren't exposed to users.
    |
    */

    'path' => env('HORIZON_PATH', 'horizon'),

    /*
    |--------------------------------------------------------------------------
    | Horizon Redis Connection
    |--------------------------------------------------------------------------
    |
    | This is the name of the Redis connection where Horizon will store the
    | meta information required for it to function. It includes the list
    | of supervisors, failed jobs, job metrics, and other information.
    |
    */

    'use' => 'default',

    /*
    |--------------------------------------------------------------------------
    | Horizon Redis Prefix
    |--------------------------------------------------------------------------
    |
    | This prefix will be used when storing all Horizon data in Redis. You
    | may modify the prefix when you are running multiple installations
    | of Horizon on the same server so that they don't have problems.
    |
    */

    'prefix' => env(
        'HORIZON_PREFIX',
        Str::slug(env('APP_NAME', 'laravel'), '_').'_horizon:'
    ),

    /*
    |--------------------------------------------------------------------------
    | Horizon Route Middleware
    |--------------------------------------------------------------------------
    |
    | These middleware will get attached onto each Horizon route, giving you
    | the chance to add your own middleware to this list or change any of
    | the existing middleware. Or, you can simply stick with this list.
    |
    */

    'middleware' => ['web'],

    /*
    |--------------------------------------------------------------------------
    | Queue Wait Time Thresholds
    |--------------------------------------------------------------------------
    |
    | This option allows you to configure when the LongWaitDetected event
    | will be fired. Every connection / queue combination may have its
    | own, unique threshold (in seconds) before this event is fired.
    |
    */

    'waits' => [
        'redis:default' => 60,
    ],

    /*
    |--------------------------------------------------------------------------
    | Job Trimming Times
    |--------------------------------------------------------------------------
    |
    | Here you can configure for how long (in minutes) you desire Horizon to
    | persist the recent and failed jobs. Typically, recent jobs are kept
    | for one hour while all failed jobs are stored for an entire week.
    |
    */

    'trim' => [
        'recent' => 60,
        'pending' => 60,
        'completed' => 60,
        'recent_failed' => 360,
        'failed' => 360,
        'monitored' => 360,
    ],

    /*
    |--------------------------------------------------------------------------
    | Silenced Jobs
    |--------------------------------------------------------------------------
    |
    | Silencing a job will instruct Horizon to not place the job in the list
    | of completed jobs within the Horizon dashboard. This setting may be
    | used to fully remove any noisy jobs from the completed jobs list.
    |
    */

    'silenced' => [
        App\Jobs\ProcessAnalytic::class,
        App\Jobs\StoreApiRequestLog::class,
        App\Jobs\ProcessModelObserver::class,
    ],

    /*
    |--------------------------------------------------------------------------
    | Metrics
    |--------------------------------------------------------------------------
    |
    | Here you can configure how many snapshots should be kept to display in
    | the metrics graph. This will get used in combination with Horizon's
    | `horizon:snapshot` schedule to define how long to retain metrics.
    |
    */

    'metrics' => [
        'trim_snapshots' => [
            'job' => 72,
            'queue' => 72,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Fast Termination
    |--------------------------------------------------------------------------
    |
    | When this option is enabled, Horizon's "terminate" command will not
    | wait on all of the workers to terminate unless the --wait option
    | is provided. Fast termination can shorten deployment delay by
    | allowing a new instance of Horizon to start while the last
    | instance will continue to terminate each of its workers.
    |
    */

    'fast_termination' => false,

    /*
    |--------------------------------------------------------------------------
    | Memory Limit (MB)
    |--------------------------------------------------------------------------
    |
    | This value describes the maximum amount of memory the Horizon master
    | supervisor may consume before it is terminated and restarted. For
    | configuring these limits on your workers, see the next section.
    |
    */

    'memory_limit' => 512,

    /*
    |--------------------------------------------------------------------------
    | Queue Worker Configuration
    |--------------------------------------------------------------------------
    |
    | Here you may define the queue worker settings used by your application
    | in all environments. These supervisors and settings handle all your
    | queued jobs and will be provisioned by Horizon during deployment.
    |
    */

    'defaults' => [

        /**
         * Fast jobs
         *
         * Jobs on this queue are optimised to run as quickly as possible.
         */
        'supervisor-fast-jobs' => [
            'connection' => 'redis-short-running',
            'queue' => ['on-demand-runs-now', 'redirects', 'commissions', 'statistics', 'listeners', 'observers', 'notifications', 'redis-short-running'],
            'balance' => 'auto',
            'maxProcesses' => 1,
            'maxTime' => 60,
            'maxJobs' => 0,
            'memory' => 128,
            'tries' => 1,
            'timeout' => 40,
            'nice' => 0,
        ],

        /**
         * Regular jobs
         *
         * Jobs on this queue should be default.
         */
        'supervisor-regular-jobs' => [
            'connection' => 'redis',
            'queue' => ['on-demand-runs-now', 'logs', 'analytics', 'csv-exports', 'default'],
            'balance' => 'auto',
            'maxProcesses' => 1,
            'maxTime' => 180,
            'maxJobs' => 0,
            'memory' => 128,
            'tries' => 1,
            'timeout' => 120,
            'nice' => 0,
        ],

        /**
         * Slow jobs
         *
         * Jobs on this queue are allowed to run for a longer period of time.
         */
        'supervisor-slow-jobs' => [
            'connection' => 'redis-long-running',
            'queue' => ['on-demand-runs-now', 'applications', 'pingtrees', 'redis-long-running'],
            'balance' => 'auto',
            'maxProcesses' => 1,
            'maxTime' => 700,
            'maxJobs' => 0,
            'memory' => 512,
            'tries' => 1,
            'timeout' => 570,
            'nice' => 0,
        ],
    ],

    'environments' => [
        'production' => [
            'supervisor-fast-jobs' => [
                'minProcesses' => 10,
                'maxProcesses' => 200,
                'balanceMaxShift' => 1,
                'balanceCooldown' => 3,
            ],
            'supervisor-regular-jobs' => [
                'minProcesses' => 10,
                'maxProcesses' => 100,
                'balanceMaxShift' => 1,
                'balanceCooldown' => 3,
            ],
            'supervisor-slow-jobs' => [
                'minProcesses' => 50,
                'maxProcesses' => 350,
                'balanceMaxShift' => 1,
                'balanceCooldown' => 3,
            ],
        ],

        'local' => [
            'supervisor-fast-jobs' => [
                'minProcesses' => 1,
                'maxProcesses' => 6,
                'balanceMaxShift' => 1,
                'balanceCooldown' => 3,
            ],
            'supervisor-regular-jobs' => [
                'minProcesses' => 1,
                'maxProcesses' => 6,
                'balanceMaxShift' => 1,
                'balanceCooldown' => 3,
            ],
            'supervisor-slow-jobs' => [
                'minProcesses' => 1,
                'maxProcesses' => 6,
                'balanceMaxShift' => 1,
                'balanceCooldown' => 3,
            ],
        ],
    ],
];

And for reference, here's my queue connections in my database file:

'connections' => [

        'sync' => [
            'driver' => 'sync',
        ],

        'database' => [
            'driver' => 'database',
            'table' => 'jobs',
            'queue' => 'default',
            'retry_after' => 600,
            'after_commit' => false,
        ],

        'beanstalkd' => [
            'driver' => 'beanstalkd',
            'host' => 'localhost',
            'queue' => 'default',
            'retry_after' => 90,
            'block_for' => 0,
            'after_commit' => false,
        ],

        'sqs' => [
            'driver' => 'sqs',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'prefix' => env('SQS_PREFIX', 'https://sqs.us-east-1.amazonaws.com/your-account-id'),
            'queue' => env('SQS_QUEUE', 'default'),
            'suffix' => env('SQS_SUFFIX'),
            'region' => env('AWS_DEFAULT_REGION', 'us-east-1'),
            'after_commit' => false,
        ],

        'redis-short-running' => [
            'driver' => 'redis',
            'connection' => 'default',
            'host'=>'redis',
            'queue' => env('REDIS_QUEUE', 'redis-short-running'),
            'retry_after' => 70,
            'block_for' => null,
            'after_commit' => true,
        ],

        'redis' => [
            'driver' => 'redis',
            'connection' => 'default',
            'host'=>'redis',
            'queue' => env('REDIS_QUEUE', 'default'),
            'retry_after' => 90,
            'block_for' => null,
            'after_commit' => true,
        ],

        'redis-long-running' => [
            'driver' => 'redis',
            'connection' => 'default',
            'host'=>'redis',
            'queue' => env('REDIS_QUEUE', 'redis-long-running'),
            'retry_after' => 600,
            'block_for' => null,
            'after_commit' => true,
        ],

    ],

What can I do to get connections down?

0

There are 0 best solutions below