Slide 1

Slide 1 text

@JomLaunch 2021 Build your High Concurrency App with PHP and Swoole Albert Chen

Slide 2

Slide 2 text

About Me { "data": { "human": { "name": "Albert Chen", "occupation": "Software Architect", "website": "https://albert-chen.com", "interests": [ "traveling", "programming", "cooking" ] }, "tags": [ "Laravel Artisan", "Swoole Enthusiast", "Open Source Contributor" ] } }

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Slido Join at slido.com #760920

Slide 5

Slide 5 text

Slido Join at slido.com #760920

Slide 6

Slide 6 text

What is Swoole?

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

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

Slide 9

Slide 9 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) • Excellent performance for high concurrency

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Usage Statistics for Websites

Slide 12

Slide 12 text

PHP’s Benchmark https://kinsta.com/blog/php-benchmarks/

Slide 13

Slide 13 text

Benchmark Comparison https://www.toptal.com/back-end/server-side-io-performance-node-php-java-go

Slide 14

Slide 14 text

Why PHP Performs Poorly in High Concurrency?

Slide 15

Slide 15 text

PHP Lifecycle

Slide 16

Slide 16 text

How many files are required for one request in Laravel? 426 get_included_files() In Laravel 8.52

Slide 17

Slide 17 text

Stateless PHP • How do we serve PHP today? • PHP-FPM • mod_php for Apache
 • Both patterns are all stateless

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

Blocking I/O in PHP • Resource can't be reused • I/O requests are blocking • 80% time cost on blocking I/O

Slide 21

Slide 21 text

Blocking I/O in PHP

Slide 22

Slide 22 text

Event Loop in NodeJS

Slide 23

Slide 23 text

Concurrency Problem • 100 requests at the same time need 100 processes • 100 connections will be established as well • Scale to increase concurrency

Slide 24

Slide 24 text

Let’s See What Changes in Swoole

Slide 25

Slide 25 text

Server Structure in Swoole

Slide 26

Slide 26 text

PHP Lifecycle in Swoole Run only once!

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

Benchmark Comparison between Go and Swoole

Slide 29

Slide 29 text

Server Benchmark in Go

Slide 30

Slide 30 text

Server Benchmark in Go

Slide 31

Slide 31 text

Server Benchmark in PHP

Slide 32

Slide 32 text

Server Benchmark in PHP

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

Coroutine in Swoole

Slide 35

Slide 35 text

Coroutine in Swoole • Non-Blocking I/Os are scheduled by coroutine automatically. PHP is the best Swoole is awesome! Hello world!

Slide 36

Slide 36 text

Coroutine in Swoole Hello Swoole 1.2.3.4 • Non-Blocking I/Os are scheduled by coroutine automatically.

Slide 37

Slide 37 text

Coroutine in Swoole 1.2.3.4 1.2.3.4 1.2.3.4 1.2.3.4 … Hello Swoole! • Non-Blocking I/Os are scheduled by coroutine automatically.

Slide 38

Slide 38 text

Coroutine in Swoole Hello Swoole! 1.2.3.4 1.2.3.4 1.2.3.4 1.2.3.4 … • Non-Blocking I/Os are scheduled by coroutine automatically.

Slide 39

Slide 39 text

Coroutine in Swoole • CSP Model 1 2 3 4 5

Slide 40

Slide 40 text

Some Facts about Swoole • Most of traditional PHP projects needs to be refactored. • Convert requests and responses

Slide 41

Slide 41 text

Some Facts about Swoole • Global states in PHP needs to be used more carefully. (eg. $_GET, $_POST, static, global)

Slide 42

Slide 42 text

Some Facts about Swoole • Global states in PHP needs to be used more carefully. (eg. $_GET, $_POST, static, global)

Slide 43

Slide 43 text

Some Facts about Swoole • Don’t use exit/die function in your PHP application.

Slide 44

Slide 44 text

Some Facts about Swoole • Memory leak may happen!!!

Slide 45

Slide 45 text

Some Facts about Swoole • You can try Hyperf framework for your new projects. Hyperf = Hyperspeed + Flexibility https://www.hyperf.io

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

Some Facts about Swoole • There’s knowledge gap for traditional PHP developers.

Slide 48

Slide 48 text

Q&A