100% found this document useful (1 vote)
333 views75 pages

APIs With GRPC

This document discusses moving from a monolithic architecture to microservices using gRPC and Protocol Buffers (Protobuf). It introduces gRPC as a modern remote procedure call system that uses HTTP/2 for efficient and scalable communication between services. It outlines the steps to define a service using Protobuf, generate client and server code stubs from the service definition, and create a gRPC server. Examples are provided of how gRPC can break up a monolithic codebase into independently deployable microservices while maintaining feature parity between APIs and client libraries through code generation. Context.IO's experience migrating from a RESTful monolith to a gRPC-based microservices architecture is also summarized.

Uploaded by

bengaltiger
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
333 views75 pages

APIs With GRPC

This document discusses moving from a monolithic architecture to microservices using gRPC and Protocol Buffers (Protobuf). It introduces gRPC as a modern remote procedure call system that uses HTTP/2 for efficient and scalable communication between services. It outlines the steps to define a service using Protobuf, generate client and server code stubs from the service definition, and create a gRPC server. Examples are provided of how gRPC can break up a monolithic codebase into independently deployable microservices while maintaining feature parity between APIs and client libraries through code generation. Context.IO's experience migrating from a RESTful monolith to a gRPC-based microservices architecture is also summarized.

Uploaded by

bengaltiger
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 75

FROM MONOLITH

TO
MICROSERVICE
Modernizing legacy codebases with grpc + go
(or gRPC for dummies)
Hi, I’m
Cecy Correa
@cecycorrea
Software Engineer,
Context.IO
gRPC to break up your monolith!
what is gRPC?
gRPC Remote Procedure Calls
oh ok
what is RPC?
Data exchange between 2
processes
so an API?
RPC or REST?
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


RPC == good for actions
REST == CRUD
&
modeling your domain
[REST]
GET users/:id/photos/:photo_id
[RPC]
getUserPhotos(args...)
Why gRPC?
Why gRPC?
● Use HTTP/2
Why gRPC?
● Use HTTP/2
● Language agnostic
Why gRPC?
● Use HTTP/2
● Language agnostic
● Highly efficient / scalable
Why gRPC?
● Use HTTP/2
● Language agnostic
● Highly efficient / scalable
● Handle large amounts of data
HTTP2!
HTTP2!
HTTP2!
● Binary: moar data! Moar efficient!
HTTP2!
● Binary: moar data! Moar efficient!
● Multiplexed: multiple files at a time
HTTP2!
● Binary: moar data! Moar efficient!
● Multiplexed: multiple files at a time
● Same connection!
Source: CSS Tricks
Who is using it?
● Google
● Netflix
● Docker
● Square
● CoreOS
● Twitch (Twirp)
Let’s get started!
gRPC + Protobuf
GRPC PROTOBUF
The protocol Define the service

Messages that are


exchanged back and forth
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
Install all the things
go to grpc.io for options
Step 1: Create a proto file
Sample proto file
Step 2: Create your server / client stubs
protoc -I proto --php_out=lib --grpc_out=lib
--plugin=protoc-gen-grpc=`which
grpc_php_plugin` service.proto
protoc -I DIR --LANG_out=DIR --grpc_out=DIR
--plugin=protoc-gen-grpc=`which
grpc_LANG_plugin` WHERE_YOUR_PROTO_IS.proto
project-dir
├── client
| └── client.go|js|rb|py ...
├── proto
| └── service.proto
├── server
| └── server.go|js|rb|py ...
└── libs
└── ruby

└── python

└── ...
Step 3. Create a gRPC server
Sample proto file
Step 4: Write some clients!
*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
Now in Ruby just for the heck of it
Let’s see it in action!
[pray to demo gods]
Use case: Context.IO
Pain points
● Legacy codebase (~10 years old)
● Monolith codebase
● Heavily coupled == hard to unit test
● Scaling problems
How gRPC helped
● Replace small pieces of functionality with microservices
● Easy to do by “resource”
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


Our PHP API became the frontend
for a microservice ecosystem
The brave new future:
Completely generated client libraries & docs
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
...and
gRPC can also generate REST stubs!
So you can gRPC -> REST -> gRPC
grpc-gateway
Generate stubs + gateway

path/to/your_service.pb.gw.go
What does our API look like now?
Monolith -> Monorepo
[CODE]
github.com/cecyc/dad-joke-service
Questions?
Happy to talk in the hall!

or ping @cecycorrea on
Twitter

You might also like