← Back to System Design, page 2

system-design-0024 · Medium fresher

Compare REST, gRPC and GraphQL. When would you choose each for an API?

REST exposes resources over HTTP with standard methods and usually JSON; it is simple, cacheable and works everywhere. gRPC uses Protocol Buffers over HTTP/2 with generated clients, giving compact messages, strict contracts and streaming, which suits service-to-service calls. GraphQL exposes one endpoint where clients ask for exactly the fields they need, which suits varied front-end screens but makes caching and cost control harder.

Full answer

REST

Resources are nouns with URLs; HTTP methods are the verbs.

GET /courses/42 read a course POST /courses/42/enrolments enrol a student PATCH /students/7 update some fields

Strengths: every language and tool speaks HTTP and JSON; responses can be cached by browsers and CDNs with standard headers; easy to debug with curl. Weaknesses: a screen may need several calls (course, teacher, reviews), or get more fields than it needs. Contracts are loose unless you add OpenAPI.

gRPC

You define services and messages in a .proto file:

proto
syntax = "proto3";

service Grades {
  rpc GetReportCard (ReportCardRequest) returns (ReportCard);
  rpc StreamScores (ExamId) returns (stream Score);
}

message ReportCardRequest { int64 student_id = 1; }
message ReportCard { int64 student_id = 1; repeated Score scores = 2; }
message Score { string subject = 1; int32 marks = 2; }
message ExamId { int64 id = 1; }

Code generators create typed clients and servers. Messages are binary Protobuf, smaller and faster to parse than JSON. HTTP/2 allows many concurrent calls on one connection and supports client, server and bidirectional streaming. Deadlines propagate across calls. Weaknesses: browsers cannot call it directly (gRPC-Web or a gateway is needed), binary payloads are harder to inspect, and HTTP caching does not apply.

GraphQL

One endpoint (POST /graphql). The client sends a query naming the fields it wants:

graphql
query {
  course(id: 42) {
    title
    teacher { name }
    reviews(first: 3) { rating }
  }
}

Strengths: one round trip per screen, no over-fetching, a typed schema, and front-end teams can evolve screens without new endpoints. Weaknesses: caching by URL does not work (needs persisted queries or client caches); a nested query can be very expensive, so you need depth or cost limits; the N+1 database query problem needs batching (DataLoader); errors often come back with HTTP 200.

Choosing

SituationGood fit
Public API for third partiesREST (with OpenAPI)
Internal service-to-service calls, low latency, streaminggRPC
Many client screens with different data needs (web, mobile)GraphQL, often as a layer over internal services
Heavy CDN caching of public readsREST

Many companies use all three: gRPC between services, GraphQL or a REST backend-for-frontend for apps, and REST for partners.

By level

Fresher: Describe REST with methods and URLs, and explain in simple terms what gRPC and GraphQL change. A fresher should give one use case for each style.

Mid: Compare on payload, contract, caching, streaming and tooling, and choose for public, internal and front-end use cases. Expect one weakness of each style, such as GraphQL query cost.

Senior: Discuss schema evolution and backward compatibility, API gateways and federation, observability of each style, cost control in GraphQL, and migration between styles. Expect a strategy for backward compatibility of each style.

Key points

  • REST: resources plus HTTP methods, cacheable, universal
  • gRPC: Protobuf over HTTP/2, typed generated clients, streaming, deadlines
  • GraphQL: client chooses fields from one endpoint, one round trip per screen
  • GraphQL needs query cost limits and batching for N+1
  • Mixing them by audience is common

Common mistakes

  • Saying gRPC works directly from browsers without gRPC-Web or a proxy
  • Claiming GraphQL is always faster
  • Forgetting HTTP caching advantages of REST
  • Ignoring query cost and N+1 problems in GraphQL

Follow-ups

  • How do you version a gRPC API safely?
  • How would you stop an expensive GraphQL query from hurting the database?
  • What is a backend-for-frontend?

Tips

  • Describe each in one line before comparing
  • Pick by audience: partners, services, front-ends
  • Mention one weakness of each

Evaluation rubric

  • Accurate description of all three
  • Correct strengths and weaknesses
  • Sensible choice by use case
  • Mentions caching, contracts and performance aspects