$30 off During Our Annual Pro Sale. View Details »

SLAC17: PHP On Scale

SLAC17: PHP On Scale

License: CC BY-SA

Slides of my talk at the Secure Linux Administration Conference in Berlin at May 23rd, 2017.

Martin Helmich

May 23, 2017
Tweet

More Decks by Martin Helmich

Other Decks in Programming

Transcript

  1. 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

    View Slide

  2. 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/

    View Slide

  3. 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

    View Slide

  4. View Slide

  5. View Slide

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

    View Slide

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

    View Slide

  8. View Slide

  9. View Slide

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

    View Slide

  11. View Slide

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

    View Slide

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

    View Slide

  14. View Slide

  15. <
    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

    View Slide

  16. 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

    View Slide

  17. 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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  21. View Slide

  22. View Slide

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

    View Slide

  24. PHP-Applikation
    Datenbank

    View Slide

  25. 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/

    View Slide

  26. Redis
    memcached APC
    XCache

    View Slide

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

    View Slide

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

    View Slide

  29. 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();
    }
    }

    View Slide

  30. 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

    View Slide

  31. PHP-Applikation
    Datenbank

    View Slide

  32. 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

    View Slide

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

    View Slide

  34. 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

    View Slide

  35. View Slide

  36. Server
    Server
    Server
    Server
    LB Datenbank
    HAProxy
    NGINX
    Traefik
    Server

    View Slide

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

    View Slide

  38. View Slide

  39. Server
    Server
    Datenbank
    LB

    View Slide

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

    View Slide

  41. Server
    Server
    Request
    Datenbank
    LB

    View Slide

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

    View Slide

  43. 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

    View Slide

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

    View Slide

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

    View Slide

  46. Server
    Server
    Request
    Datenbank
    LB
    Session-Daten
    Session Store

    View Slide

  47. View Slide

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

    View Slide

  49. [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!

    View Slide

  50. Server
    Server
    LB Datenbank
    Filesystem?

    View Slide

  51. LB Datenbank
    Server
    Server
    NFS

    View Slide

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

    View Slide

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

    View Slide

  54. LB Datenbank
    Server
    Server
    NGINX
    Varnish
    Cloudfront
    Cloudflare
    Edge Cache

    View Slide

  55. PHP-Applikation
    Datenbank
    APCu

    View Slide

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

    View Slide

  57. PHP-Applikation
    Datenbank
    Redis
    PHP-Applikation
    PHP-Applikation
    Scale-Out

    View Slide

  58. View Slide

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

    View Slide

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

    View Slide

  61. App App
    DB
    A
    Cache
    APC APC APC
    App

    View Slide

  62. App App
    DB
    A
    Cache
    APC APC APC
    A
    A
    App

    View Slide

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

    View Slide

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

    View Slide

  65. 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)
    ]);

    View Slide

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

    View Slide

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

    View Slide

  68. View Slide

  69. View Slide

  70. 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

    View Slide

  71. View Slide

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

    View Slide

  73. 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

    View Slide

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

    View Slide

  75. 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

    View Slide

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

    View Slide

  77. 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, …)?

    View Slide

  78. https://karriere.mittwald.de

    View Slide