How Swoole
Blows Up Your Mind?
@PHPCon Okinawa 2019
By Albert Chen
About Me
• Albert Chen
• Software Architect
• M17 HandsUp
• Open Source Maintainer
What is Swoole?
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)
• High performance for high concurrency
Why PHP Performs
Poorly in High Concurrency?
Lifecycle in PHP
Lifecycle in PHP
PHP
Files
Tokenizing
Semantic
Parsing
AST
Generate
Bytecode
Execute
Bytecode
Output
Load Bytecode
From Memory
Check
Bytecode Cache
Blocking I/O in PHP
Request
Request
Request
Request
PHP
FPM
FPM
Pool
FPM
Pool
Worker
Worker
Worker
Worker
Blocking I/O
Eventloop in NodeJS
There’s no Eventloop
in Traditional PHP
Let’s See What Changes
in Swoole?
Structure in Swoole Server
Structure in Swoole Server
• Code Is Persistent in Memory
• Swoole Takes Lifecycle Over PHP
• Reduce Initialization Steps
• Reduce Response Latency
• Event-driven Non-blocking I/O for Requests
Benchmark Comparison
of Go with Swoole
Server Benchmark in Go
Server Benchmark in Go
Server Benchmark in Swoole
Server Benchmark in Swoole
This Doesn’t Mean
PHP with Swoole is Faster Than Go
(Of Course Not)
Coroutine in Swoole
Coroutine in Swoole
• Blocking I/Os Are Scheduled By Coroutine Automatically.
Coroutine in Swoole
• Blocking I/Os Are Scheduled By Coroutine Automatically.
Coroutine in Swoole
• CSP Model
Does This Mean Every
Blocking I/O Doesn’t Block Anymore?
Coroutine in Swoole
• How about This?
Coroutine in Swoole
• How about This?
Runtime Hooks
Transform Blocking I/Os into
Coroutines with Magic!
Use Case
In HandsUp
Use Case
Facebook
Webhooks
Message
Load
Balancer
Server
Server
Server
Queue
PubNub
Service
Analysis
Service
Swoole Service
PubSub
Many to Many
Queue Workers
High Concurrent
Requests
Use Case
• Replace Guzzle Handler with Swoole Client
Q&A

How Swoole Blows Up your Mind about PHP?