REST, RPC ও GraphQL
REST/gRPC/GraphQL, pagination, idempotency, versioning আর mobile payload।
Start with the production incident, then the design decision that fixes it.
চলো-র mobile home screen-এ ১২টা API call হচ্ছিল: rider profile, upcoming ride, wallet, nearby drivers, promo, notifications… প্রতিটা call-এ ৩G-তে ৩০০ms latency, total ৩ সেকেন্ড। Websocket update-ও ছিল না। নুসরাত দেখল, একই screen-এর data চারটা endpoint-এ ছড়ানো; mobile-এ প্রতিটা round-trip দামি। তারপর সে একটা aggregate endpoint বানাল, কিন্তু response-এ ৪০টা field পাঠাল—যার ৮০% UI ব্যবহার করে না।
API design মানে শুধু URL নয়; client-এর দরকার, payload size, latency, versioning, error contract। REST, RPC (gRPC), GraphQL—তিনটারই জায়গা আছে; ধর্ম নয়, trade-off।
REST। Resource + HTTP verb: GET /rides/{id}, POST /rides, PATCH. Cacheable GET, stateless, widely understood। কিন্তু multiple client view-এ over/under-fetching; chatty mobile।
RPC/gRPC। Procedure call, protobuf binary, HTTP/2; internal service-to-service-এ দ্রুত, typed, streaming। Public browser-এ gRPC-web/compat দরকার; human-debuggable নয়।
GraphQL। Client query-তে field বেছে নেয়; over-fetching কম, এক call-এ multiple resource। কিন্তু nested query cost, N+1, caching কঠিন, authorization per field; complexity budget দরকার।
Pagination। Offset (?page=2) সহজ, কিন্তু deep page ধীর, insert-এ duplicate। Cursor (?after=id) scalable, stable; UI infinite scroll-এ ভালো।
Idempotency। POST retry-তে duplicate; Idempotency-Key header + server store। PUT idempotent, PATCH নয়; payment-এ key বাধ্যতামূলক।
Versioning/error। URL /v1, breaking change-এ নতুন version; additive field safe। Error: stable code + human message + request_id; HTTP status semantics ঠিক।
| চাহিদা | REST | gRPC | GraphQL |
|---|---|---|---|
| Public/browser | ভালো | দুর্বল | ভালো |
| Mobile payload | মাঝারি | ভালো | ভালো |
| Streaming | SSE/WS | ভালো | subscription |
| Cache | সহজ | কঠিন | কঠিন |
| Internal RPC | মাঝারি | সেরা | কম |
Cursor pagination আর idempotent POST:
GET /v1/rides?limit=20&after=ride_01J...
Idempotency-Key: 7f4c2a...
POST /v1/rides
{ "pickup": "...", "drop": "..." }
def list_rides(cursor, limit=20):
rows = db.fetch(
"SELECT id, created_at FROM rides WHERE (created_at, id) < ($1, $2) "
"ORDER BY created_at DESC, id DESC LIMIT $3",
(cursor.created_at, cursor.id, limit + 1),
)
return rows[:limit], rows[limit - 1].id if len(rows) > limit else None
query Home {
me { id name wallet }
upcomingRide { id status driver { name } }
}
GraphQL-এ client exact field চায়; server-এ depth limit, query cost, per-field auth লাগবে।
১. Client/channel ঠিক করো। Public web/mobile REST/GraphQL; internal service gRPC; realtime SSE/WS।
২. Contract লিখো। Resource/Procedure, request/response schema, error code, pagination, idempotency.
৩. Mobile budget। Screen অনুযায়ী aggregate/BFF endpoint; response <100KB; compression; cache header.
৪. Version/backward compat। Additive field, /v1; deprecation timeline; client telemetry.
৫. Failure contract। 4xx vs 5xx, retryable, request_id, rate limit 429; docs/example।
একটা mobile home screen-এর ৬টা data block লেখো। প্রতিটার জন্য REST call ধরলে কত round-trip, কত payload? GraphQL-এ এক query-তে কত? BFF/aggregate endpoint-এ কত? 3G latency 300ms ধরে total time মেলাও।
Response-এ internal field/DB column leak করো না; serializer/allowlist। GraphQL depth/cost limit না দিলে একটা query DB মেরে দেবে। Public API-তে version/deprecation policy, OpenAPI schema, rate limit—সব এক জায়গায় document করো।
See the mental model first — the animation and the steps below it say the same thing.
Concept visual
REST, RPC না GraphQL—যে চুক্তি মোবাইলের payload আর versioning সামলাতে পারে, সেটাই বাছো।
Write a short design on paper or in a note, then compare it with the rubric. This is not a judge submission.
Design practice
home screen-এ ৬টা data block, 3G latency 300ms, ১২টা REST call।
One timed attempt. Solve every problem to earn the module badge.
Final checkpoint
Ready to prove this module?
Ask questions, share an approach or leave a study tip for this lesson.
Was this lesson helpful?
Your reaction helps other learners judge these notes.
0 comments
Sign in to ask a question or share an approach.
Sign in to commentNo comments yet
Be the first to ask a question or leave a study tip.
| প্রশ্ন | উত্তর |
|---|
| Public API? | REST/GraphQL |
| Internal? | gRPC |
| Mobile? | BFF/aggregate |
| Pagination? | cursor |
| Retry? | Idempotency-Key |
| Breaking? | /v2 + deprecation |