Blog Detail


Now Dynamically Create and Destroy Servers in Laravel cover image

Now Dynamically Create and Destroy Servers in Laravel

Laravel Dynamic Servers is an awesome package that can help you start and stop servers when you need them. The prime use case is to spin up extra working servers that can help you process the workload on queues.

You can think of this as a sort of PHP-based version of Kubernetes that has 5% of its features but covers that 80% use case. For most PHP and Laravel developers, this package will also be easier to learn and use.

Typically, on your hosting provider, you would prepare a server snapshot, that will be used as a template when starting new servers.

After the package is configured, spinning up an extra server is as easy as:

// typically, in a service provider

use Laravel\Horizon\WaitTimeCalculator;
use Spatie\DynamicServers\Facades\DynamicServers;
use Spatie\DynamicServers\Support\DynamicServersManager;

 * The package will call the closure passed 
 * to `determineServerCount` every minute
DynamicServers::determineServerCount(function(DynamicServersManager $servers) {
    * First, we'll calculate the number of servers needed. 
    * In this example, we will take a look at Horizon's 
    * reported waiting time. Of course, in your project you can 
    * calculate the number of servers needed however you want.    
    $waitTimeInMinutes = app(WaitTimeCalculator::class)->calculate('default');
    $numberOfServersNeeded = round($waitTimeInMinutes / 10);

    * Next, we will pass the number of servers needed to the `ensure` method.
    * If there currently are less that that number of servers available,
    * the package will start new ones.
    * If there are currently more than that number of servers running,
    *  the package will stop a few servers.

Installation & setup

You can install the package via composer:

composer require spatie/laravel-dynamic-servers

Run the installer

Next, you can run the installer with:

php artisan dynamic-servers:install

This command will:

publish the migration that will create a dynamic_servers table in your database. This table is used to keep track of all servers created by the package. create the dynamic-servers.php config file in the /config directory of your.

This package will keep track of all dynamic servers in the dynamic_servers table. To create that table, run these commands:

php artisan migrate

Scheduling commands

You should register a couple of commands in your kernel schedule.

The MonitorDynamicServersCommand command will take care of creating and destroying servers.

The HandleHangingServersCommand command will detect any servers that are starting and stopping, but never did start or stop completely.

To clean up records of stopped servers in the dynamic_servers table, you should add Laravel's model:prune command. If you already have this command in your schedule, add the \Spatie\DynamicServers\Models\Server::class model to its options.

You should add the commands to your schedule, and let them run every minute.

// in app/Console/Kernel.php
use Spatie\DynamicServers\Commands\MonitorDynamicServersCommand;
use Spatie\DynamicServers\Commands\DetectHangingServersCommand;
use Spatie\DynamicServers\Models\Server;

protected function schedule(Schedule $schedule)
    $schedule->command('model:prune', [
        '--model' => [Server::class],

Above is the basic introduction of this package, For more details, you can visit its complete documentation here.

Published at : 06-10-2022

Author : Rizwan Aslam
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch project