Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

Agenda 1. Motivation 2. What are gRPC and Protocol Buffers? 3. PHP as gRPC Server? 4. gRPC in Action (code examples included)

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

Invoicing Analytics Project Management

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

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)

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

gRPC Performance HTTP/2 Bene fi ts of HTTP/2 Multiplexing Streams Binary protocol Server push

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Official libraries 10

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

gRPC Use Cases • Microservices • Cloud Native Applications • High-performance APIs • IoT and Mobile Applications • Inter-service communication • Real time communication 12

Slide 13

Slide 13 text

13 What about web?

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

No content

Slide 16

Slide 16 text

gRPC Server gRPC Client Stub Protocol Buffers

Slide 17

Slide 17 text

• 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

Slide 18

Slide 18 text

User Message De fi nition

Slide 19

Slide 19 text

Documentation pseudomuto/protoc-gen-doc

Slide 20

Slide 20 text

Evolve messages / Add fi eld

Slide 21

Slide 21 text

Evolve messages / Remove fi eld

Slide 22

Slide 22 text

Evolve messages / Remove fi eld

Slide 23

Slide 23 text

User Message De fi nition

Slide 24

Slide 24 text

Additional De fi nitions

Slide 25

Slide 25 text

Service De fi nition

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

Roadrunner

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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"}

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

Generated Clases

Slide 36

Slide 36 text

Implement the service

Slide 37

Slide 37 text

Con fi gure gRPC in Roadrunner

Slide 38

Slide 38 text

Con fi gure gRPC in Roadrunner

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

Generated Clases

Slide 46

Slide 46 text

Send the request

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

Mooooment, da habe ich eine Frage! Questions