betterCode (API) 2022, April, Mario-Leander Reimer (@LeanderReimer, Principal Software Architect bei QAware).
== Dokument bitte herunterladen, falls unscharf! Please download slides if blurred! ==
This talk focused on modern and efficient Inter-Process Communication (IPC) for microservices.
We started with a simple REST API built with JAX-RS and Quarkus, and briefly discussed the pros and cons of this approach. We extended the API with an efficient protocol-buffers serialization and converted it into a full-featured high-performance GRPC interface.
But that's not all! Based on the gRPC interface definition, we generated a matching REST API in full again: The circle towards seamless interoperability between microservices is closed.
6. A Quick History Lesson on Inter Process Communication (IPC)
QAware | 6
DCOM
18.09.1996
Win95
RPC
14.01.1976
RFC 707
REST
2000
by Roy T.
Fielding
Java RMI
Feb 1997
JDK 1.1
HTTP/1.0
Mai 1996
RFC 1945
HTTP/1.1
Juni 1999
RFC 2616
HTTP/2.0
Mai 2015
RFC 7540
SOAP 1.2
2003
RPC
Oct 1983
Birrel und
Nielson
CORBA 1.0
Oct 1991
CORBA 2.0
August 1996
CORBA 2.3
Juni 1999
XML-RPC
1998
gRPC 1.0
Aug 2016
RESTful
Applications
2014 (?)
CORBA 3.0
July 2002
7. Agenda
QAware | 7
QAware | 7
QAware | 7
REST Beer
Service
REST Beer
Service
application/json
application/x-protobuf
gRPC Beer
Service
gRPC Beer
Service
gRPC Beer
Web UI
gRPC Beer
Client
gRPC REST
Gateway
application/json
gRPC LB
Nginx
gRPC
gRPC
gRPC
gRPC
gRPC
Web UI
gRPC Web
Envoy
TypeScript
8. HTTP/1.1 200 OK
Content-Length: 139
Content-Type: application/json; charset=utf-8
Date: Wed, 10 Nov 2021 10:21:54 GMT
{
"alcohol": 5.6,
"asin": "B01AU6LWNC",
"brand": "Augustiner Brauerei München",
"country": "Germany",
"name": "Edelstoff Exportbier",
"type": "Lager"
}
GET /api/beers/B01AU6LWNC HTTP/1.1
Accept: application/json
Accept-Encoding: gzip, deflate
Connection: keep-alive
Host: localhost:8080
User-Agent: HTTPie/2.5.0
REST APIs
GET /api/beers
POST /api/beers
GET /api/beers/{asin}
PUT /api/beers/{asin}
DELETE /api/beers/{asin}
9. Richardson REST Maturity Model
QAware | 9
https://martinfowler.com/articles/richardsonMaturityModel.html
POST /bookingService HTTP/1.1
[various other headers]
<makeBookingRequest date="2010-01-04" persons="2"/>
POST /bookings HTTP/1.1
[various other headers]
<getBookingRequest id="ID-1234567890" user"lreimer"/>
GET /bookings/1234567890?user=lreimer HTTP/1.1
Accept: application/json
[various other headers]
GET /bookings/1234567890?user=lreimer HTTP/1.1
Accept: application/json
Link: /users/lreimer
[various other headers]
10. QAware | 10
1. The network is reliable
2. Latency is zero
3. Bandwidth is infinite
4. The network is secure
5. Topology doesn’t change
6. There is one administrator
7. Transport cost is zero
8. The network is homogeneous
The 8 Fallacies of Distributed Computing
11. Protocol Buffers are a language-neutral, platform-neutral
extensible mechanism for serializing structured data.
QAware | 11
■ https://developers.google.com/protocol-buffers
■ Like XML or JSON - just smaller, faster and easier!
■ Google Protobuf uses an efficient binary format to serialize data structures.
■ An Interface Definition Language (IDL) is used to define data structures and message payloads.
Many primitive types, enums, maps, arrays, nested types.
■ Protocol Buffers supports code generation for Java, Python, Objective-C, C++, Kotlin, Dart, Go,
Ruby und C#.
■ Protobuf supports evolution as well as extension of schemas. Backwards and forwards
compatibility are supported:
– you must not change the tag numbers of any existing fields.
– you may delete fields.
– you may add new fields but you must use fresh tag numbers (i.e. tag numbers that were never
used in this protocol buffer, not even by deleted fields).
13. JSON vs Protobuf Performance
QAware | 13
■ Protobuf on a non-compressed environment, the requests took 78% less time than the JSON requests.
The binary format performed almost 5 times faster than the text format.
■ Protobuf requests on a compressed environment, the difference was even bigger. Protobuf performed 6
times faster, taking only 25ms to handle requests that took 150ms on a JSON format.
https://auth0.com/blog/beating-json-performance-with-protobuf/
https://blog.qaware.de/posts/binary-data-format-comparison/
Disclaimer: please perform your own benchmarks for your specific use case!
14. Agenda
QAware | 14
QAware | 14
QAware | 14
REST Beer
Service
REST Beer
Service
application/json
application/x-protobuf
gRPC Beer
Service
gRPC Beer
Service
gRPC Beer
Web UI
gRPC Beer
Client
gRPC REST
Gateway
application/json
gRPC LB
Nginx
gRPC
gRPC
gRPC
gRPC
gRPC
Web UI
gRPC Web
Envoy
TypeScript
15. gRPC. A modern, high performance, open source and
universal RPC framework.
■ Uses HTTP/2 as modern Web-friendly transport protocol (Multiplexing, TLS, compression, …)
■ Supports several types of communication: classic request-response as well as streaming from
Client-side, Server-side, Uni- and Bi-Directional
■ Uses Protocol Buffers as efficient binary payload format
■ Support various load balancing options: proxy, client-side and look-aside balancing
■ Flexible support for tracing, health checks and authentication
■ Client and server code can be generated from the IDL easily for several languages
– https://github.com/grpc/grpc-go
– https://buf.build
■ gRPC Gateway and gRPC Web projects from the ecosystem enable good interoperability
– https://grpc-ecosystem.github.io/grpc-gateway/
– https://grpc.io/docs/platforms/web/quickstart/
■ gRPC is a CNCF incubating project QAware | 15