REST, GraphQL, gRPC
তিনটি API style — তিনটি ভিন্ন শক্তি।
API design-এর সবচেয়ে কমন প্রশ্ন: REST, GraphQL, না gRPC? প্রতিটির শক্তি ভিন্ন — wrong choice মানে integration nightmare। চলুন বিস্তারিত দেখি।
REST (Representational State Transfer)
REST = Roy Fielding-এর ২০০০ dissertation-এ propose করা architectural style। Resource-based, HTTP method ব্যবহার করে।
মূল principles
- Resource-based: URL = noun (e.g.,
/users/123)। - HTTP methods: GET, POST, PUT, PATCH, DELETE।
- Stateless: Each request self-contained।
- JSON/XML: Standard data format।
- HATEOAS: Response-এ next action-এর link (rare in practice)।
- Cacheable: HTTP cache headers।
উদাহরণ
GET /users/123 → Get user
POST /users → Create user
PUT /users/123 → Update user
DELETE /users/123 → Delete
GET /users/123/orders → User-এর orders
সুবিধা
- Universal — সবাই জানে।
- HTTP-native — browser, curl works।
- Cacheable।
- Stateless = scalable।
- Mature tooling।
অসুবিধা
- Over-fetching: পুরো user object পাচ্ছি — শুধু name দরকার ছিল।
- Under-fetching: User + orders দুই endpoint call।
- Versioning complex।
- No real-time native।
GraphQL
GraphQL = Facebook (২০১৫) — query language for APIs। Client বলে কী চাই; server precisely দেয়।
মূল principles
- Single endpoint:
/graphql— সব query সেখানে। - Schema: Strongly typed; client সব field জানে।
- Query, Mutation, Subscription: Read, write, real-time।
- Client-driven: Client বলে কোন field দরকার।
উদাহরণ Query
query {
user(id: 123) {
name
email
orders {
id
total
product { name }
}
}
}
-- Response: শুধু requested fields।
সুবিধা
- No over/under-fetching।
- Single endpoint।
- Strongly typed schema।
- Self-documenting।
- Multiple data source merge।
- Subscription = real-time।
- Frontend autonomy — backend change না।
অসুবিধা
- Caching complex (single endpoint)।
- N+1 query problem।
- Complexity bigger।
- Authentication granular না।
- File upload tricky।
- Steep learning curve।
gRPC
gRPC = Google (২০১৫) — high-performance RPC framework। Protocol Buffers + HTTP/2।
মূল principles
- Protocol Buffers (protobuf): Binary serialization — small, fast।
- HTTP/2: Multiplexing, server push।
- Schema-first: .proto file-এ contract।
- Code generation: Multi-language client/server।
- Streaming: Unary, server-stream, client-stream, bidirectional।
উদাহরণ .proto
service UserService {
rpc GetUser(UserRequest) returns (User);
rpc StreamUsers(Empty) returns (stream User);
}
message UserRequest { int32 id = 1; }
message User {
int32 id = 1;
string name = 2;
}
সুবিধা
- Very fast — binary protocol।
- Strongly typed।
- Streaming support।
- Multi-language code-gen।
- HTTP/2 multiplexing।
- Service-to-service ideal।
অসুবিধা
- Browser-এ direct support নেই (gRPC-Web দরকার)।
- Binary — debug কঠিন।
- HTTP-tooling work করে না।
- Caching limited।
- Learning curve।
তুলনা
REST
- JSON, HTTP/1.1
- Resource-based
- Multiple endpoints
- Easy to start
- Public API standard
GraphQL
- JSON, HTTP/1.1
- Query language
- Single endpoint
- Flexible client
- Mobile/SPA-friendly
gRPC
- Protobuf, HTTP/2
- RPC-style
- Binary fast
- Service-to-service
- Internal API
কখন কোনটা?
REST ভালো
- Public API — wide compatibility।
- Standard CRUD।
- Caching critical।
- Simple resource model।
GraphQL ভালো
- Mobile/SPA — bandwidth-conscious।
- Multiple frontend, varied data needs।
- Complex nested data।
- Rapid frontend evolution।
- Multiple data source aggregate।
gRPC ভালো
- Microservice-to-microservice।
- Low latency critical।
- High throughput।
- Streaming data।
- Polyglot system।
বাস্তব উদাহরণ
- REST: Twitter API, GitHub API, Stripe।
- GraphQL: Facebook, GitHub (newer), Shopify, Airbnb।
- gRPC: Google internal, Netflix, Uber, etcd, Kubernetes internal।
Mixed Approach
Modern systems-এ একসাথে use:
- External: REST/GraphQL।
- Internal microservice: gRPC।
- Frontend: GraphQL (with REST/gRPC backend)।
সাধারণ ভুল ধারণা
- "GraphQL REST replace করবে": Different use case — coexist।
- "gRPC always fastest": Network-এ HTTP/2 ভালো — কিন্তু other factors-ও।
- "REST = JSON": XML, HTML-ও REST।
- "Public API-তে gRPC": Browser support সীমিত।
Best Practices
- Public/external API: REST default।
- Internal service-to-service: gRPC।
- Mobile/SPA: GraphQL।
- Versioning থাকতেই হবে — strategy clear।
- Schema-first design — strong typing।
- Documentation auto-generate (OpenAPI, GraphQL introspection)।
📌 চ্যাপ্টার সারমর্ম
- REST: standard, simple, public API-এর জন্য।
- GraphQL: client-driven, mobile/SPA-এর জন্য।
- gRPC: fast binary, service-to-service-এর জন্য।
- Use case-অনুযায়ী choose; coexist possible।
- Modern সিস্টেমে hybrid approach common।