> For the complete documentation index, see [llms.txt](https://kdongs.gitbook.io/kdocs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kdongs.gitbook.io/kdocs/grpc/grpc.md).

# gRPC

{% embed url="<https://grpc.io/docs/what-is-grpc/introduction/>" %}

## About

It is a framework for building robust and scalable communication between server applications.

Ideal for:

* Microservices. *(Communication between services)*
* Backend. *(*`server-to-server` *communication)*
* Client. *(*`client-to-backend` *communication)*

{% hint style="warning" %}
Browsers are not yet fully prepared to handle gRPC.
{% endhint %}

It has a couple of advantages over `REST`, like:

* Type safety.
  * `gRPC` enforces message structures defined in [Protocol Buffers](/kdocs/grpc/protocol-buffers.md). This ensures compatibility between services and prevents unexpected data types from causing errors.
* Performance otimizations.
  * Uses a compact binary encoding format for messages, leading to faster transmissions and lower resource consumption compred to other `JSON`-based APIs.
* Cleaner developement experience.
  * It offers features like streaming `RPCs`, allowing for efficient handling of data transfers or real-time updates.

{% hint style="info" %}
`gRPC` can communicate over `HTTP/2`. This means it:

* Supports traffic in `binary`.
* Supports bi-directional communication. *(Between Client-Backend)*
  {% endhint %}

### REST vs gRPC

<table data-card-size="large" data-view="cards"><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td><strong>REST</strong></td><td><ul><li>Text / Json</li><li>One-directional only</li><li>Higher latency</li><li><p>No contracts <em>(higher change of error)</em></p><ul><li><em>Like</em> <code>number</code> <em>as</em> <code>string</code><em>.</em></li></ul></li><li>No streaming support.</li></ul></td><td></td></tr><tr><td><strong>gRPC</strong></td><td><ul><li>Protocol Buffers</li><li>Bi-directional and Asyncronous</li><li>Lower lantency</li><li>Defined contract (<code>.proto</code>)</li><li>Streaming support.</li></ul></td><td></td></tr></tbody></table>

## RPC (Remote Procedure Call)

A `Client` calls the `Server`, with a request, invoking a procedure and the Server responds with a response.

## Communication Types

### Unary

Client makes a **single** `request` and the Server responds with a **single** `response`.

<img src="https://3323317117-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ5xXd5s1ke9Kv8ajF30t%2Fuploads%2FhXP8BRUgYtpod5Fr0bEi%2Ffile.excalidraw.svg?alt=media&amp;token=cba1dbc6-2a5f-4535-b2b0-30e4993115ce" alt="" class="gitbook-drawing">

### Server Streaming

Client makes a **single** `request` and the Server responds **one or multiple** `responses`.

This helps the Client to use the data as it comes (piece by piece), and not have to wait the server to fully process and returns the hole data.

<img src="https://3323317117-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ5xXd5s1ke9Kv8ajF30t%2Fuploads%2FODHBvV3dILrO52rheZwh%2Ffile.excalidraw.svg?alt=media&amp;token=2070db1f-b908-47c3-a6c2-75488e8ba844" alt="" class="gitbook-drawing">

### Client Streaming

Client makes **multiple** `requests` and the Server responds with a **single** `response`.

Like if you will upload a file, you may upload it in peaces. After the Server receives them all it process it and respond.

<img src="https://3323317117-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ5xXd5s1ke9Kv8ajF30t%2Fuploads%2Fqo7AcgqRhTLApiQvoizv%2Ffile.excalidraw.svg?alt=media&amp;token=5788c630-8b3e-4169-8ab3-cbd8b0c73bbb" alt="" class="gitbook-drawing">

### Bi Directional Streaming

Client makes **multiple** `requests` and the Server responds with **multiple** `responses`.

<img src="https://3323317117-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQ5xXd5s1ke9Kv8ajF30t%2Fuploads%2FKJd8IMg8LdVs3dR0mJxh%2Ffile.excalidraw.svg?alt=media&amp;token=ed529d84-e2ad-4504-ac57-3b9e0d841005" alt="" class="gitbook-drawing">
