Slide 1

Slide 1 text

FROM MONOLITH TO MICROSERVICE Modernizing legacy codebases with grpc + go

Slide 2

Slide 2 text

(or gRPC for dummies)

Slide 3

Slide 3 text

Hi, I’m Cecy Correa @cecycorrea Software Engineer, Context.IO

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

gRPC to break up your monolith!

Slide 6

Slide 6 text

what is gRPC?

Slide 7

Slide 7 text

gRPC Remote Procedure Calls

Slide 8

Slide 8 text

oh ok

Slide 9

Slide 9 text

what is RPC?

Slide 10

Slide 10 text

Data exchange between 2 processes

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

so an API?

Slide 13

Slide 13 text

RPC or REST?

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

The “RPC” part stands for “remote procedure call,” and it’s essentially the same as calling a function in JavaScript, PHP, Python and so on, taking a method name and arguments. Source: Smashing Magazine

Slide 16

Slide 16 text

RPC == good for actions

Slide 17

Slide 17 text

REST == CRUD & modeling your domain

Slide 18

Slide 18 text

[REST] GET users/:id/photos/:photo_id

Slide 19

Slide 19 text

[RPC] getUserPhotos(args...)

Slide 20

Slide 20 text

Why gRPC?

Slide 21

Slide 21 text

Why gRPC? ● Use HTTP/2

Slide 22

Slide 22 text

Why gRPC? ● Use HTTP/2 ● Language agnostic

Slide 23

Slide 23 text

Why gRPC? ● Use HTTP/2 ● Language agnostic ● Highly efficient / scalable

Slide 24

Slide 24 text

Why gRPC? ● Use HTTP/2 ● Language agnostic ● Highly efficient / scalable ● Handle large amounts of data

Slide 25

Slide 25 text

HTTP2!

Slide 26

Slide 26 text

HTTP2!

Slide 27

Slide 27 text

HTTP2! ● Binary: moar data! Moar efficient!

Slide 28

Slide 28 text

HTTP2! ● Binary: moar data! Moar efficient! ● Multiplexed: multiple files at a time

Slide 29

Slide 29 text

HTTP2! ● Binary: moar data! Moar efficient! ● Multiplexed: multiple files at a time ● Same connection!

Slide 30

Slide 30 text

Source: CSS Tricks

Slide 31

Slide 31 text

Who is using it? ● Google ● Netflix ● Docker ● Square ● CoreOS ● Twitch (Twirp)

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

Let’s get started!

Slide 34

Slide 34 text

gRPC + Protobuf

Slide 35

Slide 35 text

GRPC PROTOBUF The protocol Define the service Messages that are exchanged back and forth

Slide 36

Slide 36 text

What we will do ● Use protobuf to define your service ● Use protoc to generate client and server stubs based on your service definition ● Create a gRPC server to handle requests

Slide 37

Slide 37 text

Install all the things go to grpc.io for options

Slide 38

Slide 38 text

Step 1: Create a proto file

Slide 39

Slide 39 text

Sample proto file

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

Sample proto file

Slide 43

Slide 43 text

Step 2: Create your server / client stubs

Slide 44

Slide 44 text

protoc -I proto --php_out=lib --grpc_out=lib --plugin=protoc-gen-grpc=`which grpc_php_plugin` service.proto

Slide 45

Slide 45 text

protoc -I DIR --LANG_out=DIR --grpc_out=DIR --plugin=protoc-gen-grpc=`which grpc_LANG_plugin` WHERE_YOUR_PROTO_IS.proto

Slide 46

Slide 46 text

project-dir ├── client | └── client.go|js|rb|py ... ├── proto | └── service.proto ├── server | └── server.go|js|rb|py ... └── libs └── ruby └── python └── ...

Slide 47

Slide 47 text

Step 3. Create a gRPC server

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

Sample proto file

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

Step 4: Write some clients!

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

*Note: you will need to install some stuff to get started in PHP ● composer install grpc as a dependency ● pecl install grpc / protobuf ● Full instructions on getting started: https://grpc.io/docs/quickstart/php.html

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

Let’s see it in action!

Slide 57

Slide 57 text

Use case: Context.IO

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

Pain points ● Legacy codebase (~10 years old) ● Monolith codebase ● Heavily coupled == hard to unit test ● Scaling problems

Slide 60

Slide 60 text

How gRPC helped ● Replace small pieces of functionality with microservices ● Easy to do by “resource”

Slide 61

Slide 61 text

From REST to microservice GET /discovery Discovery service GET /user/:id/contacts Contacts service GET | POST /user/:id/webhooks Webhooks service GET | POST /webhooks GET /user/:id/messages Messages services GET /user/:id/folders Folder service

Slide 62

Slide 62 text

Our PHP API became the frontend for a microservice ecosystem

Slide 63

Slide 63 text

The brave new future: Completely generated client libraries & docs

Slide 64

Slide 64 text

This solves... ● Feature parity between API and client libraries ● Feature parity between API and docs ● Lack of knowledge in the team around certain languages ● Easy for other teams to consume your service

Slide 65

Slide 65 text

...and gRPC can also generate REST stubs!

Slide 66

Slide 66 text

So you can gRPC -> REST -> gRPC

Slide 67

Slide 67 text

grpc-gateway

Slide 68

Slide 68 text

Generate stubs + gateway path/to/your_service.pb.gw.go

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

What does our API look like now?

Slide 71

Slide 71 text

Monolith -> Monorepo

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

[CODE] github.com/cecyc/dad-joke-service

Slide 76

Slide 76 text

Questions? Happy to talk in the hall! or ping @cecycorrea on Twitter