Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCP sessions with gRPC

Tags:

tcp

grpc

Sorry if this question is naive. (gRPC novice here). But, I would like to understand this.

Let's say I have a gRPC service definition like this:

service ABC {
  // Update one or more entities.
  rpc Write(WriteRequest) returns (WriteResponse) {
  }
  // Read one or more entities.
  rpc Read(ReadRequest) returns (stream ReadResponse) 
  {
  }
  // Represents the bidirectional stream 
  rpc StreamChannel(stream StreamMessageRequest)
      returns (stream StreamMessageResponse) {
  }
}

Our potential use case would be the server built using C++ and the client using Java. (Not sure is that matters).

I would like to understand how the TCP sessions are managed. The Stream Channel would be used for constant telemetry data streaming between the client and the server. (Constant data transfer, but the bulk from the server to the client).

Does the StreamChannel have a separate TCP session, while for every Write and Read a new session would be established and terminated after the call is done?

Or is there a single TCP session over which all the communication happens?

Again, please excuse me if this is very naive.

Thanks for your time.

like image 436
SimpleCoder Avatar asked Oct 05 '18 01:10

SimpleCoder


People also ask

Does gRPC work on TCP?

gRPC uses HTTP/2, which multiplexes multiple calls on a single TCP connection. All gRPC calls over that connection go to one endpoint.

Is gRPC good for streaming?

gRPC supports streaming semantics, where either the client or the server (or both) send a stream of messages on a single RPC call. The most general case is Bidirectional Streaming where a single gRPC call establishes a stream in which both the client and the server can send a stream of messages to each other.

Is gRPC faster than rest?

“gRPC is roughly 7 times faster than REST when receiving data & roughly 10 times faster than REST when sending data for this specific payload. This is mainly due to the tight packing of the Protocol Buffers and the use of HTTP/2 by gRPC.”

Is gRPC multithreaded?

gRPC Python wraps gRPC core, which uses multithreading for performance, and hence doesn't support fork() .


2 Answers

Since gRPC uses HTTP/2, it can multiplex multiple RPCs on the same TCP connection. The Channel abstraction in gRPC lets gRPC make connection decisions without the application needing to be strongly-aware.

By default, gRPC uses the "pick first" load balancing policy, which will use a single connection to the backend. All new RPCs would go over that connection.

Connections may die (due to I/O failures) or need to be shut down (various reasons), so gRPC handles reconnecting automatically. Because it can take a very long time to shut down a connection (as gRPC waits for RPCs on that connection to complete), it's still possible that gRPC would have 2 or more connections to the same backend.

So for your case, all the RPCs would initially exist on the same connection. As time goes on new RPCs may use a newer connection, and an old, long-lived StreamChannel RPC may keep the initial TCP connection alive. If that long-lived StreamChannel is closed and re-created by the application, then it could share the newer connection again.

like image 117
Eric Anderson Avatar answered Sep 28 '22 04:09

Eric Anderson


I also posted the same question in grpc.io, and the response I got was inline with the marked answer. Summary: If there is no load-balancing, all the RPCs use the same session. The session remains connected across requests. The session establishment happens the first time a call is attempted on the channel.

like image 23
SimpleCoder Avatar answered Sep 28 '22 04:09

SimpleCoder