51
boilerplate to high performance scalable APIs gRPC Robert Kubis Developer Advocate

Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

Embed Size (px)

Citation preview

Page 1: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

boilerplate to high performance scalable APIs

gRPC

Robert KubisDeveloper Advocate

Page 2: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

2@hostirosti @grpcio

Robert KubisDeveloper AdvocateGoogle Cloud PlatformLondon, UK

hostirosti github.com/hostirosti

About me

Page 3: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

Agenda

Motivation (Microservices)

HTTP/2

Protobuf

gRPC

1

2

3

4

Page 4: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

4

Microservices

Page 5: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

5@hostirosti @grpcio

A B

C D

Decomposing Monolithic apps

Page 6: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

6@hostirosti @grpcio

A B

CD

Decomposing Monolithic apps

Page 7: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

7@hostirosti @grpcio

A B

CDgRPC

Decomposing Monolithic apps

Page 8: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

8

HTTP/2

Page 9: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

9@hostirosti @grpcio

Today statistics show that there are on average: 12 distinct hosts per page 78 distinct requests per page 1,232 KB transferred per page

Resulting in typical render times of 2.6-5.6 seconds (50th and 90th percentiles).

So what’s up with HTTP/1?

* Numbers are medians, based on latest HTTP Archive crawl data.

Page 10: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

10@hostirosti @grpcio

HTTP Pipelining Queue responses not requests

Domain Sharding Need more than 6 connections per origin?

Add more origins!

Concatenation and Spriting Multiple JavaScript/CSS/Image files

combined into a single resource

Resource Inlining Inline JS/CSS/Images in the page

img1.example.comimg2.example.comimg3.example.com...

Coping Strategies

Page 11: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

11@hostirosti @grpcio

HTTP Pipelining Head of Line blocking

Domain Sharding Over sharding can kill performance

Concatenation and Spriting Increased complexity, can hurt cache

performance and page load

Resource Inlining Can hurt cache performance, hard to get

granularity right

img1.example.comimg2.example.comimg3.example.com...

Copying Strategies - The downsides

Page 12: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

12

● Improve end-user perceived latency● Address the "head of line blocking"● Not require multiple connections● Retain the semantics of HTTP/1.1

Ilya Grigorik - hpbn.co/http2 - High Performance Browser Networking

"HTTP/2 is a protocol designed for low-latency transport of content over the World Wide Web"

Page 13: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

13@hostirosti @grpcio

One TCP connection

Request → Stream Streams are multiplexed Streams are prioritized

Binary framing layer Prioritization Flow control Server push

Header compression

HTTP/2 in one slide ...

Page 14: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

14

“... we’re not replacing all of HTTP — the methods, status codes, and most of the headers you use today will be the same.

Instead, we’re redefining how it gets used “on the wire” so it’s more efficient, and so that it is more gentle to the Internet itself ....”

- Mark Nottingham (HTTPbis chair)

Page 15: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

15@hostirosti @grpcio

Sits between Socket interface and the API

HTTP messages are decomposed into one or more frames

HEADERS for meta-data DATA for payload RST_STREAM to cancel …

Each frame has a common header 9-byte, length prefixed Easy and efficient to parse

HTTP/2 binary framing 101

Page 16: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

16@hostirosti @grpcio

Basic data flow in HTTP 2.0...

Page 17: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

17@hostirosti @grpcio

Streams are multiplexed, because frames can be interleaved

All frames (e.g. HEADERS, DATA, etc) are sent over single TCP connection

Frame delivery is prioritized based on stream dependencies and weights

DATA frames are subject to per-stream and connection flow control

Basic data flow in HTTP 2.0...

Page 18: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

18@hostirosti @grpcio

Each stream can have a weight [1-256] integer value

Each stream can have a dependency parent is another stream ID

1. E.g.. style.css (“A”) should get 2/3rd’s of available resources as compared to logo.jpg (“B”)2. E.g.. product-photo-1.jpg (“D”) should be delivered before product-photo-2.jpg (“C”)

NOTE: Prioritization is an advisory optimization hint to the server

Stream prioritization in HTTP/2...

Page 19: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

19@hostirosti @grpcio

HTTP/2 server: “You asked for /product/123, I know you’ll also need app.js and product-photo-1.jpg, so… I promise to deliver these to you, no need to ask for them. That is, unless you decline or cancel.”

GET /product/123

/product/123/app.js/product-photo-1.jpg

● Server push is optional and can be disabled by the client● Server push is subject to flow control - e.g. “you can only push 5Kb”● Pushed resources can be cached and prioritized individually

Server push, “because you’ll also need…”

Page 20: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

20@hostirosti @grpcio

HTTP/2 client: “I want photo.jpg, please send the first 20KB.”

HTTP/2 server: “Ok, 20KB… pausing stream until you tell me to send more.”

HTTP/2 client: “Send me the rest now.”

GET /product-photo.jpg/product-photo.jpg

/product-photo.jpgWINDOW_UPDATE

● Flow control allows the client to pause stream delivery, and resume it later● Flow control is a “credit-based” scheme

○ Sending DATA frames decrements the window○ WINDOW_UPDATE frames update the window

Per-stream flow control

Page 21: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

21@hostirosti @grpcio

● Literal values are (optionally) encoded with a static Huffman code● Previously sent values are (optionally) indexed

○ e.g. “2” in above example expands to “method: GET”

HPACK header compression

Page 22: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

22

Protocol Buffers

Page 23: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

23@hostirosti @grpcio

Structured representation of data

Google's lingua franca for data 48k+ Message Types 12k+ Proto files

Evolutionary Development

Incrementally solved problems, Now used for: RPC Systems Persistent Data Storage

What are Protocol Buffers?

Page 24: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

24@hostirosti @grpcio

Protocol buffers:

Efficient and compact binary data representation Clear compatibility rules; can easily be extended over time Generates idiomatic, easy to use classes for many languages Strongly typed; less error prone

Why Protocol Buffers?

Page 25: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

25@hostirosti @grpcio

Protocol Buffers also are:

No use as a markup language Not human readable (native format) Only meaningful if you have the message definition

Why not?

Page 26: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

26@hostirosti @grpcio

Uniquely numbered fieldsTypedHierarchical StructureOptional and Repeated fieldsExtensible without breaking backward compatibility

message Person { required string name = 1; required int32 id = 2; optional string email = 3;

enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; }

message PhoneNumber { required string number = 1; optional PhoneType type = 2 [default = HOME]; }

repeated PhoneNumber phone = 4;}

Message Format (proto2)

Page 27: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

27@hostirosti @grpcio

Protocol Buffers language version 3

Specified by syntax = “proto3”;

All fields are optional in proto3

No user specified default values

No groups (FYI for those that use them)

syntax = “proto3”;

message Person { string name = 1; int32 id = 2; string email = 3;

enum PhoneType { MOBILE = 0; HOME = 1; WORK = 2; }

message PhoneNumber { string number = 1; PhoneType type = 2; } repeated PhoneNumber phone = 4;}

Message Format (proto3)

Page 28: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

28@hostirosti @grpcio

Add new fields without breaking backwards-compatibility

old implementations ignore the new fields when parsing

In proto3 any field can be removed, but don’t renumber existing fields

syntax = “proto3”;

message Person { string name = 1; int32 id = 2; string email = 3; address addr = 5;

message address { string firstLine = 1; string secondLine = 2; string postalCode = 3; string country = 4; }

...}

Extensible

Page 29: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

29@hostirosti @grpcio

https://github.com/google/protobuf

Protocol Buffer Compiler

Page 30: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

30

gRPC

Page 31: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

31@hostirosti @grpcio

gRPC goalsBuild an open source, standards-based, best-of-breed, feature-rich RPC system

Enable developers to build microservice-based applications

Build an open source, standards-based, best-of-breed, feature-rich RPC system

Create easy-to-use, efficient and idiomatic libraries

micro-servicesperformant and scalableefficient and idiomatic

Provide a performant and scalable RPC framework

Page 32: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

32@hostirosti @grpcio

IDL to describe the API

Automatically generated servers and clients in 10+ languages

Takes advantage of feature set of HTTP/2

Lightweight open connections

Point to point

Streaming! Bidirectional streaming!

gRPC in a nutshell

Page 33: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

33@hostirosti @grpcio

gRPC client/server architecture

Page 34: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

34@hostirosti @grpcio

Define a service in a .proto file using Protocol Buffers IDL

Generate server and client code using the protocol buffer compiler with grpc plugin

Use the gRPC API to write a simple client and server for your service in the languages of your choice

Getting Started

Page 35: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

35@hostirosti @grpcio

The most common use-case is a unary request-response RPC

Msg1

MsgAClient Server

RPC Messages

Page 36: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

36@hostirosti @grpcio

service RouteGuide {

rpc GetFeature(Point) returns (Feature);

}

message Point {

int32 latitude = 1;

int32 longitude = 2;

}

message Feature {

string name = 1;

Point location = 2;

}

Service Definition

Page 37: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

37@hostirosti @grpcio

Msg1Msg2Msg3

MsgA MsgB

Metadata

Metadata MetadataClient Server

The second most common use-case is sending multiple request or response messages in the context of a single RPC call

Ordered Bidirectional Streaming RPC

Page 38: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

38@hostirosti @grpcio

service RouteGuide { rpc ListFeatures (Rectangle) returns (stream Features); ...}

message Rectangle { Point lo = 1; Point hi = 2;}

Server Streaming RPC Definition

Page 39: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

39@hostirosti @grpcio

service RouteGuide { rpc RecordRoute (stream Point) returns (RouteSummary); ...}

message RouteSummary { int32 point_count = 1; int32 feature_count = 2; int32 distance = 3; int32 elapsed_time = 4;}

Client Streaming RPC Definition

Page 40: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

40@hostirosti @grpcio

service RouteGuide { rpc RouteChat (stream RouteNote) returns (stream RouteNote); ...}

message RouteNote { string location = 1; string message = 2;}

Bidirectional Streaming RPC Definition

Page 41: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

41@hostirosti @grpcio

Implementations● C core

○ Native bindings in C++, Node.js, Python, Ruby, ObjC, PHP, C#

● Java using Netty or OkHttp (+ inProcess for testing)

● Go

gRPC Language Support

Page 42: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

42@hostirosti @grpcio

SSL/TLS

gRPC has SSL/TLS integration and promotes the use of SSL/TLS to authenticate the server, and encrypt all the data exchanged between the client and the server. Optional mechanisms are available for clients to provide certificates to accomplish mutual authentication.

OAuth 2.0

gRPC provides a generic mechanism to attach metadata to requests and responses. Can be used to attach OAuth 2.0 Access Tokens to RPCs being made at a client.

Authentication

Page 43: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

43@hostirosti @grpcio

Multi-languageFreedom to pick the language independently for each micro-service, based on performance, library availability, team expertise etc

Loosely coupled developmentBlocks of functionality can be broken off into separate MicroService. Allows organic growth

High PerformanceMake use of the strengths of HTTP/2 and Protocol Buffers

MicroServices using gRPC

Page 44: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

44@hostirosti @grpcio

C++ Service

gRPC serverGolang Service

gRPC server

gRPC Stub

Java Service

gRPC Stub

Python Service

gRPC server

gRPC Stub

Polyglot Microservices Architecture

Page 45: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

45

Demo gRPCFingers crossed :)

Page 46: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

46

Mobile

Page 47: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

47@hostirosti @grpcio

Photo-sharing App

Mobile Clients communicate with gRPC endpoint

Clients for iOS and Android

https://cloud.google.com/solutions/mobile/image-management-mobile-apps-grpc

Abelana Mobile Sample App

Page 48: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

48@hostirosti @grpcio

Abelana Architecture

Page 49: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

49

Wrap-up

Page 50: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

50@hostirosti @grpcio

grpc is Open SourceWe want your help!

http://grpc.io/contribute

https://github.com/grpc

irc.freenode.net #grpc

@grpcio

[email protected]

Page 51: Robert Kubis - gRPC - boilerplate to high-performance scalable APIs - code.talks 2015

51

@hostirosti @grpcio @googlecloud

Thank you! Danke und auf Wiedersehen :)