The Problem with Traditional PHP
Traditional PHP follows a “shared-nothing” architecture:- Starts a new PHP process
- Loads the framework from disk
- Parses and compiles PHP files
- Initializes dependencies
- Finally executes your code
- Destroys everything
The Always-Alive Solution
Lyger’s Always-Alive architecture keeps PHP running in memory:With Always-Alive, only your application code executes per request. The framework overhead is paid once at startup.
How It Works
Server Startup
Behind the Scenes
ServerManager
TheServerManager class orchestrates the persistent PHP worker:
Framework Preloading
PHP’s class autoloader caches parsed classes. Once loaded, subsequent instantiations are nearly instant.
Request Handling
When a request arrives:- Rust receives HTTP request (port 8000)
- Rust parses request (method, URI, headers, body)
- Rust calls PHP via FFI with request data
- PHP executes router (already in memory)
- PHP returns response to Rust
- Rust sends HTTP response to client
Performance Benefits
Startup Time Comparison
Your actual application code execution time remains the same. The improvement comes from eliminating startup overhead.
Real-World Impact
Memory Considerations
Memory Usage
Memory Leaks
In traditional PHP, memory leaks are cleared after each request. With Always-Alive, you must be careful:Best practices for Always-Alive
Best practices for Always-Alive
- Avoid static arrays that grow - Use proper cache implementations
- Close resources - Database connections, file handles, etc.
- Unset large variables - Help PHP’s garbage collector
- Use dependency injection - Avoid globals and static state
- Monitor memory usage - Set up alerts for memory growth
Starting the Server
Command Line
Custom Port
Programmatic
Stopping the Server
PressCtrl+C or send SIGTERM:
Development vs Production
Development Mode
In development, you’ll want to reload code changes:During development, restart the server when you change code. Use file watchers to automate this.
Production Mode
In production, the Always-Alive server shines:Comparing to Other Solutions
Lyger provides RoadRunner/Swoole-like performance with much simpler architecture thanks to Rust FFI.
Troubleshooting
Server Won’t Start
Port Already in Use
Memory Growth
Monitor memory usage:Next Steps
Architecture Overview
Understand the full system architecture
Rust FFI Integration
Learn how PHP talks to Rust
Zero-Copy Database
Optimize database operations
Routing
Build your application routes