3. Outline
• Introduction of Swoole
• PHP’s Internal Lifecycle
• Blocking I/O in PHP
• Server Structure of Swoole
• Benchmark of Swoole
• Coroutine in Swoole
• Some Facts about Swoole
• Q&A
8. What is Swoole?
• Since 2012
• Open source based on Apache-2.0 License
• 17k stars until July 2021
• More than 100 contributors
• Adopted by many enterprises in China
• O
ffi
cially supported by Laravel Octane
9. What is Swoole?
• A C extension for PHP
• An asynchronous network engine for PHP
• Features:
• Event-driven non-blocking I/O
• HTTP / HTTP2 / Websocket / TCP / UDP
• Coroutine (CSP Model)
• Excellent performance for high concurrency
10. What is Swoole?
• Not a new language, but an extension for PHP
• Provides many new features for traditional PHP
• New lifecycle in Swoole
• Server patterns, coroutine, async I/O, Process Management, etc.
• It's still under active development
• Stable for production environment
• High performance
16. How many files are required for one
request in Laravel?
426
get_included_files()
In Laravel 8.52
17. Stateless PHP
• How do we serve PHP today?
• PHP-FPM
• mod_php for Apache
• Both patterns are all stateless
18. Stateless PHP
• Pros
• Easy scaling
• Simple, less risk causing memory leaks
• Cons
• States can't be shared between requests
• States must rely on external storages
• Resources can't be reused e
ffi
ciently
• Connection cost is expensive (like database)
• Not good for high performance
19. Blocking I/O in PHP
• PHP is originally created as glue layer to call C functions
• The I/O model is blocking by default
• Almost all client-side libraries involving I/O are blocking
• Multiple process for handling blocking I/O
• I/O bound concurrency depends on process number
• Cost for context switch in processes is expensive
20. Blocking I/O in PHP
• Resource can't be reused
• I/O requests are blocking
• 80% time cost on blocking I/O
23. Concurrency Problem
• 100 requests at the same time need 100 processes
• 100 connections will be established as well
• Scale to increase concurrency
27. Server Structure in Swoole
• Code Is Persistent in Memory
• Swoole Server Takes Lifecycle Over PHP
• Reduce Initialization Steps
• Reduce Response Latency
• Event-driven Non-blocking I/O for Requests
45. Some Facts about Swoole
• You can try Hyperf framework for your new projects.
Hyperf = Hyperspeed + Flexibility
https://www.hyperf.io
46. Some Facts about Swoole
• Swoole is now o
ffi
cially supported by Laravel team.
• Laravel will become friendly in long-running server
• Some Octane features rely on Swoole
• No coroutine features are supported yet
47. Some Facts about Swoole
• There’s knowledge gap for traditional PHP developers.