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

Building a blazing fast gRPC Service with PHP

Building a blazing fast gRPC Service with PHP

Building a blazing fast gRPC Service with PHP using Roadrunner and the Spiral Framework.
Held on the Neos CMS conference 2023

Daniel Lienert

April 28, 2023
Tweet

More Decks by Daniel Lienert

Other Decks in Programming

Transcript

  1. Building a blazing fast gRPC
    Service with PHP
    Daniel Lienert, 28.04.2023

    View Slide

  2. Agenda
    1. Motivation


    2. What are gRPC and Protocol Buffers?


    3. PHP as gRPC Server?


    4. gRPC in Action (code examples included)

    View Slide

  3. View Slide

  4. Invoicing
    Analytics
    Project Management

    View Slide

  5. View Slide

  6. Local Function Call vs. Remote Procedure Call (RPC)
    6
    Server A
    Login
    Server B
    User Management
    Process
    Login
    User Management
    Local function call Remote procedure call (RPC)

    View Slide

  7. View Slide

  8. gRPC Performance
    HTTP/2
    Bene
    fi
    ts of HTTP/2


    Multiplexing


    Streams


    Binary protocol


    Server push

    View Slide

  9. Flow Types
    9
    Unary
    Client
    Server
    Server Streaming
    Client
    Server
    Client Streaming
    Client
    Server
    Bi-drectional Streaming
    Client
    Server

    View Slide

  10. Official libraries
    10

    View Slide

  11. gRPC vs SOAP (and REST)
    11
    Protocol Bene
    fi
    ts Drawbacks
    gRPC - High performant


    - Multiplexing


    - Binary protocol - smaller and faster


    - Stream processing


    - API oriented (no constraints)
    - Limited tooling and documentation compared to
    REST and SOAP


    - Requires HTTP/2 support
    SOAP - Established and widely adopted


    - Tooling and documentation available


    - API oriented (no constraints)
    - Bulky Text-based protocol slow and ine
    ff
    i
    cient


    - Complex and verbose syntax


    - Request / Response only


    - Heavyweight
    REST - Widely adopted and established


    - Simple and easy to use


    - Tooling and documentation available
    - Text-based protocol slower and less e
    ff
    i
    cient


    - Request / Response only


    - Requires additional e
    f
    fort for error handling


    - CRUD oriented

    View Slide

  12. gRPC Use Cases
    • Microservices


    • Cloud Native Applications


    • High-performance APIs


    • IoT and Mobile Applications


    • Inter-service communication


    • Real time communication
    12

    View Slide

  13. 13
    What about web?

    View Slide

  14. 14
    gRPC Web
    Browser
    gRPC WebApp
    Proxy
    Envoy
    Server
    gRPC Server
    HTTP HTTP/2

    View Slide

  15. View Slide

  16. gRPC Server gRPC Client Stub
    Protocol Buffers

    View Slide

  17. • Data is fully typed


    • Data efficient binary format


    • Encoding / decoding data is fast


    • Data can be read across any language


    • Embedded Documentation


    • Safe Schema Evolution
    protobuf
    Advantages

    View Slide

  18. User Message De
    fi
    nition

    View Slide

  19. Documentation
    pseudomuto/protoc-gen-doc

    View Slide

  20. Evolve messages / Add
    fi
    eld

    View Slide

  21. Evolve messages / Remove
    fi
    eld

    View Slide

  22. Evolve messages / Remove
    fi
    eld

    View Slide

  23. User Message De
    fi
    nition

    View Slide

  24. Additional De
    fi
    nitions

    View Slide

  25. Service De
    fi
    nition

    View Slide

  26. View Slide

  27. Roadrunner

    View Slide

  28. View Slide

  29. Initialization
    Common Libraries


    User Libraries


    Cache Warmup


    Entry Point


    Routing


    Domain Logic


    Response
    Return and Exit


    Classic Request Lifecycle
    Con
    fi
    guration
    Bootstrapping
    Actual Work
    Roadrunner handled requests

    View Slide

  30. Initialization
    Common Libraries


    User Libraries


    Cache Warmup


    Entry Point


    Routing


    Domain Logic


    Response
    Return and Exit


    Classic Request Lifecycle
    Con
    fi
    guration
    Bootstrapping
    Actual Work
    Roadrunner handled requests
    2023-04-25T23:46:09.670+0200 DEBUG grpc method was called successfully {"method": "/app.neoscon.UserService/Search", "start": "2023-04-25T23:46:09.640+0200", "elapsed": "30.629458ms"}


    2023-04-25T23:46:16.459+0200 DEBUG grpc method was called successfully {"method": "/app.neoscon.UserService/Search", "start": "2023-04-25T23:46:16.458+0200", "elapsed": "992.041µs"}


    2023-04-25T23:46:18.806+0200 DEBUG grpc method was called successfully {"method": "/app.neoscon.UserService/Search", "start": "2023-04-25T23:46:18.805+0200", "elapsed": "526.459µs"}


    2023-04-25T23:46:32.023+0200 DEBUG grpc method was called successfully {"method": "/app.neoscon.UserService/Search", "start": "2023-04-25T23:46:32.022+0200", "elapsed": "895.875µs"}


    View Slide

  31. HTTP
    gRPC
    Jobs
    KV Store
    Status
    Metrics
    Real Time Messaging
    Cron worker

    View Slide

  32. View Slide

  33. View Slide

  34. View Slide

  35. Generated Clases

    View Slide

  36. Implement the service

    View Slide

  37. Con
    fi
    gure gRPC in Roadrunner

    View Slide

  38. Con
    fi
    gure gRPC in Roadrunner

    View Slide

  39. View Slide

  40. go install github.com/fullstorydev/grpcui/cmd/grpcui@latest
    grpcui --plaintext -import-path ./proto/ -proto service.proto localhost:50051
    Web UI

    View Slide

  41. View Slide

  42. View Slide

  43. View Slide

  44. protoc --proto_path=Resources/Private/Proto \


    --php_out=Classes \


    --grpc_out=Classes \


    --plugin=protoc-gen-grpc=grpc_php_plugin \


    ./Resources/Private/Proto/service.proto


    Generate Client Stub Code

    View Slide

  45. Generated Clases

    View Slide

  46. Send the request

    View Slide

  47. Building a blazing fast gRPC
    Service with PHP
    Daniel Lienert, 28.04.2023

    View Slide

  48. Mooooment, da habe
    ich eine Frage!
    Questions

    View Slide