Search Tutorials


Spring Boot 3 + gRPC - Types of gRPC | JavaInUse

Spring Boot 3 + gRPC - Types of gRPC

In previous tutorial we had implemented Spring Boot + gRPC Hello World Example. gRPC, short for Google Remote Procedure Call, is a high-performance open-source framework developed by Google. It facilitates communication between client and server applications, allowing them to call methods on each other as if they were making local function calls.
In this tutorial we will look at the different gRPC types. We need different types of gRPC calls to be used in different use cases. Each type of gRPC provides a specific communication pattern, allowing developers to choose the most appropriate type based on their needs, resource constraints, and desired behavior. By offering various types, gRPC ensures flexibility and efficiency in handling different data exchange scenarios.
Spring Boot + gRPC - Types of gRPC

gRPC - Table of Contents

Spring Boot+ gRPC Hello World Example Spring Boot gRPC Server + C# gRPC Client Example Spring Boot 3 + gRPC - Types of gRPC Spring Boot 3 + gRPC Unary Example Spring Boot 3 + gRPC Server Streaming Example Spring Boot 3 + gRPC Client Streaming Example Spring Boot 3 + gRPC Bidirectional Streaming Example Spring Boot + gRPC Deadline Example Spring Boot + gRPC Error Handling Example Spring Boot + gRPC Error Handling - Using Trailer Metadata Spring Boot + gRPC Error Handling - Global Exception Handler Using GrpcAdvice

Video

This tutorial is explained in the below Youtube Video.


Types of gRPC

  • Unary gRPC


    Spring Boot + gRPC Unary RPC
    Unary gRPC is a type of communication protocol that allows client-server interactions where the client sends a single request to the server and gets a single response back. This means that the client sends one piece of data to the server and waits for a response.
    For example, if you want to fetch a single item from a database, authenticate a user, or perform a calculation and obtain the result, unary gRPC can be a good choice.

    Use Case

    Consider a banking service where the the client needs to get the account balance. For this a unary gRPC call is made to the server which returns the balances.
    service AccountBalanceService {
      rpc GetAccountBalance (AccountRequest) returns (AccountBalanceResponse) {}
    }
    

    Bank Unary gRPC
  • Server Streaming gRPC


    Spring Boot + gRPC Server Streaming RPC
    Server streaming gRPC is a communication protocol that allows the server to send a stream of responses to the client. This pattern is useful in scenarios where the server needs to push a large amount of data or a continuous stream of updates to the client.
    For example, it can be used for real-time updates like stock market prices, weather updates, news feeds, or even sending a large file in chunks.

    Use Case

    Consider a banking service where the the client needs to get transaction information of 30 days. This transaction information is processed by the server. But this is very large information which cannot be sent to the client as a single response. So it is sent as stream to the client.
    service TransactionService {
      rpc streamTransactions(AccountRequest) returns (stream TransactionDetailList);
    }
    

    Bank Server Streaming gRPC




  • Client Streaming gRPC


    Spring Boot + gRPC Client Streaming RPC
    Client streaming gRPC is a communication pattern in which the client sends multiple messages to the server using a single gRPC connection. It allows the client to initiate a stream of data and send chunks of information to the server, which processes the data and responds accordingly.
    One common use case for Client streaming gRPC is when the client needs to send a large amount of data in small chunks to the server. It can be useful in scenarios where the client is continuously generating or collecting data that needs to be processed or analyzed on the server side.

    Use Case

    Consider a banking service where the the client needs to upload address proof pdf to back server. This is large amount of information to be sent by the client to the server. So this address pdf is uploaded by the client to the server.
    service BankService {
      rpc UploadAddressProof(stream AddressProofRequest) returns (AddressProofResponse) {}
    }
    

    Bank Client Streaming gRPC
  • Bidirectional Streaming gRPC


    Spring Boot + gRPC Bidirectional Streaming RPC
    Bidirectional streaming gRPC is a communication pattern in which both the client and server can send multiple messages to each other in a continuous stream. It allows real-time, two-way communication between the client and server.
    This pattern is commonly used in scenarios where there is a need for ongoing, interactive communication. For example, in a chat application, bidirectional streaming gRPC can be used to send and receive messages in real-time between the client and server. It can also be used for real-time analytics, where data is continuously sent from server to client and vice versa.

    Use Case

    Consider a banking service where the client chats with bank customer service. The client can send multiple requests and receive multiple responses. Similarly the bank customer service can receive multiple requests and send multiple responses.
    service ChatService {
      rpc StartChat (stream ChatMessage) returns (stream ChatResponse) {}
    }
    

    Bank Client Streaming gRPC
  •