Sidecar — Deploy and execute AWS Lambda functions from your Laravel application.

Warming Functions

Hammerstone Refine has not launched yet. Sign up for the early access list to stay up to date!

The first request that comes in for every newly activated function will incur a "startup penalty" known as a "cold start." Cold starts are an AWS phenomenon whereby AWS is spinning up your container for the first time to handle the request. This process can take up to a second or two depending on the runtime.

If you function goes for some time without being called then Lambda will "freeze" it to save resources. When a new request comes in it will "unfreeze" that container for reuse. There is no telling when a container will be frozen, or when Lambda will decide to completely throw away a container and start a new one from scratch.

Much has been written about cold starts.

To help mitigate the effect of cold starts, Sidecar provides you with a warming mechanism to keep a certain number of containers booted and ready to go.

Warming Configuration

Sidecar ships with a WarmingConfig class to help you configure your desired warming configuration. By default every function returns an empty WarmingConfig, meaning that warming is disabled.

1class ExampleFunction extends LambdaFunction
2{
3 /**
4 * A warming configuration that can help mitigate against
5 * the Lambda "Cold Boot" problem.
6 *
7 * @return WarmingConfig
8 */
9 public function warmingConfig()
10 {
11 return new WarmingConfig;
12 }
13}

Instances

To enable warming, you must configure the number of instances you want warmed.

Every Lambda can only handle one request at a time. If multiple requests come in at the exact same time, AWS will spin up new containers to handle all of those requests. The number of instances you decide to warm depends on how busy you expect your function to be. You may have to play around with it!

If you wanted to warm 5 containers, you could call the static instances method on the WarmingConfig, or pass it in through the constructor.

1public function warmingConfig()
2{
3 // Using the static `instances`.
4 return WarmingConfig::instances(5);
5}
Code highlighting powered by torchlight.dev, a Hammerstone product.
1public function warmingConfig()
2{
3 // Using the constructor.
4 return new WarmingConfig(5);
5}

This instructs Sidecar that when it is time to warm, 5 requests should be sent simultaneously so that Lambda boots 5 containers.

Payload

Sidecar will send the number of request configured by the instances variable. Sidecar will also send along the payload defined by your warming configuration. By default, this config is just an array with a single warming key:

1[
2 'warming' => true
3]

You can then look for this payload in your handler and bail out early if you see it:

handler.js

1exports.handle = async function(event) {
2 // Bail early if it's a warming event.
3 if (event.warming) {
4 return;
5 }
6 
7 // Actual handling code...
8}

If you'd like to change the payload, you can update it via the payload method on the WarmingConfig class:

1public function warmingConfig()
2{
3 return WarmingConfig::instances(5)->payload([
4 'foo' => 'bar'
5 ]);
6}

Sidecar will now send along your custom payload instead of the default one.

Warming Command

To keep your containers warm after they have been deployed, you may add the sidecar:warm command to your Laravel schedule:

Kernel.php

1class Kernel extends ConsoleKernel
2{
3 /**
4 * Define the application's command schedule.
5 *
6 * @param \Illuminate\Console\Scheduling\Schedule $schedule
7 * @return void
8 */
9 protected function schedule(Schedule $schedule)
10 {
11 $schedule->command('sidecar:warm')->everyFiveMinutes();
12 }
13}

Warming Facade

If you want to warm your functions manually, you may use the facade: Sidecar::warm().

Pre-warming

You may use the command above to keep your functions after they are deployed, but Sidecar also gives you the ability to warm your functions before they are activated.

When you are activating your functions, you can add the --pre-warm flag to instruct Sidecar to warm the functions before they are switched live. This means when they are activated, they'll be booted and ready to go!

1# Pre-warm with the `deploy` command
2php artisan sidecar:deploy --activate --pre-warm
3 
4# Or pre-warm with the `activate` command
5php artisan sidecar:activate --pre-warm
Code highlighting powered by torchlight.dev, a Hammerstone product.

To read more about deploying and activating, take a look at their corresponding pages:

For more tips on how to use warming to further speed up your invocations, take a look at the Performance Tips page