I have a multi-tenant application that is hosted on an Azure Web app instance that is load balancing. The issue I am facing is that for the 90% of jobs, the code works perfectly fine. The 10% fail for reasons that I cannot yet understand but do not fall short of the probability that scaling is the issue.
Inside of my database service, I configure the connection (done in Middleware).
config()->set('database.connections.company', [
'driver' => 'pgsql',
'url' => env('DATABASE_URL'),
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', '5432'),
'database' => $db_name,
'username' => env('DB_USERNAME', ''),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'prefix' => '',
'prefix_indexes' => true,
'schema' => 'public',
'sslmode' => env('DB_SSLMODE', 'prefer'),
]);
I am yet to have in issue with any requests with this issue. The issue mainly occurs when I am then dispatching jobs from the request where I recieve this issue:
I've read resources on dotenv issues in PHP but I feel like this is a race condition. Currently, there is a CI/CD process that copies the .env files over to the Azure server. The startup script executes the following:
php artisan optimize:clear
php artisan cache:clear
Which stops any image related issues previously experienced with caching. It seems to me that the server the job was picked up by did not have the .env file hence why it returns the default 127.0.0.1 values.
Has anybody come across scaling issues in Azure with laravel like this? Is there an alternative way I can store this information to achieve the same solution that is not stateful? I have tried to use the Server properties in Azure configuration to store my environmental variables but this hasn't fixed this issue.
Any help appreciated.
Stack is PHP 8.0 running Laravel 8.83.23 on Azure with nginx:latest (upgraded to 8.0 as of a few months ago).

env()returns null if the key is not found so it wouldn't error on theDATABASE_URLkey, the issue is they're returning null always. I digged into the docs and found that the--envflag must be passed which I assume Azure is not doing during this scaling/load balancing @dz0nika