SlideShare une entreprise Scribd logo
1  sur  49
Télécharger pour lire hors ligne
Massively Scaled High Performance Web Services
with PHP
Demin Yin
https://joind.in/talk/e81e7
• Staff Engineer and Technical Lead on Glu Mobile’s Design Home team.
• Playing with PHP since 2000.
• Worked for a diverse group of companies in China and US, including eBay, Visa and Glu.
• Focus on building high performance, secure web services.
• Born in China, worked in U.S., live in Canada.
• On twitter: @deminy
• On Github: https://github.com/deminy
About Me
2
● An Overview on Design Home and the Inbox Service
● The Inbox Service: Key Improvements Made
1. Web Server: Dockerized PHP 7 containers
2. HTTP Processing: Background processing, HTTP compression, and more
3. Data Storage: Doing NoSQL with Redis and Couchbase
4. Hardware and Network: Migrating to Amazon Web Services
● Next
● Q&A
Agenda
3
● An Overview on Design Home and the Inbox Service
● The Inbox Service: Key Improvements Made
1. Web Server: Dockerized PHP 7 containers
2. HTTP Processing: Background processing, HTTP compression, and more
3. Data Storage: Doing NoSQL with Redis and Couchbase
4. Hardware and Network: Migrating to Amazon Web Services
● Next
● Q&A
Agenda
4
• Launched in Q4 2016.
• #1 Top Free and #7 Top Grossing game in the U.S. App Store for iPhone.
• #1 Top Free and #23 Top Grossing game in the U.S. Google Play Store.
• 1M+ daily active users; 40M votes daily; 1 billion designs to date.
• 100,000 API calls per minute; 60 56 ms response time on average.
Design Home:
Top-ranked Mobile Game with PHP Backend
5
Inbox
Localization
Receipt Validation
Moderation
Email
E-commerce
Event Logging
Main
Service
……
Database
Client
Client
Client
Client
Client
Client
Client
Design Home: List of Backend Services
6
● REST APIs with different CRUD endpoints to manage inbox messages.
● PHP 7, Nginx, Composer, NoSQL, Docker, AWS, Development Tools, ……
● NoSQL: Redis + Couchbase.
● One of the busiest microservices.
Average Requests: 14k rpm
Peak Requests: 34k rpm
Response Time: less than 9ms
The Inbox Service: Technical Overview
7
The Inbox Service: Technical Overview (cont.)
● PHP 7.
● Composer.
● Opcache.
● Data cache: APCu, distributed cache and chained cache.
● Tests in place.
● Unit tests.
● Blackbox tests, or feature tests, or functional tests.
● PHP The Right Way: Follow good coding practices including design patterns.
8
● An Overview on Design Home and the Inbox Service
● The Inbox Service: Key Improvements Made
1. Web Server: Dockerized PHP 7 containers
2. HTTP Processing: Background processing, HTTP compression, and more
3. Data Storage: Doing NoSQL with Redis and Couchbase
4. Hardware and Network: Migrating to Amazon Web Services
● Next
● Q&A
Agenda
9
Hardware & Network
● Migrate to Amazon ECS.
● Hardware upgrade.
● Move everything into VPC.
The Inbox Service: Key Improvements Made
HTTP Processing
● Use background processing.
● Use HTTP compression (gzip).
● Dynamic content caching.
Web Server Instances
● Decouple microservices.
● Dockerize the microservices.
● Development Tools Integration.
Data Storage
● NoSQL solutions.
● Fight against IO and disk space.
● Faster PHP drivers.
10
● An Overview on Design Home and the Inbox Service
● The Inbox Service: Key Improvements Made
1. Web Server: Dockerized PHP 7 containers
2. HTTP Processing: Background processing, HTTP compression, and more
3. Data Storage: Doing NoSQL with Redis and Couchbase
4. Hardware and Network: Migrating to Amazon Web Services
● Next
● Q&A
Agenda
11
1.1. Decouple Microservices
Inbox
Localization
Receipt Validation
Moderation
Email
E-commerce
Event Logging
Main
Service
……
Database
Client
Client
Client
Client
Client
Client
Client
12
• Improved and simplified development environments.
• Easier to upgrade web server and PHP environments.
• Easier to troubleshoot and debug issues.
1.2. Dockerized Microservices
13
1.2. Dockerized Microservices (cont.)
14
Base Images
HTTP/REST microservices
asynchronous microservices
job worker instances
1.2. Dockerized Microservices (cont.)
Specification:
1. Tags match with PHP versions.
2. Tag latest is never used.
3. Freeze tagged images once
used.
4. Images are built and tagged
manually.
Benefits:
1. Less efforts to build images for
different microservices.
2. Easy and safe to upgrade to
new versions of PHP.
3. Easier to fix security
vulnerabilities.
4. Easier to make improvements
across different microservices.
15
Base Images
● Error Handling, Error Reporting and System Monitoring
○ New Relic
○ Bugsnag
○ Amazon CloudWatch
● Security Check
○ SonarQube
○ SensioLabs Security Checker
● Debugging and Profiling: Blackfire, Xdebug, etc.
1.3. Development Tools Integration
16
● An Overview on Design Home and the Inbox Service
● The Inbox Service: Key Improvements Made
1. Web Server: Dockerized PHP 7 containers
2. HTTP Processing: Background processing, HTTP compression, and more
3. Data Storage: Doing NoSQL with Redis and Couchbase
4. Hardware and Network: Migrating to Amazon Web Services
● Next
● Q&A
Agenda
17
● Use cases: error handling, data reporting, outgoing emails, push notifications, etc.
● Common approaches:
● Execute external program.
● Register shutdown functions.
● Job server (Gearman, Resque, RabbitMQ, etc.).
● Function fastcgi_finish_request() under PHP-FPM.
● …...
2.1. Background Processing
exec("nohup ./http.sh > /dev/null &");
18
2.1. Background Processing (cont.)
Output: 1234 Output: 1
19
2.1. Background Processing (cont.)
Average Response Time
Without background processing: 13 ms
With background processing: 9 ms
20
How to prevent and address failed operations properly?
● Basic data validation first.
● Exponential backoff.
● https://github.com/Crowdstar/exponential-backoff
● Proper error reporting.
2.1. Background Processing (cont.)
21
For more details, please check
• Background Processing: https://github.com/Crowdstar/background-processing
• Technical Discussion: https://github.com/deminy/background-processing-in-php
2.1. Background Processing (cont.)
22
2.2. HTTP Compression
23
2.2. HTTP Compression
gzip on;
24
2.2. HTTP Compression (cont.)
gzip on;
gzip_types text/plain text/css application/json text/xml application/xml application/xml+rss
text/javascript;
25
2.2. HTTP Compression (cont.)
gzip on;
gzip_types text/plain text/css application/json text/xml application/xml application/xml+rss
text/javascript;
gzip_proxied any;
26
2.2. HTTP Compression (cont.)
gzip on;
gzip_types text/plain text/css application/json text/xml application/xml application/xml+rss
text/javascript;
gzip_proxied any;
gzip_comp_level 5;
gzip_min_length 1280;
27
2.2. HTTP Compression (cont.)
If HTTP header ‘Content-Length’ is not set in a PHP response, Nginx will always compress the
response even its length is less than the minimum length.
• Slim 3 always has HTTP header ‘Content-Length’ set by default.
• Laravel and Lumen don’t have HTTP header ‘Content-Length’ set.
28
2.2. HTTP Compression (cont.)
A Lumen Middleware to Make HTTP Compression Work as Should
29
2.3. Dynamic Content Caching
Challenge: feed new data to the client.
Possible Solutions:
● Server push, but it’s not supported in HTTP 1 (It’s supported under HTTP 2 and the WebSocket API).
● HTTP Clients built on top of HTTP 1 have to hit servers again and again to check and fetch new data.
Our Approach:
● Data cache on the server side if OK.
● Use the Last-Modified and If-Modified-Since headers, and return HTTP 304 "Not Modified" if no need to
retransmit the requested resources.
30
2.3. Dynamic Content Caching (cont.)
Related HTTP headers:
● If-None-Match, If-Unmodified-Since
● Cache-Control
● Content-Location
● ETag
● Expires
● Vary
● …...
31
● An Overview on Design Home and the Inbox Service
● The Inbox Service: Key Improvements Made
1. Web Server: Dockerized PHP 7 containers
2. HTTP Processing: Background processing, HTTP compression, and more
3. Data Storage: Doing NoSQL with Redis and Couchbase
4. Hardware and Network: Migrating to Amazon Web Services
● Next
● Q&A
Agenda
32
● Lots of choices: Redis, Couchbase, Aerospike, MongoDB, …
● Our selection and implementation: Redis and Couchbase.
● Redis.
● Couchbase.
● Concerns with NoSQL
○ limitations on queries.
○ indexes.
○ serialization and compression.
3.1. NoSQL
33
• Data compression to reduce network IO and save disk space.
• Compress larger messages before saving.
• Couchbase encoder (serialization).
• Use message templates.
• Shorten field names.
• Add ttl (time to live) on certain messages (to expire messages).
3.2. Fight Against IO and Disk Space
34
• PhpRedis: the PHP extension for Redis.
• Predis: the Redis client written in PHP.
• Other drivers
• Package predis/predis-async: asynchronous (non-blocking) version of Predis built on
top of ReactPHP.
• Class SwooleCoroutineRedis: the asynchronous Redis Client from Swoole.
3.3. Faster PHP Drivers (Redis)
35
3.3. Faster PHP Drivers (Redis)
Before and after switching from Predis to PhpRedis
Average request time reduced from 16 ms to 11 ms
36
● An Overview on Design Home and the Inbox Service
● The Inbox Service: Key Improvements Made
1. Web Server: Dockerized PHP 7 containers
2. HTTP Processing: Background processing, HTTP compression, and more
3. Data Storage: Doing NoSQL with Redis and Couchbase
4. Hardware and Network: Migrating to Amazon Web Services
● Next
● Q&A
Agenda
37
4.1. Deployment with Amazon ECS
How did we do code deployment before?
38
Benefits:
• Zero downtime deployments.
• Quick rollback.
• ELB health checks.
• Autoscaling.
4.1. Deployment with Amazon ECS (cont.)
39
• Multiple deployment environments in AWS: test, dev, RC, staging, and production.
• Staging environment is helpful.
• to do some final tests before major production deployments.
• to reproduce and identify production issues.
4.1. Deployment with Amazon ECS (cont.)
40
4.2. Hardware Upgrade
Feb 2018: upgraded API servers with faster ones
(c5.xlarge: 4 vCPU, 8GB memory; Enhanced networking enabled)
41
• Place instances closer to each other.
• Move instances to same locations (Availability Zones).
• Use internal network instead of Internet to communicate.
4.3. Move Everything into VPC
42
4.3. Move Everything into VPC (cont.)
Jan 2019: upgraded API servers with faster ones that were closer to the database nodes (same zone)
In the main service, Couchbase operation time decreased to 5 ms
43
● An Overview on Design Home and the Inbox Service
● The Inbox Service: Key Improvements Made
1. Web Server: Dockerized PHP 7 containers
2. HTTP Processing: Background processing, HTTP compression, and more
3. Data Storage: Doing NoSQL with Redis and Couchbase
4. Hardware and Network: Migrating to Amazon Web Services
● Next
● Q&A
Agenda
44
There are always rooms to
push the limit of PHP
45
• PHP-FPM
• Asynchronous I/O
• Swoole
• Network Protocols
• HTTP/2
• Tars
Next
46
● An Overview on Design Home and the Inbox Service
● The Inbox Service: Key Improvements Made
1. Web Server: Dockerized PHP 7 containers
2. HTTP Processing: Background processing, HTTP compression, and more
3. Data Storage: Doing NoSQL with Redis and Couchbase
4. Hardware and Network: Migrating to Amazon Web Services
● Next
● Q&A
Agenda
47
https://joind.in/talk/e81e7
Questions?
48
Thanks
https://joind.in/talk/e81e7
49

Contenu connexe

Tendances

Spilo, отказоустойчивый PostgreSQL кластер / Oleksii Kliukin (Zalando SE)
Spilo, отказоустойчивый PostgreSQL кластер / Oleksii Kliukin (Zalando SE)Spilo, отказоустойчивый PostgreSQL кластер / Oleksii Kliukin (Zalando SE)
Spilo, отказоустойчивый PostgreSQL кластер / Oleksii Kliukin (Zalando SE)
Ontico
 
plProxy, pgBouncer, pgBalancer
plProxy, pgBouncer, pgBalancerplProxy, pgBouncer, pgBalancer
plProxy, pgBouncer, pgBalancer
elliando dias
 
High Concurrency Architecture and Laravel Performance Tuning
High Concurrency Architecture and Laravel Performance TuningHigh Concurrency Architecture and Laravel Performance Tuning
High Concurrency Architecture and Laravel Performance Tuning
Albert Chen
 

Tendances (20)

[245] presto 내부구조 파헤치기
[245] presto 내부구조 파헤치기[245] presto 내부구조 파헤치기
[245] presto 내부구조 파헤치기
 
Elephant Roads: a tour of Postgres forks
Elephant Roads: a tour of Postgres forksElephant Roads: a tour of Postgres forks
Elephant Roads: a tour of Postgres forks
 
Spilo, отказоустойчивый PostgreSQL кластер / Oleksii Kliukin (Zalando SE)
Spilo, отказоустойчивый PostgreSQL кластер / Oleksii Kliukin (Zalando SE)Spilo, отказоустойчивый PostgreSQL кластер / Oleksii Kliukin (Zalando SE)
Spilo, отказоустойчивый PostgreSQL кластер / Oleksii Kliukin (Zalando SE)
 
Automating Complex Setups with Puppet
Automating Complex Setups with PuppetAutomating Complex Setups with Puppet
Automating Complex Setups with Puppet
 
Integrated Cache on Netscaler
Integrated Cache on NetscalerIntegrated Cache on Netscaler
Integrated Cache on Netscaler
 
Shootout at the AWS Corral
Shootout at the AWS CorralShootout at the AWS Corral
Shootout at the AWS Corral
 
Shootout at the PAAS Corral
Shootout at the PAAS CorralShootout at the PAAS Corral
Shootout at the PAAS Corral
 
Large Scale Log collection using LogStash & mongoDB
Large Scale Log collection using LogStash & mongoDB Large Scale Log collection using LogStash & mongoDB
Large Scale Log collection using LogStash & mongoDB
 
GUC Tutorial Package (9.0)
GUC Tutorial Package (9.0)GUC Tutorial Package (9.0)
GUC Tutorial Package (9.0)
 
Pragmatic Monolith-First, easy to decompose, clean architecture
Pragmatic Monolith-First, easy to decompose, clean architecturePragmatic Monolith-First, easy to decompose, clean architecture
Pragmatic Monolith-First, easy to decompose, clean architecture
 
How to Monitor MySQL
How to Monitor MySQLHow to Monitor MySQL
How to Monitor MySQL
 
Testing applications with traffic control in containers / Alban Crequy (Kinvolk)
Testing applications with traffic control in containers / Alban Crequy (Kinvolk)Testing applications with traffic control in containers / Alban Crequy (Kinvolk)
Testing applications with traffic control in containers / Alban Crequy (Kinvolk)
 
How to monitor NGINX
How to monitor NGINXHow to monitor NGINX
How to monitor NGINX
 
plProxy, pgBouncer, pgBalancer
plProxy, pgBouncer, pgBalancerplProxy, pgBouncer, pgBalancer
plProxy, pgBouncer, pgBalancer
 
vert.x 소개 및 개발 실습
vert.x 소개 및 개발 실습vert.x 소개 및 개발 실습
vert.x 소개 및 개발 실습
 
ReplacingSquidWithATS
ReplacingSquidWithATSReplacingSquidWithATS
ReplacingSquidWithATS
 
Apache Traffic Server
Apache Traffic ServerApache Traffic Server
Apache Traffic Server
 
SaltConf 2014: Safety with powertools
SaltConf 2014: Safety with powertoolsSaltConf 2014: Safety with powertools
SaltConf 2014: Safety with powertools
 
High Concurrency Architecture and Laravel Performance Tuning
High Concurrency Architecture and Laravel Performance TuningHigh Concurrency Architecture and Laravel Performance Tuning
High Concurrency Architecture and Laravel Performance Tuning
 
Tuning Linux for MongoDB
Tuning Linux for MongoDBTuning Linux for MongoDB
Tuning Linux for MongoDB
 

Similaire à Massively Scaled High Performance Web Services with PHP

AOUG_11Nov2016_Challenges_with_EBS12_2
AOUG_11Nov2016_Challenges_with_EBS12_2AOUG_11Nov2016_Challenges_with_EBS12_2
AOUG_11Nov2016_Challenges_with_EBS12_2
Sean Braymen
 

Similaire à Massively Scaled High Performance Web Services with PHP (20)

Dockerizing Aurea - Docker Con EU 2017
Dockerizing Aurea - Docker Con EU 2017Dockerizing Aurea - Docker Con EU 2017
Dockerizing Aurea - Docker Con EU 2017
 
Criteo Labs Infrastructure Tech Talk Meetup Nov. 7
Criteo Labs Infrastructure Tech Talk Meetup Nov. 7Criteo Labs Infrastructure Tech Talk Meetup Nov. 7
Criteo Labs Infrastructure Tech Talk Meetup Nov. 7
 
Scaling 100PB Data Warehouse in Cloud
Scaling 100PB Data Warehouse in CloudScaling 100PB Data Warehouse in Cloud
Scaling 100PB Data Warehouse in Cloud
 
Monkey Server
Monkey ServerMonkey Server
Monkey Server
 
#RADC4L16: An API-First Archives Approach at NPR
#RADC4L16: An API-First Archives Approach at NPR#RADC4L16: An API-First Archives Approach at NPR
#RADC4L16: An API-First Archives Approach at NPR
 
MySQL X protocol - Talking to MySQL Directly over the Wire
MySQL X protocol - Talking to MySQL Directly over the WireMySQL X protocol - Talking to MySQL Directly over the Wire
MySQL X protocol - Talking to MySQL Directly over the Wire
 
Maximizing PHP Performance with NGINX
Maximizing PHP Performance with NGINXMaximizing PHP Performance with NGINX
Maximizing PHP Performance with NGINX
 
PAC 2019 virtual Mark Tomlinson
PAC 2019 virtual Mark TomlinsonPAC 2019 virtual Mark Tomlinson
PAC 2019 virtual Mark Tomlinson
 
12-Step Program for Scaling Web Applications on PostgreSQL
12-Step Program for Scaling Web Applications on PostgreSQL12-Step Program for Scaling Web Applications on PostgreSQL
12-Step Program for Scaling Web Applications on PostgreSQL
 
AOUG_11Nov2016_Challenges_with_EBS12_2
AOUG_11Nov2016_Challenges_with_EBS12_2AOUG_11Nov2016_Challenges_with_EBS12_2
AOUG_11Nov2016_Challenges_with_EBS12_2
 
Website & Internet + Performance testing
Website & Internet + Performance testingWebsite & Internet + Performance testing
Website & Internet + Performance testing
 
Beginners Node.js
Beginners Node.jsBeginners Node.js
Beginners Node.js
 
Netflix Open Source Meetup Season 4 Episode 2
Netflix Open Source Meetup Season 4 Episode 2Netflix Open Source Meetup Season 4 Episode 2
Netflix Open Source Meetup Season 4 Episode 2
 
Meteor Day Athens (2014-11-07)
Meteor Day Athens (2014-11-07)Meteor Day Athens (2014-11-07)
Meteor Day Athens (2014-11-07)
 
1049: Best and Worst Practices for Deploying IBM Connections - IBM Connect 2016
1049: Best and Worst Practices for Deploying IBM Connections - IBM Connect 20161049: Best and Worst Practices for Deploying IBM Connections - IBM Connect 2016
1049: Best and Worst Practices for Deploying IBM Connections - IBM Connect 2016
 
Profiling and Tuning a Web Application - The Dirty Details
Profiling and Tuning a Web Application - The Dirty DetailsProfiling and Tuning a Web Application - The Dirty Details
Profiling and Tuning a Web Application - The Dirty Details
 
The 5 most common reasons for a slow WordPress site and how to fix them – ext...
The 5 most common reasons for a slow WordPress site and how to fix them – ext...The 5 most common reasons for a slow WordPress site and how to fix them – ext...
The 5 most common reasons for a slow WordPress site and how to fix them – ext...
 
RedisConf18 - Redis on Flash
RedisConf18 - Redis on FlashRedisConf18 - Redis on Flash
RedisConf18 - Redis on Flash
 
Supporting Digital Media Workflows in the Cloud with Perforce Helix
Supporting Digital Media Workflows in the Cloud with Perforce HelixSupporting Digital Media Workflows in the Cloud with Perforce Helix
Supporting Digital Media Workflows in the Cloud with Perforce Helix
 
PyConIE 2017 Writing and deploying serverless python applications
PyConIE 2017 Writing and deploying serverless python applicationsPyConIE 2017 Writing and deploying serverless python applications
PyConIE 2017 Writing and deploying serverless python applications
 

Dernier

在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
ydyuyu
 
( Pune ) VIP Pimpri Chinchwad Call Girls 🎗️ 9352988975 Sizzling | Escorts | G...
( Pune ) VIP Pimpri Chinchwad Call Girls 🎗️ 9352988975 Sizzling | Escorts | G...( Pune ) VIP Pimpri Chinchwad Call Girls 🎗️ 9352988975 Sizzling | Escorts | G...
( Pune ) VIP Pimpri Chinchwad Call Girls 🎗️ 9352988975 Sizzling | Escorts | G...
nilamkumrai
 
➥🔝 7737669865 🔝▻ mehsana Call-girls in Women Seeking Men 🔝mehsana🔝 Escorts...
➥🔝 7737669865 🔝▻ mehsana Call-girls in Women Seeking Men  🔝mehsana🔝   Escorts...➥🔝 7737669865 🔝▻ mehsana Call-girls in Women Seeking Men  🔝mehsana🔝   Escorts...
➥🔝 7737669865 🔝▻ mehsana Call-girls in Women Seeking Men 🔝mehsana🔝 Escorts...
nirzagarg
 
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Chandigarh Call girls 9053900678 Call girls in Chandigarh
 
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
Call Girls In Delhi Whatsup 9873940964 Enjoy Unlimited Pleasure
 
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
nirzagarg
 

Dernier (20)

在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
 
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
 
( Pune ) VIP Pimpri Chinchwad Call Girls 🎗️ 9352988975 Sizzling | Escorts | G...
( Pune ) VIP Pimpri Chinchwad Call Girls 🎗️ 9352988975 Sizzling | Escorts | G...( Pune ) VIP Pimpri Chinchwad Call Girls 🎗️ 9352988975 Sizzling | Escorts | G...
( Pune ) VIP Pimpri Chinchwad Call Girls 🎗️ 9352988975 Sizzling | Escorts | G...
 
Trump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts SweatshirtTrump Diapers Over Dems t shirts Sweatshirt
Trump Diapers Over Dems t shirts Sweatshirt
 
Real Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirtReal Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirt
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53
 
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls DubaiDubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
Dubai=Desi Dubai Call Girls O525547819 Outdoor Call Girls Dubai
 
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
Call Girls Sangvi Call Me 7737669865 Budget Friendly No Advance BookingCall G...
 
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
 
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
Wagholi & High Class Call Girls Pune Neha 8005736733 | 100% Gennuine High Cla...
 
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
 
➥🔝 7737669865 🔝▻ mehsana Call-girls in Women Seeking Men 🔝mehsana🔝 Escorts...
➥🔝 7737669865 🔝▻ mehsana Call-girls in Women Seeking Men  🔝mehsana🔝   Escorts...➥🔝 7737669865 🔝▻ mehsana Call-girls in Women Seeking Men  🔝mehsana🔝   Escorts...
➥🔝 7737669865 🔝▻ mehsana Call-girls in Women Seeking Men 🔝mehsana🔝 Escorts...
 
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
Shikrapur - Call Girls in Pune Neha 8005736733 | 100% Gennuine High Class Ind...
 
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
Low Sexy Call Girls In Mohali 9053900678 🥵Have Save And Good Place 🥵
 
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
 
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
Yerawada ] Independent Escorts in Pune - Book 8005736733 Call Girls Available...
 
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort ServiceBusty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
Busty Desi⚡Call Girls in Vasundhara Ghaziabad >༒8448380779 Escort Service
 
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
valsad Escorts Service ☎️ 6378878445 ( Sakshi Sinha ) High Profile Call Girls...
 
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
💚😋 Bilaspur Escort Service Call Girls, 9352852248 ₹5000 To 25K With AC💚😋
 
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
20240509 QFM015 Engineering Leadership Reading List April 2024.pdf
 

Massively Scaled High Performance Web Services with PHP

  • 1. Massively Scaled High Performance Web Services with PHP Demin Yin https://joind.in/talk/e81e7
  • 2. • Staff Engineer and Technical Lead on Glu Mobile’s Design Home team. • Playing with PHP since 2000. • Worked for a diverse group of companies in China and US, including eBay, Visa and Glu. • Focus on building high performance, secure web services. • Born in China, worked in U.S., live in Canada. • On twitter: @deminy • On Github: https://github.com/deminy About Me 2
  • 3. ● An Overview on Design Home and the Inbox Service ● The Inbox Service: Key Improvements Made 1. Web Server: Dockerized PHP 7 containers 2. HTTP Processing: Background processing, HTTP compression, and more 3. Data Storage: Doing NoSQL with Redis and Couchbase 4. Hardware and Network: Migrating to Amazon Web Services ● Next ● Q&A Agenda 3
  • 4. ● An Overview on Design Home and the Inbox Service ● The Inbox Service: Key Improvements Made 1. Web Server: Dockerized PHP 7 containers 2. HTTP Processing: Background processing, HTTP compression, and more 3. Data Storage: Doing NoSQL with Redis and Couchbase 4. Hardware and Network: Migrating to Amazon Web Services ● Next ● Q&A Agenda 4
  • 5. • Launched in Q4 2016. • #1 Top Free and #7 Top Grossing game in the U.S. App Store for iPhone. • #1 Top Free and #23 Top Grossing game in the U.S. Google Play Store. • 1M+ daily active users; 40M votes daily; 1 billion designs to date. • 100,000 API calls per minute; 60 56 ms response time on average. Design Home: Top-ranked Mobile Game with PHP Backend 5
  • 7. ● REST APIs with different CRUD endpoints to manage inbox messages. ● PHP 7, Nginx, Composer, NoSQL, Docker, AWS, Development Tools, …… ● NoSQL: Redis + Couchbase. ● One of the busiest microservices. Average Requests: 14k rpm Peak Requests: 34k rpm Response Time: less than 9ms The Inbox Service: Technical Overview 7
  • 8. The Inbox Service: Technical Overview (cont.) ● PHP 7. ● Composer. ● Opcache. ● Data cache: APCu, distributed cache and chained cache. ● Tests in place. ● Unit tests. ● Blackbox tests, or feature tests, or functional tests. ● PHP The Right Way: Follow good coding practices including design patterns. 8
  • 9. ● An Overview on Design Home and the Inbox Service ● The Inbox Service: Key Improvements Made 1. Web Server: Dockerized PHP 7 containers 2. HTTP Processing: Background processing, HTTP compression, and more 3. Data Storage: Doing NoSQL with Redis and Couchbase 4. Hardware and Network: Migrating to Amazon Web Services ● Next ● Q&A Agenda 9
  • 10. Hardware & Network ● Migrate to Amazon ECS. ● Hardware upgrade. ● Move everything into VPC. The Inbox Service: Key Improvements Made HTTP Processing ● Use background processing. ● Use HTTP compression (gzip). ● Dynamic content caching. Web Server Instances ● Decouple microservices. ● Dockerize the microservices. ● Development Tools Integration. Data Storage ● NoSQL solutions. ● Fight against IO and disk space. ● Faster PHP drivers. 10
  • 11. ● An Overview on Design Home and the Inbox Service ● The Inbox Service: Key Improvements Made 1. Web Server: Dockerized PHP 7 containers 2. HTTP Processing: Background processing, HTTP compression, and more 3. Data Storage: Doing NoSQL with Redis and Couchbase 4. Hardware and Network: Migrating to Amazon Web Services ● Next ● Q&A Agenda 11
  • 12. 1.1. Decouple Microservices Inbox Localization Receipt Validation Moderation Email E-commerce Event Logging Main Service …… Database Client Client Client Client Client Client Client 12
  • 13. • Improved and simplified development environments. • Easier to upgrade web server and PHP environments. • Easier to troubleshoot and debug issues. 1.2. Dockerized Microservices 13
  • 14. 1.2. Dockerized Microservices (cont.) 14 Base Images HTTP/REST microservices asynchronous microservices job worker instances
  • 15. 1.2. Dockerized Microservices (cont.) Specification: 1. Tags match with PHP versions. 2. Tag latest is never used. 3. Freeze tagged images once used. 4. Images are built and tagged manually. Benefits: 1. Less efforts to build images for different microservices. 2. Easy and safe to upgrade to new versions of PHP. 3. Easier to fix security vulnerabilities. 4. Easier to make improvements across different microservices. 15 Base Images
  • 16. ● Error Handling, Error Reporting and System Monitoring ○ New Relic ○ Bugsnag ○ Amazon CloudWatch ● Security Check ○ SonarQube ○ SensioLabs Security Checker ● Debugging and Profiling: Blackfire, Xdebug, etc. 1.3. Development Tools Integration 16
  • 17. ● An Overview on Design Home and the Inbox Service ● The Inbox Service: Key Improvements Made 1. Web Server: Dockerized PHP 7 containers 2. HTTP Processing: Background processing, HTTP compression, and more 3. Data Storage: Doing NoSQL with Redis and Couchbase 4. Hardware and Network: Migrating to Amazon Web Services ● Next ● Q&A Agenda 17
  • 18. ● Use cases: error handling, data reporting, outgoing emails, push notifications, etc. ● Common approaches: ● Execute external program. ● Register shutdown functions. ● Job server (Gearman, Resque, RabbitMQ, etc.). ● Function fastcgi_finish_request() under PHP-FPM. ● …... 2.1. Background Processing exec("nohup ./http.sh > /dev/null &"); 18
  • 19. 2.1. Background Processing (cont.) Output: 1234 Output: 1 19
  • 20. 2.1. Background Processing (cont.) Average Response Time Without background processing: 13 ms With background processing: 9 ms 20
  • 21. How to prevent and address failed operations properly? ● Basic data validation first. ● Exponential backoff. ● https://github.com/Crowdstar/exponential-backoff ● Proper error reporting. 2.1. Background Processing (cont.) 21
  • 22. For more details, please check • Background Processing: https://github.com/Crowdstar/background-processing • Technical Discussion: https://github.com/deminy/background-processing-in-php 2.1. Background Processing (cont.) 22
  • 25. 2.2. HTTP Compression (cont.) gzip on; gzip_types text/plain text/css application/json text/xml application/xml application/xml+rss text/javascript; 25
  • 26. 2.2. HTTP Compression (cont.) gzip on; gzip_types text/plain text/css application/json text/xml application/xml application/xml+rss text/javascript; gzip_proxied any; 26
  • 27. 2.2. HTTP Compression (cont.) gzip on; gzip_types text/plain text/css application/json text/xml application/xml application/xml+rss text/javascript; gzip_proxied any; gzip_comp_level 5; gzip_min_length 1280; 27
  • 28. 2.2. HTTP Compression (cont.) If HTTP header ‘Content-Length’ is not set in a PHP response, Nginx will always compress the response even its length is less than the minimum length. • Slim 3 always has HTTP header ‘Content-Length’ set by default. • Laravel and Lumen don’t have HTTP header ‘Content-Length’ set. 28
  • 29. 2.2. HTTP Compression (cont.) A Lumen Middleware to Make HTTP Compression Work as Should 29
  • 30. 2.3. Dynamic Content Caching Challenge: feed new data to the client. Possible Solutions: ● Server push, but it’s not supported in HTTP 1 (It’s supported under HTTP 2 and the WebSocket API). ● HTTP Clients built on top of HTTP 1 have to hit servers again and again to check and fetch new data. Our Approach: ● Data cache on the server side if OK. ● Use the Last-Modified and If-Modified-Since headers, and return HTTP 304 "Not Modified" if no need to retransmit the requested resources. 30
  • 31. 2.3. Dynamic Content Caching (cont.) Related HTTP headers: ● If-None-Match, If-Unmodified-Since ● Cache-Control ● Content-Location ● ETag ● Expires ● Vary ● …... 31
  • 32. ● An Overview on Design Home and the Inbox Service ● The Inbox Service: Key Improvements Made 1. Web Server: Dockerized PHP 7 containers 2. HTTP Processing: Background processing, HTTP compression, and more 3. Data Storage: Doing NoSQL with Redis and Couchbase 4. Hardware and Network: Migrating to Amazon Web Services ● Next ● Q&A Agenda 32
  • 33. ● Lots of choices: Redis, Couchbase, Aerospike, MongoDB, … ● Our selection and implementation: Redis and Couchbase. ● Redis. ● Couchbase. ● Concerns with NoSQL ○ limitations on queries. ○ indexes. ○ serialization and compression. 3.1. NoSQL 33
  • 34. • Data compression to reduce network IO and save disk space. • Compress larger messages before saving. • Couchbase encoder (serialization). • Use message templates. • Shorten field names. • Add ttl (time to live) on certain messages (to expire messages). 3.2. Fight Against IO and Disk Space 34
  • 35. • PhpRedis: the PHP extension for Redis. • Predis: the Redis client written in PHP. • Other drivers • Package predis/predis-async: asynchronous (non-blocking) version of Predis built on top of ReactPHP. • Class SwooleCoroutineRedis: the asynchronous Redis Client from Swoole. 3.3. Faster PHP Drivers (Redis) 35
  • 36. 3.3. Faster PHP Drivers (Redis) Before and after switching from Predis to PhpRedis Average request time reduced from 16 ms to 11 ms 36
  • 37. ● An Overview on Design Home and the Inbox Service ● The Inbox Service: Key Improvements Made 1. Web Server: Dockerized PHP 7 containers 2. HTTP Processing: Background processing, HTTP compression, and more 3. Data Storage: Doing NoSQL with Redis and Couchbase 4. Hardware and Network: Migrating to Amazon Web Services ● Next ● Q&A Agenda 37
  • 38. 4.1. Deployment with Amazon ECS How did we do code deployment before? 38
  • 39. Benefits: • Zero downtime deployments. • Quick rollback. • ELB health checks. • Autoscaling. 4.1. Deployment with Amazon ECS (cont.) 39
  • 40. • Multiple deployment environments in AWS: test, dev, RC, staging, and production. • Staging environment is helpful. • to do some final tests before major production deployments. • to reproduce and identify production issues. 4.1. Deployment with Amazon ECS (cont.) 40
  • 41. 4.2. Hardware Upgrade Feb 2018: upgraded API servers with faster ones (c5.xlarge: 4 vCPU, 8GB memory; Enhanced networking enabled) 41
  • 42. • Place instances closer to each other. • Move instances to same locations (Availability Zones). • Use internal network instead of Internet to communicate. 4.3. Move Everything into VPC 42
  • 43. 4.3. Move Everything into VPC (cont.) Jan 2019: upgraded API servers with faster ones that were closer to the database nodes (same zone) In the main service, Couchbase operation time decreased to 5 ms 43
  • 44. ● An Overview on Design Home and the Inbox Service ● The Inbox Service: Key Improvements Made 1. Web Server: Dockerized PHP 7 containers 2. HTTP Processing: Background processing, HTTP compression, and more 3. Data Storage: Doing NoSQL with Redis and Couchbase 4. Hardware and Network: Migrating to Amazon Web Services ● Next ● Q&A Agenda 44
  • 45. There are always rooms to push the limit of PHP 45
  • 46. • PHP-FPM • Asynchronous I/O • Swoole • Network Protocols • HTTP/2 • Tars Next 46
  • 47. ● An Overview on Design Home and the Inbox Service ● The Inbox Service: Key Improvements Made 1. Web Server: Dockerized PHP 7 containers 2. HTTP Processing: Background processing, HTTP compression, and more 3. Data Storage: Doing NoSQL with Redis and Couchbase 4. Hardware and Network: Migrating to Amazon Web Services ● Next ● Q&A Agenda 47