from monolith to microservice · docker square coreos twitch (twirp) let’s get started! grpc +...

75
FROM MONOLITH TO MICROSERVICE Modernizing legacy codebases with grpc + go

Upload: others

Post on 20-May-2020

16 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

FROM MONOLITHTO

MICROSERVICEModernizing legacy codebases with grpc + go

Page 2: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

(or gRPC for dummies)

Page 3: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Hi, I’m Cecy Correa

@cecycorreaSoftware Engineer,

Context.IO

Page 4: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The
Page 5: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

gRPC to break up your monolith!

Page 6: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

what is gRPC?

Page 7: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

gRPC Remote Procedure Calls

Page 8: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

oh ok

Page 9: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

what is RPC?

Page 10: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Data exchange between 2 processes

Page 11: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The
Page 12: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

so an API?

Page 13: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

RPC or REST?

Page 14: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The
Page 15: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

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

Page 16: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

RPC == good for actions

Page 17: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

REST == CRUD&

modeling your domain

Page 18: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

[REST]

GET users/:id/photos/:photo_id

Page 19: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

[RPC]

getUserPhotos(args...)

Page 20: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Why gRPC?

Page 21: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Why gRPC?

● Use HTTP/2

Page 22: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Why gRPC?

● Use HTTP/2● Language agnostic

Page 23: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Why gRPC?

● Use HTTP/2● Language agnostic● Highly efficient / scalable

Page 24: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Why gRPC?

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

Page 25: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

HTTP2!

Page 26: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

HTTP2!

Page 27: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

HTTP2!

● Binary: moar data! Moar efficient!

Page 28: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

HTTP2!

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

Page 29: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

HTTP2!

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

Page 31: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Who is using it?

● Google● Netflix● Docker● Square● CoreOS● Twitch (Twirp)

Page 32: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The
Page 33: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Let’s get started!

Page 34: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

gRPC + Protobuf

Page 35: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

GRPC PROTOBUF

The protocol Define the service

Messages that are exchanged back and forth

Page 36: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

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

Page 37: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Install all the things

go to grpc.io for options

Page 38: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Step 1: Create a proto file

Page 39: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Sample proto file

Page 40: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The
Page 41: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The
Page 42: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Step 2: Create your server / client stubs

Page 43: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

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

Page 44: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

protoc -I DIR --LANG_out=DIR --grpc_out=DIR --plugin=protoc-gen-grpc=`which

grpc_LANG_plugin` WHERE_YOUR_PROTO_IS.proto

Page 45: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

project-dir

├── client

| └── client.go|js|rb|py ...

├── proto

| └── service.proto

├── server

| └── server.go|js|rb|py ...

└── libs

└── ruby

└── python

└── ...

Page 46: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Step 3. Create a gRPC server

Page 47: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The
Page 48: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Sample proto file

Page 49: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Step 4: Write some clients!

Page 50: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The
Page 51: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

*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

Page 52: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The
Page 53: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Now in Ruby just for the heck of it

Page 54: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The
Page 55: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Let’s see it in action![pray to demo gods]

Page 56: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Use case: Context.IO

Page 57: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The
Page 58: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Pain points

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

Page 59: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

How gRPC helped

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

Page 60: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

From REST to microservice

GET /discovery Discovery service

GET /user/:id/contacts Contacts service

GET | POST /user/:id/webhooks Webhooks serviceGET | POST /webhooks

GET /user/:id/messages Messages services

GET /user/:id/folders Folder service

Page 61: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Our PHP API became the frontendfor a microservice ecosystem

Page 62: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

The brave new future:Completely generated client libraries & docs

Page 63: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

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

Page 64: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

...andgRPC can also generate REST stubs!

Page 65: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

So you can gRPC -> REST -> gRPC

Page 66: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

grpc-gateway

Page 67: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Generate stubs + gateway

path/to/your_service.pb.gw.go

Page 68: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The
Page 69: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

What does our API look like now?

Page 70: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Monolith -> Monorepo

Page 71: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The
Page 72: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The
Page 73: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The
Page 74: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

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

Page 75: FROM MONOLITH TO MICROSERVICE · Docker Square CoreOS Twitch (Twirp) Let’s get started! gRPC + Protobuf. GRPC PROTOBUF The protocol Define the service Messages that are ... The

Questions?Happy to talk in the hall!

or ping @cecycorrea on Twitter