Skip to content

.env values (APP_KEY & DB_DATABASE) not loaded under high request load #57632

@saeedvir

Description

@saeedvir

Laravel Version

12.36.1

PHP Version

8.3.10

Database Driver & Version

mysql 8.0

Description

When the application receives a high number of requests per second, two critical environment variables fail to load properly:

Steps To Reproduce

When the application receives a high number of requests per second, two critical environment variables fail to load properly:

Errors :
production.ERROR: No application encryption key has been specified. {"exception":"[object] (Illuminate\Encryption\MissingAppKeyException(code: 0): No application encryption key has been specified. at C:\laragon\www\LaravelProject\vendor\laravel\framework\src\Illuminate\Encryption\EncryptionServiceProvider.php:83)

local.ERROR: SQLSTATE[HY000] [1049] Unknown database 'laravel' (Connection: mysql, SQL: select * from sessions where id = GhVeF3wLwdWFekfNuHEFjdoGBNksHsdeimH32rC2 limit 1) {"exception":"[object] (Illuminate\Database\QueryException(code: 1049): SQLSTATE[HY000] [1049] Unknown database 'laravel' (Connection: mysql, SQL: select * from sessions where id = GhVeF3wLwdWFekfNuHEFjdoGBNksHsdeimH32rC2 limit 1) at C:\laragon\www\LaravelProject\vendor\laravel\framework\src\Illuminate\Database\Connection.php:824)

APP_KEY cannot be read
File: src/Illuminate/Encryption/EncryptionServiceProvider.php

Even though .env contains correct database credentials,
the database name in config/database.php is replaced by the fallback value 'laravel'.

'database' => env('DB_DATABASE', 'laravel')

Expected Behavior

Laravel should consistently read all .env variables (e.g., APP_KEY, DB_DATABASE) under any load or concurrency level.

Actual Behavior

Under heavy load:

The app randomly fails to read APP_KEY

The database config falls back to 'laravel' instead of using .env value

Steps To Reproduce

Set up a Laravel app with .env correctly configured:

APP_KEY=base64:UUVDVdesybjNpBP5QeXP68c+x9fLNBfmICMTxoEdlOA=
DB_DATABASE=my_database_name

Use a load testing tool (e.g. ab, wrk, or k6) to send hundreds of requests per second.

Observe random failures:

Encryption errors due to missing APP_KEY

Database connection using default 'laravel' instead of .env value

Temporary Workaround

Manually hardcode the values in config files:

// config/app.php
'key' => 'base64:UUVDVdesybjNpBP5QeXP68c+x9fLNBfmICMTxoEdlOA=';

// config/database.php
'database' => env('DB_DATABASE', 'my_database_name');

This resolves the issue temporarily, but it defeats the purpose of environment configuration.

Environment

Laravel version: 12.x (12.36.1)

PHP version: 8.3.x

Server: Apache

Cache driver: (e.g., file, redis, etc.)

Operating System: Windows 11

Additional Notes

It seems that under high concurrency, Laravel may be failing to read the .env file properly, possibly due to a race condition or environment caching issue in the bootstrap phase.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions