Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon How does Octane handle request-specific services differently from singletons
    Laravel Octane request-specific services singletons application instance state management best practices Laravel performance


How does Octane handle request-specific services differently from singletons


Laravel Octane handles request-specific services differently from singletons to ensure that each request receives a clean state. Here are the key differences:

Request-Specific Services

1. Cloning the Application Instance: Octane clones the original application instance for each incoming request. This ensures that each request has its own isolated instance of the application, which helps to prevent state persistence issues between requests.

Singletons

1. Persistence Across Requests: Singletons are designed to persist across the lifetime of the application. They are resolved once and reused throughout the application. This can lead to issues if the singleton state is not properly managed, as it can persist across requests.

Handling Request-Specific Services

1. Dispatching Events: Before handling a request, Octane dispatches an event to ensure that the sandbox instance is properly initialized. This includes resetting any state that might have been mutated during previous requests.

Managing State

1. Config Service: Octane provides a separate config service for each sandbox instance. This ensures that the config service is reset for each request, preventing any state changes from persisting across requests.

Best Practices

1. Avoid Passing Application Instance: Avoid passing the application instance to services or singletons. Instead, use the `app()` helper or `Container::getInstance()` to ensure that the correct instance is used.
2. Use Callbacks: Use callbacks to return the sandbox instance instead of passing the application instance directly.

Example Configuration

php
// Instead of passing the application instance directly
$this->app->singleton(Service::class, function ($app) {
    return new Service($app);
});

// Use a callback to return the sandbox instance
$this->app->singleton(Service::class, function ($app) {
    return new Service(fn () => Container::getInstance());
});

Example Event Listener

php
// Dispatch an event before handling a request
$server->on('request', function ($request) use ($app) {
    $sandbox = clone $app;
    Container::setInstance($sandbox);
    $sandbox->make('events')->dispatch(new RequestReceived);
    // Handle the request
});

By following these best practices and understanding how Octane handles request-specific services and singletons, you can ensure that your application runs efficiently and effectively with Octane.

Citations:
[1] https://divinglaravel.com/laravel-octane-bootstrapping-the-application-and-handling-requests
[2] https://beyondco.de/blog/laravel-octane-introduction
[3] https://github.com/laravel/framework/discussions/47958
[4] https://laravel.com/docs/11.x/octane
[5] https://stackoverflow.com/questions/72391308/laravel-application-stuck-using-octane-after-x-amount-of-requests