Slide 1

Slide 1 text

qaware.de Microservice-Kommunikation entmystifiziert: Von REST zu gRPC und darüber hinaus Mario-Leander Reimer [email protected] @LeanderReimer #CloudNativeNerd

Slide 2

Slide 2 text

2 Mario-Leander Reimer Managing Director & CTO @LeanderReimer #cloudnativenerd #qaware #gernperDude

Slide 3

Slide 3 text

Agenda QAware | 3 QAware | 3 QAware | 3 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 GraphQL Beer Service application/graphql

Slide 4

Slide 4 text

lreimer/from-rest-to-grpc

Slide 5

Slide 5 text

“One cannot not communicate.” - Paul Watzlawick

Slide 6

Slide 6 text

A Quick History Lesson on Inter Process Communication (IPC) QAware | 6 REST 2000 by Roy T. Fielding RPC 14.01.1976 RFC 707 RPC Oct 1983 Birrel und Nielson DCOM 18.09.1996 Win95 HTTP/1.0 Mai 1996 RFC 1945 CORBA 1.0 Oct 1991 CORBA 2.0 August 1996 HTTP/1.1 Juni 1999 RFC 2616 CORBA 2.3 Juni 1999 Java RMI Feb 1997 JDK 1.1 XML-RPC 1998 SOAP 1.2 2003 CORBA 3.0 July 2002 RESTful Applications 2014 (?) HTTP/2.0 Mai 2015 RFC 7540 gRPC 1.0 Aug 2016 GraphQL Sep 2015

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

Agenda QAware | 8 QAware | 8 QAware | 8 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 GraphQL Beer Service application/graphql

Slide 9

Slide 9 text

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} „Pretty URLs returning JSON“ — @hhariri Nouns Verbs Request Response

Slide 10

Slide 10 text

Richardson REST Maturity Model QAware | 10 https://martinfowler.com/articles/richardsonMaturityModel.html POST /bookingService HTTP/1.1 [various other headers] POST /bookings HTTP/1.1 [various other headers] 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]

Slide 11

Slide 11 text

Readability is not a known software quality attribute! QAware | 11 Software Product Quality (ISO 25010) ● Modularity ● Reusability ● Analysability ● Modifiability ● Testability Maintainability ● Confidentiality ● Integrity ● Non-repudiation ● Authenticity ● Accountability Security ● Adaptability ● Installability ● Replaceability Portability ● Co-existence ● Interoperability Compatibility ● Maturity ● Availability ● Fault Tolerance ● Recoverability Reliability ● Time Behaviour ● Resource Utilization ● Capacity Efficiency ● Completeness ● Correctness ● Appropriateness Functional Suitability ● Operability ● Learnability ● UI Aesthetics ● Accessibility Usability

Slide 12

Slide 12 text

Establish team, product or even company wide API guidelines. QAware | 12 https://opensource.zalando.com/restful-api-guidelines/ https://adidas.gitbook.io/api-guidelines

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

JSON vs Protobuf Performance QAware | 14 ■ 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!

Slide 15

Slide 15 text

Protocol Buffers are a language-neutral, platform-neutral extensible mechanism for serializing structured data. QAware | 15 ■ 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).

Slide 16

Slide 16 text

syntax = "proto3"; option java_package = "hands.on.grpc"; option java_outer_classname = "BeerProtos"; package beer; message Beer { string asin = 1; string name = 2; string brand = 3; string country = 4; float alcohol = 5; enum BeerType{ IndianPaleAle = 0; SessionIpa = 1; Lager = 2; } BeerType type = 6; } // Protobuf marshalling protoBeer = BeerProtos.Beer.newBuilder() .setAsin("B079V9ZDNY") .setName("Drunken Sailor") .build(); byte[] bytes = protoBeer.toByteArray(); // Protobuf unmarshalling protoBeer = BeerProtos.Beer.parseFrom(bytes); $ ./gradlew generateProto $ protoc -I=$SRC_DIR --java_out=$DST_DIR $SRC_DIR/beer.proto

Slide 17

Slide 17 text

Agenda QAware | 17 QAware | 17 QAware | 17 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 GraphQL Beer Service application/graphql

Slide 18

Slide 18 text

gRPC in a Nutshell. ■ 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 is a CNCF incubating project QAware | 18

Slide 19

Slide 19 text

syntax = "proto3"; option java_package = "hands.on.grpc"; option java_outer_classname = "BeerProtos"; import "google/protobuf/empty.proto"; package beer; service BeerService { rpc AllBeers (google.protobuf.Empty) returns (GetBeersResponse) {} rpc GetBeer (GetBeerRequest) returns (GetBeerResponse) {} rpc CreateBeer (CreateBeerRequest) returns (google.protobuf.Empty) {} rpc UpdateBeer (UpdateBeerRequest) returns (google.protobuf.Empty) {} rpc DeleteBeer (DeleteBeerRequest) returns (google.protobuf.Empty) {} } // more Protobuf message definitions ...

Slide 20

Slide 20 text

Agenda QAware | 20 QAware | 20 QAware | 20 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

Slide 21

Slide 21 text

The gRPC ecosystem in a Nutshell. ■ Different projects from the gRPC ecosystem enable good interoperability – gRPC Gateway https://grpc-ecosystem.github.io/grpc-gateway/ – gRPC Web https://grpc.io/docs/platforms/web/quickstart/ ■ Support various load balancing options: proxy, client-side and look-aside balancing – Nginx https://nginx.org/en/docs/http/ngx_http_grpc_module.html – Envoy https://www.envoyproxy.io/docs/envoy/latest/intro/arch_overview/other_protocols/grpc QAware | 21

Slide 22

Slide 22 text

import "google/protobuf/empty.proto"; import "google/api/annotations.proto"; service BeerService { rpc AllBeers (google.protobuf.Empty) returns (GetBeersResponse) { option (google.api.http) = { get: "/api/beers" }; } rpc GetBeer (GetBeerRequest) returns (GetBeerResponse) { option (google.api.http) = { get: "/api/beers/{asin}" response_body: "beer" }; } rpc CreateBeer (CreateBeerRequest) returns (google.protobuf.Empty) { option (google.api.http) = { post: "/api/beers" body: "*" }; } // more definitions … } Map gRPC call to GET request path Map {asin} path param to request Use beer field as response body Map POST body to request

Slide 23

Slide 23 text

Agenda QAware | 23 QAware | 23 QAware | 23 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 GraphQL Beer Service application/graphql

Slide 24

Slide 24 text

GraphQL in a Nutshell. ■ Developed by Facebook, publicly available since 2015 → graphql.org ■ Data can be retrieved flexible and use case oriented. – Especially useful for mobile applications and environments with limited connectivity and bandwidth. – No more over or under fetching of data! ■ Allows efficient, server-side querying and retrieval of complex and deeply nested data structures. ■ Suitable to implement Backend-for-Frontend pattern (B4F) ■ Powerful schema language to define data structures, with fields and types. ■ Evolve API without versions. Client, server and schema can evolve independently. - Not suitable for simple CRUD or realtime services, and inter-service communication - Caching GraphQL is complex and challenging, often requiring manual implementation - Strict requirements for security and access control can be challenging - Complex queries can have significant impact on server performance (e.g. DoS) QAware | 24

Slide 25

Slide 25 text

schema { query: Query mutation: Mutation } type Query { allBeers: [Beer]! getBeer(asin: String!): Beer } type Mutation { createBeer(asin: String!, name: String!, country: String, abv: Float, brewery: String): Beer updateBeer(asin: String!, name: String!, country: String, abv: Float, brewery: String): Beer deleteBeer(asin: String!): String } type Beer { asin: String! name: String! country: String abv: Float brewery: String } Data Type Query Operations Mutating Operations

Slide 26

Slide 26 text

North-South communication with REST B4F using GraphQL East-West communication with gRPC N S E W REST REST GraphQL gRPC

Slide 27

Slide 27 text

QAware GmbH | Aschauer Straße 30 | 81549 München | GF: Dr. Josef Adersberger, Michael Stehnken, Michael Rohleder, Mario-Leander Reimer Niederlassungen in München, Mainz, Rosenheim, Darmstadt | +49 89 232315-0 | [email protected] Thank you!