Slide 1

Slide 1 text

PHP on Scale Martin Helmich Mittwald CM Service GmbH & Co. KG 10. Secure Linux Administration Conference, Berlin 23. Mai 2017 CC-0, coffy https://pixabay.com/de/thailand-elefanten-sonnenuntergang-142982

Slide 2

Slide 2 text

Dieses Werk ist lizensiert unter einer Creative Commons (Namensnennung & Weitergabe unter gleichen Bedingungen) 4.0 International Lizenz. http://creativecommons.org/licenses/by-sa/4.0/

Slide 3

Slide 3 text

Martin Helmich Software- & Systemarchitekt bei Mittwald CM Service Dozent (Private Hochschule für Wirtschaft & Technik, Hochschule Hamm-Lippstadt) http://stackoverflow.com/story/martinhelmich https://github.com/martin-helmich https://www.martin-helmich.de

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Parse Error: syntax error, unexpected T_PAAMAYIM_NEKUDOTAYIM פעמיים נקודתיים = "Doppelter Doppelpunkt"

Slide 7

Slide 7 text

CC-0, Michael Gaida https://pixabay.com/de/hafen-binnenhafen-rhein-kran-1569694

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

82,6 % https://w3techs.com/technologies/overview/programming_language/all

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

+100ms Seitenaufbau -1% Umsatz https://blog.gigaspaces.com/amazon-found-every-100ms-of-latency-cost-them-1-in-sales/

Slide 13

Slide 13 text

+500ms Seitenaufbau -20% Traffic https://blog.gigaspaces.com/amazon-found-every-100ms-of-latency-cost-them-1-in-sales/

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

< Erste Regel für PHP-Performance: „Neuer ist besser!“ CC BY-SA, Grm https://commons.wikimedia.org/wiki/File:Macintosh_128k_transparency.png CC BY-SA, Sonicscrewdriver https://commons.wikimedia.org/wiki/File:Late_2016_MacBook_Pro.jpg

Slide 16

Slide 16 text

68,69 TPS 167,87 TPS 163,48 TPS 0 50 100 150 200 PHP 5.6 PHP 7.0 PHP 7.1 Transactions per Second Quelle: https://github.com/martin-helmich/php7-benchmark Mehr = Besser Zutaten • Wordpress 4.7.4 • MariaDB 10.1 • Siege mit 100 parallelen Nutzern

Slide 17

Slide 17 text

68,69 TPS 167,87 TPS 163,48 TPS 0 50 100 150 200 PHP 5.6 PHP 7.0 PHP 7.1 Transactions per Second Quelle: https://github.com/martin-helmich/php7-benchmark Mehr = Besser 1,44 s 0,59 s 0,60 s 0 0,5 1 1,5 2 PHP 5.6 PHP 7.0 PHP 7.1 Average Response Time Weniger = Besser

Slide 18

Slide 18 text

http://php.net/manual/en/migration70.php

Slide 19

Slide 19 text

Zweite Regel für PHP-Performance: Engpässe identifizieren

Slide 20

Slide 20 text

xdebug.profiler_enable=1 xdebug.profiler_output_dir=/var/lib/xdebug https://xdebug.org/docs/profiler

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

CC-0 hpgruesen https://pixabay.com/de/schienenwege-streckengleise-1377329/

Slide 24

Slide 24 text

PHP-Applikation Datenbank

Slide 25

Slide 25 text

PHP-Applikation Datenbank Cache PHP-Applikation Datenbank Cache CC-BY, Lauris Rubenis https://www.flickr.com/photos/beardforgood/9186992422 CC-0, PublicDomainPictures https://pixabay.com/de/schnecke-weichtier-schale-1959/

Slide 26

Slide 26 text

Redis memcached APC XCache

Slide 27

Slide 27 text

function getExpensiveValue() { $cacheValue = apc_fetch("my_expensive_value", $success); if ($success) { return $success; } $value = computeExpensiveValue(); apc_store("my_expensive_value", $value, 3600); return $value; }

Slide 28

Slide 28 text

PSR-6: Caching Interface http://www.php-fig.org/psr/psr-6/

Slide 29

Slide 29 text

class Foo { private $cache; public __construct(CacheItemPoolInterface $cache) { $this->cache = $cache; } public getExpensiveValue() { $item = $this->cache->getItem("my_expensive_value"); if (!$item->isHit()) { $item->set($this->computeExpensiveValue()); $this->cache->save($item); } return $item->get(); } }

Slide 30

Slide 30 text

PSR 6-Implementierungen: • cache/array-adapter • cache/memcached-adapter • cache/filesystem-adapter • cache/redis-adapter • cache/apcu-adapter https://packagist.org/providers/psr/cache-implementation

Slide 31

Slide 31 text

PHP-Applikation Datenbank

Slide 32

Slide 32 text

CC-BY, Lauris Rubenis https://www.flickr.com/photos/beardforgood/9186992422 CC-0, PublicDomainPictures https://pixabay.com/de/schnecke-weichtier-schale-1959/ PHP-Applikation Datenbank Cache PHP-Applikation Datenbank Cache NGINX Varnish Cloudfront Cloudflare

Slide 33

Slide 33 text

CC-0 hpgruesen https://pixabay.com/de/schienenwege-streckengleise-1377329/

Slide 34

Slide 34 text

Dritte Regel für PHP-Performance: „Viel hilft viel!“ CC BY-SA Victorgrigas https://commons.wikimedia.org/wiki/File:Wikimedia_Foundation_Servers-8055_35.jpg

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

Server Server Server Server LB Datenbank HAProxy NGINX Traefik Server

Slide 37

Slide 37 text

CC-0, julielines https://pixabay.com/de/elefant-s%C3%BCdafrika-kruger-erhaltung-765735/

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Server Server Datenbank LB

Slide 40

Slide 40 text

Server Server „Login!“ Datenbank LB Cookie Session-Daten

Slide 41

Slide 41 text

Server Server Request Datenbank LB

Slide 42

Slide 42 text

[Session] session.save_handler = files session.save_path = "/var/lib/php7/sessions"

Slide 43

Slide 43 text

backend my_application balance source hash-type consistent server backend01 10.0.0.9:80 check server backend02 10.0.0.10:80 check server backend03 10.0.0.11:80 check Source Address Affinity

Slide 44

Slide 44 text

CC-0, maxmann https://pixabay.com/de/waage-apfel-gewichtskontrolle-1155878/

Slide 45

Slide 45 text

Server Server „Login!“ Datenbank LB Cookie Session-Daten Session Store

Slide 46

Slide 46 text

Server Server Request Datenbank LB Session-Daten Session Store

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

[Session] -session.save_handler = files -session.save_path = "/var/lib/php7/sessions" +session.save_handler = redis +session.save_path = "tcp://:6379"

Slide 49

Slide 49 text

[Session] -session.save_path = "tcp://:6379" +session.save_path = "tls://:6379?auth=" Security-Tipp: Verbindung über reines TCP nur in vertrauenswürdigen Netzen! Ansonsten TLS-Proxy (HAProxy, Hitch, …) und Passwort-Authentifizierung verwenden!

Slide 50

Slide 50 text

Server Server LB Datenbank Filesystem?

Slide 51

Slide 51 text

LB Datenbank Server Server NFS

Slide 52

Slide 52 text

LB Datenbank Server Server Object Storage S3, Ceph, Swift, Minio, …

Slide 53

Slide 53 text

Server Server Server Server Server LB Datenbank DFS DFS DFS DFS DFS GlusterFS, CephFS, XtreemFS, …

Slide 54

Slide 54 text

LB Datenbank Server Server NGINX Varnish Cloudfront Cloudflare Edge Cache

Slide 55

Slide 55 text

PHP-Applikation Datenbank APCu

Slide 56

Slide 56 text

PHP-Applikation Datenbank APCu PHP-Applikation APCu PHP-Applikation APCu Scale-Out Achtung! Inkonsistente Caches!

Slide 57

Slide 57 text

PHP-Applikation Datenbank Redis PHP-Applikation PHP-Applikation Scale-Out

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

Cache Chaining CC BY-SA, Paul Maenz https://commons.wikimedia.org/wiki/File:Lima_Eimerkette_141207_Pressefoto.jpg

Slide 60

Slide 60 text

PHP-Applikation Datenbank APCu PHP-Applikation APCu PHP-Applikation APCu Scale-Out Redis

Slide 61

Slide 61 text

App App DB A Cache APC APC APC App

Slide 62

Slide 62 text

App App DB A Cache APC APC APC A A App

Slide 63

Slide 63 text

App App DB Cache A APC A APC A APC A A B App B B Achtung! Inkonsistente Caches!

Slide 64

Slide 64 text

App App DB Cache B APC A APC A APC B B App ttl=60 ttl=60 ttl=60 ttl=86400 ttl=∞

Slide 65

Slide 65 text

Cache Chaining Zum Zuhause nachmachen: composer require \ cache/apcu-adapter \ cache/redis-adapter \ cache/chain-adapter $redis = new Redis(); $redis->connect( '', 6379 ); $pool = new CachePoolChain([ new ApcuCachePool(), new RedisCachePool($redis) ]);

Slide 66

Slide 66 text

„Was ist eigentlich mit HHVM?“ CC-0, Yummymoon https://pixabay.com/de/meerkat-blick-beobachter-natur-s%C3%BC%C3%9F-968862/

Slide 67

Slide 67 text

Lexing Parsing Compilation Opcode Cache PHP-Opcodes Opcodes werden vom PHP-Interpreter ausgeführt Ausführung von PHP-Code ~ Traditionell ~ Compilation

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

Lexing Parsing Compilation Compilation JIT- Compiler HipHop- Bytecode Ausführung als nativer Maschinencode Ausführung von PHP-Code ~ HipHop VM ~ Code- Repository AST HipHop intermediate representation x86-64/ARM- Maschinencode

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

https://speakerdeck.com/martinhelmich/inspiringcon14-elephpants-on-speed-running-typo3-flow-on-hiphop-vm

Slide 73

Slide 73 text

68,69 TPS 167,87 TPS 163,48 TPS 171,95 TPS 0 50 100 150 200 PHP 5.6 PHP 7.0 PHP 7.1 HHVM 3.19 PHP 5.6 PHP 7.0 PHP 7.1 HHVM 3.19 Quelle: https://github.com/martin-helmich/php7-benchmark 1,44 s 0,59 s 0,60 s 0,58 s 0 0,5 1 1,5 2 PHP 5.6 PHP 7.0 PHP 7.1 HHVM 3.19 PHP 5.6 PHP 7.0 PHP 7.1 HHVM 3.19

Slide 74

Slide 74 text

https://speakerdeck.com/martinhelmich/inspiringcon14-elephpants-on-speed-running-typo3-flow-on-hiphop-vm

Slide 75

Slide 75 text

287,92 TPS 335,13 TPS 794,20 TPS 917,10 TPS 1739,28 TPS 1363,24 TPS 1123,41 TPS 0 200 400 600 800 1000 1200 1400 1600 1800 2000 PHP 5.6 PHP 7.0 HHVM 3.10 Laravel Drupal 8 Wordpress 4.4 Quelle: https://kinsta.com/blog/the-definitive-php-7-final-version-hhvm-benchmark

Slide 76

Slide 76 text

CC-0, Three-Shots https://pixabay.com/de/elefant-staub-road-afrika-savannah-2066383/

Slide 77

Slide 77 text

Martins PHP-Performance- Checkliste • Neueste Software-Versionen im Einsatz? • OP-Cache aktiv? • Profiling durchgeführt? • Datenbank-Struktur optimiert? • Software- und Systemarchitektur optimiert (z.B: Caching, CQRS, …)?

Slide 78

Slide 78 text

https://karriere.mittwald.de