+ All Categories
Home > Documents > From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming...

From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming...

Date post: 14-May-2020
Category:
Upload: others
View: 4 times
Download: 0 times
Share this document with a friend
72
From REST to gRPC: Joe Runde IBM @joerunde Michael Keeling IBM @michaelkeeling An API Evolution Story
Transcript
Page 1: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

From REST to gRPC:

Joe Runde

IBM

@joerunde

Michael Keeling

IBM

@michaelkeeling

An API Evolution Story

Page 2: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

2

Page 3: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

What is gRPC?

Open source Remote Procedure Call framework

3

Page 4: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

What is gRPC?

Open source Remote Procedure Call framework

4

“… a modern, bandwidth and CPU efficient, low latency way to create massively distributed systems that span data centers”

Page 5: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

5

Why should we use gRPC?

Page 6: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Why is gRPC awesome?

6

Performance

Page 7: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Performance Benefits

7

HTTP / REST

Page 8: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Performance Benefits

8

HTTP / REST gRPC

Page 9: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

How does gRPC improve performance?

• HTTP/2 transport protocol

• Binary encodings via protocol buffers

• No more parsing text!

• Compression

• Streaming

9

Page 10: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Why is gRPC awesome?

10

Performance

Page 11: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Why is gRPC awesome?

11

Performance

Remote Procedure Calls

Page 12: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

REST setup is tedious

12

headers = {}

headers.put(“X-FooBar-User”,

user.getFooBarID())

headers.put(“FooBarTransaction”,

app.getNextTxnIDAtomic())

headers.put(“FooBarAlgoType”,

ApproximateFoo.toHeaderString())

Page 13: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

REST setup is tedious

13

Public class Foo {

@JsonProperty(name=”foo_id”)

String id;

@JsonProperty(name=”bar”)

int bar;

...

}

Page 14: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

REST setup is tedious

14

Public class Foo {

...

public

Foo(@JsonProperty(“foo_id”,required=true)

String id,

@JsonProperty(“bar”,required=true)

int bar)

{ ... }

}

Page 15: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

RPC setup is easy

15

request = FooService.RequestBuilder()

.setId(foo.getId())

.setBar(foo.getBar())...

.build();

response = fooClient.DoFooBar(request);

Page 16: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Why is gRPC Awesome?

16

Performance

Remote Procedure Calls

Page 17: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Why is gRPC Awesome?

17

Performance

Remote Procedure Calls

Strategic Direction of our Platform

Page 18: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

18

Page 19: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

<insert slick demo here>

19

Page 20: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Just one problem…

Our current microservices

all use REST.

20

Page 21: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

21

Page 22: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Business Constraints

22

Page 23: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

The Transition Plan

• Phase 1: Design gRPC API

• Phase 2: Run REST and gRPC services

• Phase 3: Transition functional tests

• Phase 4: Remove REST functionality

23

Page 24: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

DESIGN THE APIPhase I

24

Page 25: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Designing a gRPC API

gRPC:

rpc AddFoo(Foo)

returns FooID;

rpc GetFoo(FooID)

returns Foo;

25

REST:

POST

/api/foo

GET

/api/foo/{foo_id}

Page 26: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Designing a gRPC API

gRPC:

message Foo {

FooID id = 1;

repeated Bar bars = 2;

}

message FooID {

string val = 1;

}

26

REST:

POST

/api/foo

GET

/api/foo/{foo_id}

Page 27: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Designing a gRPC API

gRPC:

rpc AddBar(Bar)

returns BarID;

rpc GetFoo(BarID)

returns Bar;

27

REST:

POST

/api/foo/{foo_id}/bar

GET

/api/foo/{foo_id}/bar/

{bar_id}

Page 28: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Designing a gRPC API

gRPC:

rpc AddBar(Bar)

returns BarID;

rpc GetFoo(BarID)

returns Bar;

28

REST:

POST

/api/foo/{foo_id}/bar

GET

/api/foo/{foo_id}/bar/

{bar_id}

Page 29: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Designing a gRPC API

gRPC:message Bar{

BarID id = 1;

int baz = 2;

}

message BarID {

FooID foo_id = 1;

string bar_id = 2;

}

29

REST:

POST

/api/foo/{foo_id}/bar

GET

/api/foo/{foo_id}/bar/

{bar_id}

Page 30: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Designing a gRPC API

30

Bar service:

/api/foo/{foo_id}/bar/{bar_id}

Buzz service:

/api/foo/{foo_id}/buzz/{buzz_id}

Page 31: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

SERVE REST AND GRPCPhase II

31

Page 32: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Current: REST Only

32

FooBar

Service

REST

Caller

Page 33: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Future: REST and gRPC

33

FooBar

Service

REST

Caller

gRPC

Caller

Page 34: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

We need to evolve our API without

damaging basic functionality.

34

Page 35: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Code Package

is allowed to

use

Current: Layers View

35

Rest Service

Business Logic

Datastore Access

Models

Page 36: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Models

36

Code Package

is allowed to

use

Rest

Service

Business Logic

Datastore Access

gRPC

Service

Future: Layers View

Page 37: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Layer Pattern Rocks!

A code review comment:

"What's with the service layer? This just passes its inputs to our business logic functions, it's redundant cruft!"

37

Page 38: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Evolving the code went really well…

38

Page 39: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

39

Page 40: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Things that suddenly became a problem

• Health Checks

• API Discovery

• No curl

• Headers?

• SSL?

• Simple community examples40

Page 41: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

TRANSITION FUNCTIONAL TESTSPhase III

41

Page 42: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Specification by Example

42

Page 43: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

43

Specification by Example- REST

Page 44: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Ruby metaprogramming

44

Choice of programming language really paid off

request = Object::const_get(

”FooBar::#{message_name}").decode_json(json)

response = client.method(method_name.to_sym)

.call(request)

Page 45: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

45

Specification by Example- gRPC

Page 46: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

200+ tests transitioned in 1 week

46

Page 47: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Why did this go so well?

47

• Expressiveness of spec by example

• Flexibility of Ruby

• gRPC can decode JSON

Page 48: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

REMOVE REST FUNCTIONALITYPhase IV

48

Page 49: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

49

rm –rf src/*rest*

Page 50: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

We need to retain some REST endpoints

50

REST

Caller

gRPC

Caller

FooBar

Service

Page 51: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

51

Page 52: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Current Layered Architecture

52

Models

Rest

Service

Business Logic

Datastore Access

gRPC

Service

FooBar

Service

Service

Page 53: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Layers Forked – Two Services

53

ModelsBusiness Logic

Datastore Access

gRPC Service

FooBar

Service

gRPC Clients

REST ServiceFooBar

Proxy

Service

Page 54: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Evil wizards strike again!

54

Page 55: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

NOW WE’RE READY FOR RELEASE…?

55

Page 56: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

New problems we created

• Health Checks

• API Discovery

• No curl

• Headers?

• SSL?

56

Page 57: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

New problems we created

• Health Checks

• API Discovery

• No curl

• Headers?

• SSL?

57

Page 58: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

API Discovery

• REST – Ask the service!

• gRPC – Find the (correct) proto file?

58

Page 59: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

API Discovery

• REST – Ask the service!

• gRPC – Find the (correct) proto file?• Standard InfoService serves github url + version

• Snapshot proto files with releases

• Client vendors the proto files they use

59

Page 60: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

60

Page 61: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Tools support

• Basically Nonexistent

• Our solutions:• Hand roll mocks for testing

• Write new functional tests each time we wanted to use curl

61

Page 62: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

62

Adios,

REST!

Page 63: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

INTERESTING THINGS WE LEARNED

63

Page 64: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

• The right kinds of abstractions promote

extensibility

• Focus on the domain model

• Create a specification by example

• Take care when choosing frameworks

• Deal with risks of technology adoption

64

Page 65: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Would we do it again?

65

Page 66: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Would we do it again?

• Super easy to integrate with a service

• Promotes small polyglot services

• Difficult to do bad things

• Performance is 🔥🔥🔥

66

Yes.

Page 67: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

Thank you!

Michael Keeling

@michaelkeeling

neverletdown.net

Joe Runde

@joerunde

Buy Design It! now at

http://bit.ly/2pJOrly

Page 68: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

68

BACKUP

Page 69: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

69

Page 70: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

70

Page 71: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

71

Page 72: From REST to gRPC - Carnegie Mellon University · Ruby metaprogramming 44 Choice of programming language really paid off ... •Expressiveness of spec by example •Flexibility of

72


Recommended