O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Practical gRPC

Book Description

This book is for engineers writing applications with different components that need to talk to each other. If you are a backend developer who wants a solution with better performance than JSON and REST for connecting microservices, this book is for you. If you are looking for strong contracts and streaming when building APIs and services, this book is also for you. This book will introduce you to gRPC and explain how it compares and contrasts with similar technologies. It will introduce you to Protocol Buffers, a key technology for using gRPC. It then provides information on how to make use of gRPC, from the most basic of usage, all the way to advanced capabilities. And it also provides lots of information for making the most of gRPC: from best practices for defining and evolving your gRPC APIs to tools in the ecosystem and how they can be leveraged to get the most out of gRPC and even extend it. This book explains gRPC, at multiple layers and with precise examples, to empower engineers to build these types of systems. It’s for engineers who want to know the entire gradient, from the simplest form of issuing an RPC to more advanced functionality and even how the RPC itself is structured on the wire. By the end of this book, you should have a clear understanding of the following: •What gRPC is and what its role is in applications. •How to use Protocol Buffers and gRPC to connect systems. • The mechanics of gRPC, including the underlying HTTP/2 protocol as well as how data is serialized and de-serialized. •Tools and practices to aid with successfully implementing gRPC in your applications. The book includes code samples in several languages. Most of the examples are simple enough that you don’t need to be an expert in any particular language to follow along. In fact, you may not be familiar with the given language at all, but should still be able to follow the logic and intent. Having said that, Go and Ruby developers will likely find themselves most comfortable reading the code because most of the examples are in those two languages.

Table of Contents

  1. Preface
    1. Who is this book for?
  2. 1. Introduction
    1. Computer networks and distributed computing
    2. Request-response protocols
    3. Remote Procedure Calls
    4. Summary
  3. 2. What is gRPC?
    1. HTTP and REST
    2. RPC systems
      1. gRPC
      2. Other RPC systems
    3. Streaming
    4. Where to use gRPC
    5. Summary
  4. 3. What are Protocol Buffers?
    1. Scalar types
    2. Nested types
    3. Maps
    4. Oneof
    5. Enums
    6. Services
      1. Importing other proto files
    7. Repetitive fields go first
    8. Polymorphism
    9. Packages
    10. Versioning proto files
    11. Different approaches for generating proto classes
      1. C++
      2. C
      3. GO
      4. Java
      5. Python
      6. NodeJS
      7. Option declarations
      8. Custom options
    12. Understand encoding
      1. Varint
      2. Varint for signed values
      3. Non-Varint values (type 1 and 5)
      4. Length-delimited (type 2)
      5. Embedded messages
    13. Summary
  5. 4. gRPC basics
    1. Vocabulary
    2. Creating and Implementing Services
      1. Compiling the Protocol
      2. Creating a Service Implementation
    3. Exposing services
      1. Graceful shutdown and load balancing
    4. Creating and using stubs
      1. Dialing the server
      2. Working with stubs
    5. Errors
    6. Metadata
      1. Metadata on the server
      2. Metadata on the client
    7. Summary
  6. 5. Star Friends
    1. Before writing code
    2. Nouns
    3. Verbs
      1. How the service works
    4. Getting the data
    5. Defining the Service
      1. Nouns
      2. Verbs
    6. Server Implmentation
    7. Starting it up
    8. Summary
  7. 6. Streaming gRPC calls
    1. Server streaming
      1. Creating the protobuf file
      2. Implementing the server
      3. Implementing the client
      4. Running the example
    2. Client streaming
      1. Creating the protobuf file
      2. Implementing the server
      3. Implementing the client
      4. Running the example
    3. Bidirectional streaming
      1. Creating the protobuf file
      2. Implementing the server
      3. Implementing the client
      4. Running the example
    4. Summary
  8. 7. Advanced gRPC
    1. Error management
      1. How gRPC encodes errors
    2. Interceptors
      1. Logging unary RPCs
      2. Capturing and mutating errors
      3. Adding timing to stubs
      4. Streaming interceptors
      5. Limitations
      6. Closing up
    3. Authentication
    4. RPC Timeouts
    5. Call cancellation
      1. Parallelizing steps
      2. Aggressive hedging
      3. Other languages
    6. Summary
  9. 8. HTTP2 overview
    1. HTTP/1.0 and HTTP/1.1
      1. HTTP/2
    2. Frames
    3. Messages
    4. Streams
    5. Multiplexing
    6. Flow control
    7. Server push
    8. Headers
  10. 9. Load balancing
    1. Client load balancing
    2. Proxy load balancing
    3. Summary
  11. 10. Service evolution with gRPC
    1. Binary and source compatibility
      1. Versioning a Service
      2. Define custom request and response objects
    2. Maintaining wire compatibility
      1. Implicit versus explicit values on the wire
    3. Behavior compatibility
      1. Default values for new fields
      2. Field clearing
      3. Dealing with errors
    4. Summary
  12. 11. Extending gRPC services
    1. gRPC Gateway
      1. Mapping URL params to request fields
      2. Custom options
      3. Protoc plugins
    2. Summary
  13. 12. Debugging gRPC
    1. CLI tools
    2. An example
    3. Environment variables
      1. GRPC_TRACE
    4. Decompiling protobuf messages
    5. Summary