This is the simplest, most reliable, production-ready alternative to Redis and works perfectly for microservices under the same domain.
🎯 Why this is the Best Fit
- ✅ Works even on shared hosting (no Redis needed)
- ✅ Easy to set up (just 1 shared DB table)
- ✅ Secure and Laravel-native
- ✅ Supports scaling and logout everywhere
- ✅ Works with any Laravel version (8, 9, 10, 11…)
✅ Step-by-Step Setup: Laravel Session Sharing via Database
🔧 1. Use a Shared Database (or a shared sessions table) between all microservices
Let’s say you have a database called myhospital_sessions_db
.
Update your .env
in all services (main, doctors, hospitals):
SESSION_DRIVER=database
SESSION_CONNECTION=mysql_sessions # Optional alias if you're using different DB for sessions
SESSION_COOKIE=myhospitalnow_session
SESSION_DOMAIN=.myhospitalnow.com
APP_KEY=base64:YourSameAppKeyHere
🔧 2. Update config/database.php
if using separate connection
'mysql_sessions' => [
'driver' => 'mysql',
'host' => env('SESSION_DB_HOST', '127.0.0.1'),
'port' => env('SESSION_DB_PORT', '3306'),
'database' => env('SESSION_DB_DATABASE', 'myhospital_sessions_db'),
'username' => env('SESSION_DB_USERNAME', 'your_db_user'),
'password' => env('SESSION_DB_PASSWORD', 'your_db_password'),
],
Then in .env
:
SESSION_CONNECTION=mysql_sessions
🔧 3. Create sessions table (only once)
Run in any one service:
php artisan session:table
php artisan migrate
This will create the shared sessions
table.
🔧 4. Use same APP_KEY
across all Laravel apps
Copy the APP_KEY
from one service and paste into all others.
Example:
APP_KEY=base64:abc123... (must be exactly the same in every `.env`)
🔁 5. Clear config/cache after update in all apps
php artisan config:clear
php artisan cache:clear
php artisan config:cache
✅ Final Result:
- Log in once at
https://www.myhospitalnow.com/
- Automatically recognized on:
/doctors
/hospitals
/appointments
- Seamless, secure session sharing 🎉
⚠️ Bonus: Protect Session Hijacking
In config/session.php
, enable these for security:
'secure' => true, // HTTPS-only
'same_site' => 'lax', // Or 'strict'
Optional: Add Central Logout
To ensure logging out in one service invalidates session in all, just destroy session from DB:
Session::flush(); // Will remove session from shared DB
✅ This is production-ready and works on shared hosting, VPS, or containers.
Let me know if you want:
- A working example repo
- Laravel 11-ready config
- Redis version later for scale
Want me to generate the exact config/session.php
and .env
structure for you?
Leave a Reply