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

# gRPC

তুমি যদি এমন একটা API চাও যেটা দ্রুত, কম ব্যান্ডউইথে চলে, এবং একসাথে অনেক সার্ভিসের মধ্যে যোগাযোগ করাতে পারে, তাহলে তোমার জন্য সবচেয়ে আধুনিক ও শক্তিশালী প্রযুক্তি হলো gRPC। এটি হচ্ছে Google-এর তৈরি এক অসাধারণ communication framework, যা high-performance, low-latency API তৈরি করার জন্য ব্যবহার করা হয়।

***

### gRPC কী?

**gRPC (g = Google, RPC = Remote Procedure Call)** হলো একটি **open-source framework**, যেটা client-server এর মধ্যে **function call-এর মাধ্যমে ডেটা আদান-প্রদান** করে, REST এর মতো HTTP resource-based পদ্ধতিতে নয়।

#### gRPC এর মূল বৈশিষ্ট্য:

* **Protocol Buffers (Protobuf)** নামক binary serialization format ব্যবহার করে (JSON-এর চেয়ে অনেক কম ও দ্রুত)
* HTTP/2 ব্যবহার করে (একসাথে অনেক ডেটা পাঠানো যায়)
* Built-in authentication, deadline, retry, এবং streaming support
* Language-agnostic (Python, Go, Java, C#, Node.js, সব সাপোর্টেড)

***

### gRPC কীভাবে কাজ করে?

1. **Proto file** (e.g., `service.proto`) লেখা হয়, যেখানে সার্ভিসের method, input/output message structure define করা হয়।

```proto
syntax = "proto3";

service StudentService {
  rpc GetStudent (StudentRequest) returns (StudentResponse);
}

message StudentRequest {
  int32 id = 1;
}

message StudentResponse {
  string name = 1;
  float cgpa = 2;
}
```

2. এরপর `protoc` compiler দিয়ে client ও server code auto-generate করা হয়।
3. Server সেই method implement করে।
4. Client সেই method call করে ঠিক নিজের মেশিনে যেনো call করছে—কিন্তু call টা চলে যায় remote server এ।

***

### বাস্তব উদাহরণ

#### ধরো তুমি call করছো:

```js
const response = await client.GetStudent({ id: 101 });
console.log(response.name); // Output: Mominur
```

এই call সরাসরি HTTP request না, বরং **একটি প্রোটোকল বাফার মেসেজ** যা অনেক ছোট, দ্রুত, এবং efficient!

***

### ⚖️ gRPC vs REST vs GraphQL

| বৈশিষ্ট্য           | REST            | GraphQL          | gRPC                          |
| ------------------- | --------------- | ---------------- | ----------------------------- |
| Communication style | Resource-based  | Query language   | Function-based (RPC)          |
| Data format         | JSON            | JSON             | Protobuf (binary)             |
| Speed               | ধীর             | মাঝারি           | খুব দ্রুত                     |
| Streaming           | সীমিত           | সীমিত            | Bi-directional, real-time     |
| Language support    | API level       | Client specific  | Auto-generated for many langs |
| Best for            | Web/Mobile APIs | Dynamic frontend | Microservices, internal comm. |

***

### gRPC এর ৪ ধরণের Communication

1. **Unary RPC**\
   Client → Request → Response ← Server
2. **Server Streaming RPC**\
   Client → Request → Response1, Response2, ... ← Server
3. **Client Streaming RPC**\
   Request1, Request2, ... → Server → Response
4. **Bi-directional Streaming RPC**\
   দুই দিকেই একসাথে stream করতে পারে

> Perfect for chat apps, real-time data processing, video conferencing ইত্যাদি।

***

### Security ও Performance

* TLS encryption থাকে built-in
* HTTP/2 এর কারণে **multiplexing** ও **header compression** হয়
* Protobuf ডেটা JSON এর থেকে প্রায় 10x ছোট

***

### gRPC টুলস ও লাইব্রেরি

| Language | gRPC Support Tool        |
| -------- | ------------------------ |
| Node.js  | `grpc-js`                |
| Python   | `grpcio`, `grpcio-tools` |
| Go       | Native support           |
| Java     | gRPC-Java                |
| C#       | gRPC-dotnet              |

***

### কখন gRPC ব্যবহার করবেন?

* Microservices architecture
* Internal communication (fast & efficient)
* IoT, gaming, video streaming apps
* Real-time bidirectional communication
* Polyglot environment (multi-language systems)

***

### কখন REST বা GraphQL বেটার হতে পারে?

* যদি Human-readable JSON দরকার হয়
* Public APIs বানাচ্ছো
* সহজ debug/inspect দরকার
* ব্রাউজারে সরাসরি call করতে চাই (gRPC-Web ছাড়া REST ই ভালো)

***

**gRPC** এখনকার দিনে scalable, fast এবং secure communication এর জন্য আদর্শ। এটা REST এর বিকল্প না, বরং **একটি High-performance Replacement**, বিশেষ করে Microservices এবং Internal API এর জন্য।

> If REST is a classic sedan, GraphQL is a sports car, then gRPC is a space shuttle.
