Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Serverless PHP @ PHPCon Poland 2019

Serverless PHP @ PHPCon Poland 2019

#serverless nie był do tej pory gorącym tematem w świecie PHP, ponieważ żaden z dostawców usług chmurowych nie wspiera natywnie PHP w środowisku tego typu. Pod koniec zeszłego roku AWS udostępniło dwie nowe usługi (Lambda Runtime API oraz Lambda Layers), które pozwalają na samodzielne dodanie PHP do środowiska typu #serveless. Podczas prezentacji omówię poszczególne elementy takiej architektury oraz pokażę kod PHP działąjacy na AWS Lambda.

Sebastian Grodzicki

November 16, 2019
Tweet

More Decks by Sebastian Grodzicki

Other Decks in Programming

Transcript

  1. Serverless PHP
    Sebastian Grodzicki @ PHPCon Poland 2019
    @sebgrodzicki

    View full-size slide

  2. 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019

    View full-size slide

  3. 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019

    View full-size slide

  4. phpinfo();
    Sebastian Grodzicki

    • Engineering Manager at
    • ex-CTO at SHOWROOM & GoldenLine
    • PHP developer for 15+ years
    • #serverless
    @sebgrodzicki

    View full-size slide

  5. Serverless
    • storage as a service (e.g. Amazon S3),
    • database as a service (e.g. Amazon Aurora),
    • message queue as a service (e.g. Amazon SQS),
    • etc.

    View full-size slide

  6. Serverless replaces the traditional
    approaches to running applications.

    View full-size slide

  7. Platform options
    Physical servers

    View full-size slide

  8. Platform options
    Physical servers
    Virtual machines

    View full-size slide

  9. Platform options
    Physical servers
    Virtual machines
    Containers

    View full-size slide

  10. Platform options
    Physical servers
    Virtual machines
    Containers
    Platform

    View full-size slide

  11. Platform options
    Physical servers
    Virtual machines
    Containers
    Platform
    Serverless

    View full-size slide

  12. Platform options
    Physical servers
    Virtual machines
    Containers
    Platform
    Serverless
    Abstraction

    View full-size slide

  13. Platform options
    Physical servers
    Virtual machines
    Containers
    Platform
    Serverless
    Abstraction Control

    View full-size slide

  14. Why serverless?

    View full-size slide

  15. Why serverless?
    • We don't manage, update, configure, provision servers or
    containers,

    View full-size slide

  16. Why serverless?
    • We don't manage, update, configure, provision servers or
    containers,
    • We don't reserve or scale servers or containers, instead
    they are scaled automatically and transparently for us,

    View full-size slide

  17. Why serverless?
    • We don't manage, update, configure, provision servers or
    containers,
    • We don't reserve or scale servers or containers, instead
    they are scaled automatically and transparently for us,
    • We don't pay for fixed resources, instead we pay for what
    we actually use (e.g. execution time).

    View full-size slide

  18. Serverless can provide more scalable, affordable
    and reliable architectures for less effort.

    View full-size slide

  19. Data center evolution
    DC

    View full-size slide

  20. Data center evolution
    DC

    View full-size slide

  21. Data center evolution
    DC

    View full-size slide

  22. Data center evolution
    DC
    VM VM
    VM
    VM
    VM VM
    VM
    VM
    VM VM
    VM
    VM

    View full-size slide

  23. Data center evolution
    DC
    VM
    C C
    C C
    C C
    VM
    C C
    C C
    C C
    VM
    C C
    C C
    C C
    VM
    C C
    C C
    C C
    VM
    C C
    C C
    C C
    VM
    C C
    C C
    C C
    VM
    C C
    C C
    C C
    VM
    C C
    C C
    C C
    VM
    C C
    C C
    C C
    VM
    C C
    C C
    C C
    VM
    C C
    C C
    C C
    VM
    C C
    C C
    C C

    View full-size slide

  24. Data center evolution

    DNS

    CDN

    View full-size slide

  25. Data center evolution
    PaaS
    IaaS SaaS

    View full-size slide

  26. Function as a Service (FaaS)

    View full-size slide

  27. FaaS
    AWS Lambda
    Microsoft Azure Functions Google Cloud Functions

    View full-size slide

  28. FaaS
    AWS Lambda

    View full-size slide

  29. $ composer require bref/bref
    CLI

    View full-size slide

  30. $ composer require bref/bref
    $ vendor/bin/bref init
    CLI

    View full-size slide


  31. echo 'Hello world!';
    index.php

    View full-size slide

  32. service: app


    provider:

    name: aws

    region: us-east-1

    runtime: provided


    plugins:

    - ./vendor/bref/bref


    functions:

    api:

    handler: index.php

    description: ''

    timeout: 28 # in seconds (API Gateway has a timeout of 29
    seconds)

    layers:

    - ${bref:layer.php-73-fpm}

    events:

    - http: 'ANY /'

    - http: 'ANY /{proxy+}'
    serverless.yml

    View full-size slide

  33. $ composer require bref/bref
    $ vendor/bin/bref init


    $ serverless deploy
    CLI

    View full-size slide


  34. phpinfo();
    index.php

    View full-size slide

  35. $ serverless invoke -f function
    "Hello world"
    CLI

    View full-size slide

  36. $ serverless invoke -f function -l


    "Hello world"


    ------------------------------------------------------

    START RequestId: 71dd0550-46b4-4f13-8438-8f687f385d0c
    Version: $LATEST


    END RequestId: 71dd0550-46b4-4f13-8438-8f687f385d0c


    REPORT

    RequestId: 71dd0550-46b4-4f13-8438-8f687f385d0c
    Duration: 14.20 ms

    Billed Duration: 100 ms

    Memory Size: 1024 MB

    Max Memory Used: 67 MB
    CLI

    View full-size slide


  37. require __DIR__.'/vendor/autoload.php';


    lambda(function (array $event) {

    return 'Hello ' . ($event['name'] ?? 'world');

    });
    index.php

    View full-size slide

  38. $ serverless invoke \

    > --function function \

    > --data='{"name":"PHPCon Poland"}'


    "Hello PHPCon Poland"
    CLI

    View full-size slide

  39. Benchmarks: Cold starts
    128 512 1024 2048
    PHP function 500 ms 240 ms 230 ms 220 ms
    HTTP
    application
    345 ms 250 ms 240 ms 240 ms
    Symfony 5.4 s 1.4 s 800 ms 500 ms

    View full-size slide

  40. Benchmarks: Average (warm) execution time
    128 512 1024 2048
    PHP function 175 ms 35 ms 16 ms 13 ms
    HTTP
    application
    10 ms 1 ms 1 ms 1 ms
    Symfony 58 ms 4 ms 4 ms 4 ms

    View full-size slide

  41. Benchmarks: CPU performance
    128 512 1024 2048
    bench.php 5.7 s 1.4 s 0.65 s 0.33 s

    View full-size slide

  42. Maturity matrix
    Simplicity Performances Reliability
    Jobs, Cron ⬤ ⬤ ⬤
    API ⬤ ⬤ ⬤
    API with MySQL /
    PostgreSQL
    ⬤ ⬤ ⬤
    Website ⬤ ⬤ ⬤
    Website with MySQL /
    PostgreSQL
    ⬤ ⬤ ⬤
    Legacy application ⬤ ⬤ ⬤

    View full-size slide

  43. Who uses Laravel?
    ȱȱȱȱȱȱȱȱȱȱ

    View full-size slide

  44. Laravel Vapor

    View full-size slide

  45. Laravel Vapor

    View full-size slide