What transport protocol does gRPC use? What is the difference between HTTP/2 and HTTP/1.1?
sobes.tech AI
Answer from AI
gRPC uses the HTTP/2 transport protocol for data transfer.
Main differences between HTTP/2 and HTTP/1.1:
-
Multiplexing: HTTP/2 allows sending multiple requests and receiving responses simultaneously over a single TCP connection, significantly reducing latency. In HTTP/1.1, requests are processed sequentially or with limited parallelism.
-
Binary protocol: HTTP/2 transmits data in a binary format, unlike HTTP/1.1 which uses text. This simplifies parsing and increases efficiency.
-
Header compression: HTTP/2 uses HPACK to compress headers, reducing the amount of data transmitted.
-
Stream prioritization: HTTP/2 allows clients to specify request priorities, helping the server allocate resources optimally.
-
Server push support: HTTP/2 can send resources to the client without explicit requests, speeding up loading.
Example of initializing a gRPC server in Go (uses HTTP/2 under the hood):
import (
"google.golang.org/grpc"
"net"
)
func main() {
lis, err := net.Listen("tcp", ":50051")
if err != nil {
panic(err)
}
grpcServer := grpc.NewServer()
// Register services...
grpcServer.Serve(lis)
}