Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
From PHP and REST to gRPC and Go
Search
Yuriy Tuz
December 04, 2019
Programming
0
79
From PHP and REST to gRPC and Go
Yuriy Tuz
December 04, 2019
Tweet
Share
More Decks by Yuriy Tuz
See All by Yuriy Tuz
From PHP and REST to gRPC and Go Full
yuriytuz
0
50
Other Decks in Programming
See All in Programming
What's new in Spring Modulith?
olivergierke
1
130
Cursorハンズオン実践!
eltociear
2
860
チームの境界をブチ抜いていけ
tokai235
0
150
オープンソースソフトウェアへの解像度🔬
utam0k
12
2.4k
止められない医療アプリ、そっと Swift 6 へ
medley
1
140
Back to the Future: Let me tell you about the ACP protocol
terhechte
0
140
階層構造を表現するデータ構造とリファクタリング 〜1年で10倍成長したプロダクトの変化と課題〜
yuhisatoxxx
3
970
複雑化したリポジトリをなんとかした話 pipenvからuvによるモノレポ構成への移行
satoshi256kbyte
1
1k
CSC509 Lecture 01
javiergs
PRO
1
440
Domain-centric? Why Hexagonal, Onion, and Clean Architecture Are Answers to the Wrong Question
olivergierke
2
790
NetworkXとGNNで学ぶグラフデータ分析入門〜複雑な関係性を解き明かすPythonの力〜
mhrtech
3
1.2k
Flutterで分数(Fraction)を表示する方法
koukimiura
0
130
Featured
See All Featured
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
A better future with KSS
kneath
239
18k
Become a Pro
speakerdeck
PRO
29
5.5k
Product Roadmaps are Hard
iamctodd
PRO
54
11k
Writing Fast Ruby
sferik
629
62k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
9.7k
The Cost Of JavaScript in 2023
addyosmani
53
9k
Reflections from 52 weeks, 52 projects
jeffersonlam
352
21k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
How STYLIGHT went responsive
nonsquared
100
5.8k
It's Worth the Effort
3n
187
28k
Transcript
From PHP and REST To gRPC and Go Winnipeg, December
4 2019 Yuriy Tuz Bold Commerce @yuriytuz
#GoTurns10
whoami
Objectives • Personal experience • Why gRPC • gRPC in
GO ◦ Simple implementation
In the beginning • Laravel 4.x / 5.x • PHP
7.x ◦ Introduced types
Back in the day
REST (HTTP + JSON) • Not a bad solution •
Service to service communications • JSON is not type safe in PHP • (De)Serialization performance • Text protocol
Thank you Questions?
REST in Laravel • api.php • Magic • response()->json([???]) •
Docs accuracy
Problem • Technology is driving demand • Languages and platforms
◦ Different dev experience • Software architecture is evolving ◦ Microservices and containers ◦ CI/CD
An Idea
An Idea • Bloated service ◦ 1000+ lines • Hack
Day • How?
No Idea
How/why? • Why split? • Why Go? • Why not
REST (HTTP+JSON)? ◦ No formal contract ◦ Hard to implement correctly
Possible Solution
What do we need? • PHP RPC client • RPC
server
Implementation/Findings • PHP client not fun ◦ Working POC •
First experience with Go • Refactoring • Benchmarks ◦ ~100x faster
time.Now()
Stack • Protocol buffers + gRPC • Thanks Google
Protocol buffers are a language-neutral, platform-neutral extensible mechanism for serializing
structured data. Protocol Buffers
What does this mean? • Serialization mechanism • Interface description
language
Serialization Mechanism • 0a 05 68 65 6c 6c 6f
• 68 65 6c 6c 6f -> hello • 05 -> length of “hello” • 0a is “0000 1010”, so • field_number = 1 • wire_type = 2 (length delimited)
Interface Definition Language message Person { string name = 1;
} Interface for your API
gRPC • Open source messaging system by Google that can
run anywhere • Low latency • Highly scalable • Great for distributed systems • Accurate, efficient, language independent
Adopted by many (and more)
Let’s do it
echo.proto
Let’s generate • protoc --go_out=:. ./echo.proto ◦ Default protobuf generation
• protoc --go_out=plugins=grpc:. ./echo.proto ◦ Client and Server interface ◦ gRPC
Success!
Server
Generated Interface
Server implementation
Server Spin up server
Client
Client interface
Client
So far • Server • Client
New Requirement Logging
gRPC middleware • Server or Client • Few exist in
gRPC ecosystem ◦ Prometheus ◦ Open tracing • https://github.com/grpc-ecosystem/go-grpc-middleware
How?
gRPC middleware
>1 interceptor?
Directory structure • Client & server implementation • echo.pb.go ◦
gRPC generated • echo.proto ◦ API definition
Helpful tools
grpcurl • “curl” for gRPC • Requires server reflection
None
Examples
grpcui • Postman for RPC • Also requires reflection
None
None
Final thoughts The good • Code generation ◦ Less error
prone • API spec • gRPC ◦ HTTP2 + language support • Type safety • Plugins • Dev ramp up speed The bad • Code generation • Model conversions ◦ Nested messages • Hard/scary to get started ◦ Documentation • API versioning
TL;DR • Use gRPC + Go • Generate code •
Implement stubs • Profit
Credits/Useful links • Gopher art by Ashley McNamara ◦ https://github.com/ashleymcnamara/gophers
• gRPC ◦ https://grpc.io/ • Google API Design Guide ◦ https://cloud.google.com/apis/design/ • Protobuf Style Guide ◦ https://developers.google.com/protocol-buffers/docs/style • rpcurl and grpcui ◦ https://github.com/fullstorydev
Questions? @yuriytuz
None