laika-session

Deployment

One Server or Many

The file driver keeps sessions on the local disk. Behind a load balancer with several application servers, a user whose requests land on different servers loses their session. For more than one server, use one of these:

File Driver Under PHP-FPM

Always pass the file driver an explicit path in production:

SessionConfig::file(['path' => '/var/www/app/storage/sessions']);

The default, session_save_path(), causes trouble on common FPM setups:

For the directory you choose:

Files are created with mode 0600. If a CLI process running as another user creates session files, the pool user can’t open them, and those users get empty sessions.

HTTPS and Proxies

If TLS terminates in front of PHP, the cookie secure flag can’t detect HTTPS on its own. See Behind a Proxy or Load Balancer.

Concurrent Requests

Under PHP-FPM, one user’s parallel requests really do run at the same time in separate worker processes:

Garbage Collection

With the defaults (gc_probability = 1, gc_divisor = 100), roughly one session start in a hundred runs a cleanup:

Driver Cleanup
file Scans the directory for expired <PREFIX>_* files
mysql / model One DELETE ... WHERE last_activity < ?, which uses the last_activity index
redis / memcached Nothing to do, the server expires keys

On a busy site with the file driver, the directory scan runs inside a user’s request. To take it off the request path:

  1. Set gc_probability to 0.
  2. Run a scheduled job that deletes expired files. Match on this driver’s prefix, not on sess_*.

Database Drivers in Production

Pre-Deploy Checklist