Upgrade to Pro — share decks privately, control downloads, hide ads and more …

gRPC or REST, why not both?

Avatar for Murad Murad
August 03, 2019

gRPC or REST, why not both?

While writing microservices you have to make a choice, REST or gRPC (https://grpc.io). While REST has been the norm for writing microservices for quite some time, gRPC is replacing it. But what if one wants their services to be available as both a REST and a gRPC service. In this session, I will show you how you can realize this. In addition to this, I will also show how you document your REST APIs.

Participants can have a look at https://grpc.io to know what gRPC is.
No resources are required.
Expected outcome - Attendees will have an idea about what gRPC is and how to write gRPC and REST services from a single codebase.

Avatar for Murad

Murad

August 03, 2019

Other Decks in Technology

Transcript

  1. Agenda • What is REST? • Disadvantages of REST. •

    What is gRPC? • Advantages of gRPC.
  2. Agenda • What is REST? • Disadvantages of REST. •

    What is gRPC? • Advantages of gRPC. • gRPC and REST services from a single codebase.
  3. REST • Representational State Transfer. • Not a protocol but

    an Design Pattern. • Uses HTTP protocol.
  4. REST • Representational State Transfer. • Not a protocol but

    an Design Pattern. • Uses HTTP protocol. • Objects that are send or saved are called resources.
  5. REST • Representational State Transfer. • Not a protocol but

    an Design Pattern. • Uses HTTP protocol. • Objects that are send or saved are called resources. • Operations are described by HTTP verbs.
  6. REST • Representational State Transfer. • Not a protocol but

    an Design Pattern. • Uses HTTP protocol. • Objects that are send or saved are called resources. • Operations are described by HTTP verbs. • Transfers data as XML or JSON or Protocol Buffers.
  7. A REST API can be - XML + HTTP JSON

    + HTTP Protocol Buffers + HTTP
  8. { "id": 12, "name": "Developing in Node", "author": "Prem Pratap

    Tomar", "rating": 3.2, "release_date": "2019-06-17T19:12:43.905Z", "units_sold": 123456, "publisher": "Leaf publications", "best_sell": true } Bloated data
  9. { "id": 12, "name": "Developing in Node", "author": "Prem Pratap

    Tomar", "rating": 3.2, "release_date": "2019-06-17T19:12:43.905Z", "units_sold": 123456, "publisher": "Leaf publications", "best_sell": true } Message size - 190 bytes (Ignoring the white spaces) Bloated data
  10. { "id": 12, "name": "Developing in Node", "author": "Prem Pratap

    Tomar", "rating": 3.2, "release_date": "2019-06-17T19:12:43.905Z", "units_sold": 123456, "publisher": "Leaf publications", "best_sell": true } Message size - 190 bytes (Ignoring the white spaces) Useful info - 107 bytes (56.3%) Unnecessary info describing useful info - 83 bytes (43.7%) Bloated data
  11. {"books":[{"id": 12,"name":"Developing in Node", "author":"Prem Pratap Tomar","rating":3.2,"release_date": "2019-06-17T19:12:43.905Z", "units_sold":1 23456,"publisher":"Leaf

    publications", "best_sell":true},{"id":13,"name":"Developing in Node","author":"Prem Pratap Tomar","rating":3.2,"release_date": "2019-06-17T19:12:43.930Z", "units_sold":1 23456,"publisher":"Leaf publications", "best_sell":true},{"id":14,"name":"Developing in Node","author":"Prem Pratap Tomar","rating":3.2,"release_date": "2019-06-17T19:12:43.955Z", "units_sold":1 23456,"publisher":"Leaf publications", "best_sell":true}]} Message size - 190 * 3 = 570 bytes (approx) Useful info - 107 * 3 = 321 bytes (approx) Unnecessary info - 83 * 3 = 249 bytes (approx) Bloated data
  12. Disadvantages of REST? • The data being transferred is heavily

    bloated. • There is no formal contract to communicate.
  13. Disadvantages of REST? • The data being transferred is heavily

    bloated. • There is no formal contract to communicate. • HTTP verbs don’t always describe your API.
  14. Disadvantages of REST? • The data being transferred is heavily

    bloated. • There is no formal contract to communicate. • HTTP verbs don’t always describe your API. • Streaming is difficult.
  15. Disadvantages of REST? • The data being transferred is heavily

    bloated. • There is no formal contract to communicate. • HTTP verbs don’t always describe your API. • Streaming is difficult. • GraphQL problems.
  16. gRPC • gRPC Remote Procedure Call • Open source version

    of an internal Google product. • High performance RPC framework.
  17. gRPC • gRPC Remote Procedure Call • Open source version

    of an internal Google product. • High performance RPC framework. • Uses HTTP/2 protocol.
  18. gRPC • gRPC Remote Procedure Call • Open source version

    of an internal Google product. • High performance RPC framework. • Uses HTTP/2 protocol. • Uses Protocol Buffers for message interchange.
  19. gRPC • gRPC Remote Procedure Call • Open source version

    of an internal Google product. • High performance RPC framework. • Uses HTTP/2 protocol. • Uses Protocol Buffers for message interchange. • Can use other formats like JSON, XML.
  20. Messages and Services compiler Generated Code Servers and Clients Implement

    Define Writing a gRPC service. (Step 3/3) Compile
  21. Protocol Buffer • Interface Definition Language (IDL). • Message interchange

    format. • Platform neutral mechanism for serializing structured data.
  22. Protocol Buffer • Interface Definition Language (IDL). • Message interchange

    format. • Platform neutral mechanism for serializing structured data. • Similar to XML or JSON but very small and fast.
  23. Protocol Buffer • Interface Definition Language (IDL). • Message interchange

    format. • Platform neutral mechanism for serializing structured data. • Similar to XML or JSON but very small and fast. 3-10x smaller than XML 10-100X faster than XML
  24. Messages and Services .proto Compiler protoc Define Compile Implement Servers

    and Clients Generated code C# C++ Dart Go Java Node Objective-C PHP Python Ruby
  25. Ruby Service gRPC server Go Service gRPC server gRPC Stub

    Java Service gRPC Stub Python Service gRPC server gRPC Stub Mobile client Desktop client gRPC Stub gRPC Stub gRPC server
  26. Why gRPC? What do I get? • Generated code in

    10 languages. • Strict contract between servers and clients.
  27. Why gRPC? What do I get? • Generated code in

    10 languages. • Strict contract between servers and clients. • Performant and efficient on the wire Small binary messages. Faster serialisation and deserialisation. Multiplexing and Header Compression.
  28. • Generated code in 10 languages. • Strict contract between

    servers and clients. • Performant and efficient on the wire Small binary messages. Faster serialisation and deserialisation. Multiplexing and Header Compression. • In addition to traditional unary APIs you also get Client Streaming APIs Server Streaming APIs Bi-Directional streaming APIs Why gRPC? What do I get?
  29. But... What about our existing APIs? Do we • Develop

    both flavors parallelly? • Deprecate existing APIs and ask clients to update their code.
  30. grpc-gateway • A plugin for protoc compiler. • An open

    source tool by grpc-ecosystem. • Generates a reverse proxy which can communicate to your gRPC server using JSON over HTTP.
  31. Using grpc-gateway (Step 2/3) 1. Annotating your proto file. 2.

    Generate reverse proxy and OpenAPI definitions.
  32. Annotated Messages and Services .proto Compiler protoc + grpc-gateway Server

    interfaces and client stubs Open API specification your-service.json Reverse proxy in Go your-service.pb.gw.go
  33. Using grpc-gateway (Step 3/3) 1. Annotating your proto file. 2.

    Generate reverse proxy and OpenAPI definitions. 3. Write an entry point for the reverse proxy.
  34. Thank you More about gRPC and grpc-gateway • https://grpc.io •

    https://github.com/grpc • https://github.com/grpc-ecosystem/grpc-gateway • https://grpc-ecosystem.github.io/grpc-gateway