Mastering Swoole PHP

讀書會#1

12/23

19:30~21:00

@17Live 大樓三樓 銀河灰會議室

About This Book

Zeev Suraski
Co-founder of Zend, Co-architect of PHP

"Swoole is a strategic development in the evolution of PHP, bringing high performance async-IO based apps to the mainstream of the platform. This book is everything you need to unlock that power of your new async-PHP apps!"

"Swoole is a tremendous technology and a game changer for PHP developers. I started to play with it in 2017 and I created the first runtime for Expressive framework. The results have been incredible, huge performance improvement without using a web server, amazing! If you want to design asynchronous applications in PHP the book of Bruce Dou is a must."

Enrico Zimuel
Member of PHP-FIG and co-author of Expressive and Zend Framework

About This Book

  • Background

  • General concepts

  • The practical world on Linux OS

  • Connect with protocols

  • Swoole runtime and internal

  • Hands on Swoole PHP

  • Use cases and patterns

26

77

33

28

40

37

64

What is Swoole?

  • Since 2012

  • Open source based on Apache-2.0 License

  • 16.3k start on Dec. 2020

  • More than 100 contributors

  • Adopted by many enterprises in China

  • Swoole Plus for enterprise support

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

Stateless PHP

  • How do we serve PHP today?

    • ​PHP-FPM

    • mod_php for Apache

  • Both patterns are all stateless

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 efficiently

    • Connection cost is expensive (like database)

    • Not good for high performance

PHP FPM

  • FPM (FastCGI Process Manager)

PHP FPM

PHP Architecture

  • SAPI: Adapters like PHP-Cli, PHP-Fpm or embeded

  • ZendVM: Composed with Compiler and Executor, like JVM in Java

  • Extension: Including PHP Extension and Zend Extension

PHP Lifecycle

Blocking I/O in PHP

blocking I/O model

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

Performance Problem

Identify the bottoleneck first!

Performance Problem

  • Are we wasting resources by not using the reusable variables?
  • Do we request the same data not changed from the database for each request?
  • Are we requesting the data not used by the current request?
  • Can we cache the remote data locally?
  • Can we use async I/O instead of blockingI/O to save processes?
  • Can we defer the heavy task by sending them into a queue and executing them separately?
  • Etc.

Performance Problem

  • Resource can't be reused

  • I/O requests are blocking

  • 80% time cost on blocking I/O

Execute PHP Code

Initialize PHP Code

Database Request

HTTP API Request

Response

Include PHP Files

Concurrency Problem

  • 100 requests at the same time need 100 processes

  • 100 connections will be established as well

  • Scale to increase concurrency

Concurrency Problem

  • Synchronous Process

Concurrency Problem

  • Asynchronous Process

Concurrency Problem

  • Asynchronous Process

    • Save data into a queue then ACK back to the client
    • Check process status periodically
    • Waste resource for checking requests
    • Main process lost the control about these requests
    • Not applicable for dependent requests

Concurrency Problem

  • Fast and Slow Components

Concurrency Models

Concurrency Models

  • Actor

    • Erlang

  • CSP (Communicating Sequential Processes)

    • Golang, Swoole

  • STM (Software Transactional Memory)

    • Clojure

  • Guilds
    • Ruby 3

Don't communicate by sharing memory; share memory by communicating. (Rob Pike)

Concurrency Models

  • Actor

    • Multiple actors can run at the same

    • Each actor has its own identifier and internal states

    • Actors communicate with each other by sending asynchronous messages

    • Every actor has an mailbox (queue)

    • Actors are isolated

Concurrency Models

  • CSP

    • Decoupled by channel

    • Pub/Sub Pattern

    • Shared memory and lock are implemented in channel

    • Only one active consumer can access data

    • Processes are anonymous

Concurrency Models

  • STM

    • Use shared memory

    • No need for lock

    • All changes of data are encapsulated in transactions

    • Every thread has a copy of its working data and can change its value

    • Like transactions in database

Concurrency Models

  • Guild

    • Threads from different guilds can run in parallel

    • Threads that belong to the same Guild can’t execute in parallel (GGL)

    • Immutable objects can be shared (read) across Guilds

    • Guilds can communicate with each other using channel (deep copy)

Reference

Discussion

Mstering Swoole PHP 讀書會#1

By Albert Chen

Mstering Swoole PHP 讀書會#1

  • 455