Slide 1

Slide 1 text

High Concurrency Web Architecture and Laravel Performance Tuning @LaravelConf Taiwan 2019 By Albert Chen

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

What Is High Concurrency?

Slide 4

Slide 4 text

What Is High Concurrency? PV = 2,000,000 (200w) QPS = (200w x 0.8) / (24 x 3600 x 0.2) 93 QPS 😀

Slide 5

Slide 5 text

What Is High Concurrency? 2,000,000 (200w) in 1 minute QPS = 200w / 60 33333 QPS😭

Slide 6

Slide 6 text

What Is High Concurrency? Read in High Concurrency • Vertical Scaling • Horizontal Scaling • Content Delivery Network • Data Caching • Single Point Optimization Write in High Concurrency • Message Queue • Rate Limit • Maximum Serving Limit • Wait in Line • Rules Optimization

Slide 7

Slide 7 text

Performance Tuning in Laravel

Slide 8

Slide 8 text

Performance Tuning in Laravel • Route and Config Files Cache • Cache (Not Limited to Database) • Async Event (Delayed Processing) • Database Read/Write Separation • OPCache • Preloading in PHP 7.4

Slide 9

Slide 9 text

Route and Config Files Cache php artisan route:cache

Slide 10

Slide 10 text

Route and Config Files Cache Register Route Route Register Route Route Register Route Route Route Collection • Lifecycle for Registering Routes

Slide 11

Slide 11 text

Route and Config Files Cache Unserialize Cached Serialized Route Collection Route Collection • Lifecycle for Routes Caching

Slide 12

Slide 12 text

Route and Config Files Cache Route Numbers No cache (ms) Cache (ms) Speed-up 1 2.5 1.9 1.3x 10 5.2 2.6 2.0x 100 22.5 4.3 5.3x 1,000 166 32 5.1x 10,000 1,513 334 4.5x https://voltagead.com/laravel-route-caching-for-improved-performance/

Slide 13

Slide 13 text

Route and Config Files Cache https://voltagead.com/laravel-route-caching-for-improved-performance/

Slide 14

Slide 14 text

Route and Config Files Cache php artisan config:cache

Slide 15

Slide 15 text

Route and Config Files Cache Merged Config File • Lifecycle for Config Caching Config
 Files

Slide 16

Slide 16 text

Benchmark Comparison • Provider: Google Cloud Platform • Instance Type: n1-standard-8 • CPUs: 8 vCPU Cores (Skylake) • Memory: 30 G • Disk: 20 G SSD • OS: Ubuntu 18.04 LTS • PHP Version: 7.3

Slide 17

Slide 17 text

Benchmark Comparison • Benchmark without Cache

Slide 18

Slide 18 text

Benchmark Comparison • Benchmark with Routes and Config Cache

Slide 19

Slide 19 text

Cache (Not Limited to Database) • Discover Hotspot Data (Hit Rate) • Proper TTL for Cached Data • Avoid Expiring Cache at The Same Time • Revoke Cache when Data Changes • Avoid Cache Missing • Hierarchical Cache Design • High Available Cache System • Warm Up the Cache

Slide 20

Slide 20 text

Cache (Not Limited to Database) • Revoke Cache when Data Changes

Slide 21

Slide 21 text

Cache (Not Limited to Database) • Avoid Cache Missing

Slide 22

Slide 22 text

Cache (Not Limited to Database) • Avoid Cache Missing Client Database Cache Layer Client Client

Slide 23

Slide 23 text

Cache (Not Limited to Database) • Hierarchical Cache Design Database Local Memory Local Memory Local Memory Cache Layer Cache Revoke

Slide 24

Slide 24 text

Cache (Not Limited to Database) • High Available Cache System https://rancher.com/blog/2019/deploying-redis-cluster/

Slide 25

Slide 25 text

Cache (Not Limited to Database) • Warm Up the Cache Request Request Request Request Cache Layer Database Time Consuming Query (Warm Up)

Slide 26

Slide 26 text

Async Event (Delayed Processing) Producer Kafka Rabbit MQ Queue Driver Consumer Producer Producer Producer Consumer Consumer Consumer (Laravel Event) (Laravel Queue Worker)

Slide 27

Slide 27 text

Database Read/Write Separation 20% on Write 80% on Read Master Slave Slave Slave Read Replicas

Slide 28

Slide 28 text

Database Read/Write Separation

Slide 29

Slide 29 text

Database Read/Write Separation App Master Slave Slave Slave Read Replicas Write Read

Slide 30

Slide 30 text

Database Read/Write Separation

Slide 31

Slide 31 text

Database Read/Write Separation

Slide 32

Slide 32 text

Database Read/Write Separation Master Slave Slave Slave Read Replicas SQL Proxy Slave Write Read Read Read Read

Slide 33

Slide 33 text

OPCache PHP
 Files Tokenizing Semantic Parsing AST Generate Bytecode Execute Bytecode Output • Lifecycle in PHP

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

Benchmark Comparison • Benchmark with OPCache

Slide 36

Slide 36 text

Benchmark Comparison • Benchmark without OPCache

Slide 37

Slide 37 text

Preloading in PHP 7.4

Slide 38

Slide 38 text

Preloading in PHP 7.4 PHP
 Files Tokenizing Semantic Parsing AST Generate Bytecode Execute Bytecode Output Load Bytecode From Memory Check Bytecode Cache Preload in Memory • Lifecycle in PHP with Preloading

Slide 39

Slide 39 text

Preloading in PHP 7.4 composer require ayesh/composer-preload

Slide 40

Slide 40 text

Benchmark Comparison • Benchmark with Preloading Benchmark Preloaded files Server startup time Opcache memory used Per request memory used QPS No preloading 0 0.06 s 16 MB after warmup 1,825 KB 596 rq/s Preload hot classes 878 0.26 s 21 MB 869 KB 695 rq/s Preload everything 14541 1.56 s 105 MB 881 KB 675 rq/s https://github.com/composer/composer/issues/7777

Slide 41

Slide 41 text

Use Case Analysis

Slide 42

Slide 42 text

Banana News • PV is around 200w • Large amount of content resource • Fuzzy search support • Changeable news rank by topics • 99% read, 1% write

Slide 43

Slide 43 text

Banana News Server Database Images Server (CDN)

Slide 44

Slide 44 text

Banana News Database Images Server (CDN) Server Server Server Load Balancer

Slide 45

Slide 45 text

Banana News Images Server (CDN) Load Balancer Master Slave Slave Slave Read Server Server Server

Slide 46

Slide 46 text

Banana News Images Server (CDN) Load Balancer Master Slave Slave Slave Read Redis Cache Server Server Server

Slide 47

Slide 47 text

Banana News Images Server Load Balancer Master Slave Slave Slave Read Redis Cache CDN Elastic Search Server Server Server eg. Cloudflare

Slide 48

Slide 48 text

AATIX • High concurrency in short period • Limited tickets for selling • High available service

Slide 49

Slide 49 text

AATIX Images Server Load Balancer Master Slave Slave Slave Read Redis Cache CDN Server Server Server

Slide 50

Slide 50 text

AATIX Images Server Load Balancer Master Slave Slave Slave Read Redis Cache CDN Server Server Server 😱

Slide 51

Slide 51 text

AATIX Read HTTP Request CDN Queue Write DB

Slide 52

Slide 52 text

AATIX Images Server Load Balancer Master Slave Slave Slave Read Redis Cache CDN Server Server Server Queue

Slide 53

Slide 53 text

AATIX Master Slave Slave Slave Read Redis Cache Server Server Server Queue Local Cache Atomic Counter Rate Limit Circuit Breaking

Slide 54

Slide 54 text

AATIX • Avoid large amount of write requests at the same time • Queue • Rate Limit • Reduce repeated requests from clients • Service isolation for hotspot data • Circuit breaking for high availability

Slide 55

Slide 55 text

• Large amount of webhook calls • High availability for webhooks • Instant response time • Chat service for live streaming

Slide 56

Slide 56 text

HandsUp

Slide 57

Slide 57 text

HandsUp Facebook Webhooks Message Load Balancer Server Server Server Queue PubNub
 Service Analysis Service Swoole Service PubSub Many to Many Queue Workers

Slide 58

Slide 58 text

The Last But Not the Least

Slide 59

Slide 59 text

Q&A