Slide 1

Slide 1 text

How Swoole Blows Up Your Mind? @PHPCon Okinawa 2019 By Albert Chen

Slide 2

Slide 2 text

About Me • Albert Chen • Software Architect • M17 HandsUp • Open Source Maintainer

Slide 3

Slide 3 text

What is Swoole?

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Why PHP Performs Poorly in High Concurrency?

Slide 7

Slide 7 text

Lifecycle in PHP

Slide 8

Slide 8 text

Lifecycle in PHP PHP
 Files Tokenizing Semantic Parsing AST Generate Bytecode Execute Bytecode Output Load Bytecode From Memory Check Bytecode Cache

Slide 9

Slide 9 text

Blocking I/O in PHP Request Request Request Request PHP FPM FPM Pool FPM Pool Worker Worker Worker Worker Blocking I/O

Slide 10

Slide 10 text

Eventloop in NodeJS

Slide 11

Slide 11 text

There’s no Eventloop in Traditional PHP

Slide 12

Slide 12 text

Let’s See What Changes in Swoole?

Slide 13

Slide 13 text

Structure in Swoole Server

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

Benchmark Comparison of Go with Swoole

Slide 16

Slide 16 text

Server Benchmark in Go

Slide 17

Slide 17 text

Server Benchmark in Go

Slide 18

Slide 18 text

Server Benchmark in Swoole

Slide 19

Slide 19 text

Server Benchmark in Swoole

Slide 20

Slide 20 text

This Doesn’t Mean PHP with Swoole is Faster Than Go (Of Course Not)

Slide 21

Slide 21 text

Coroutine in Swoole

Slide 22

Slide 22 text

Coroutine in Swoole • Blocking I/Os Are Scheduled By Coroutine Automatically.

Slide 23

Slide 23 text

Coroutine in Swoole • Blocking I/Os Are Scheduled By Coroutine Automatically.

Slide 24

Slide 24 text

Coroutine in Swoole • CSP Model

Slide 25

Slide 25 text

Does This Mean Every Blocking I/O Doesn’t Block Anymore?

Slide 26

Slide 26 text

Coroutine in Swoole • How about This?

Slide 27

Slide 27 text

Coroutine in Swoole • How about This?

Slide 28

Slide 28 text

Runtime Hooks Transform Blocking I/Os into Coroutines with Magic!

Slide 29

Slide 29 text

Use Case In HandsUp

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Use Case • Replace Guzzle Handler with Swoole Client

Slide 32

Slide 32 text

Q&A