Estilos de API

REST · gRPC · GraphQL

Escrito por Darjus Kazakevic · revisado el

REST for the public, gRPC inside, GraphQL when clients choose the shape.

Most calls between services, and from apps to servers, are a request and a response. Three styles are common. REST models resources with URLs and HTTP verbs. gRPC calls typed functions over HTTP/2 with Protocol Buffers. GraphQL offers one endpoint where the client asks for exactly the fields it needs.

The style matters less than three habits every API needs: stable pagination, a plan for versions, and idempotency keys so clients can retry safely. These habits decide whether an API still works in its second year.

En esencia — Pick REST for public APIs, gRPC for internal calls and GraphQL for varied client screens, and give every API pagination, versions and idempotency keys.

Cuándo recurrir a él

Recurre a él cuando

  • You are designing a new API and must choose how clients call it
  • Clients retry after timeouts, and a duplicate would cost money
  • A list endpoint returns more rows than fit in one response

Reconsidéralo cuando

  • The server must push updates as they happen: use real-time delivery
  • The caller does not need an answer now: use a queue or a topic
  • Bulk data transfer between systems: use files in object storage

Conceptos clave

REST
Resources at URLs, read and changed with GET, POST, PUT, PATCH and DELETE, usually with JSON bodies. Every language, browser, cache and CDN understands it, which makes it the default for public APIs.
gRPC
Remote calls defined in a .proto file and sent as binary Protocol Buffers over HTTP/2. Clients are generated for each language. Messages are smaller and faster to parse than JSON, and streaming is built in.
GraphQL
One endpoint with a typed schema. The client sends a query that names the fields it wants, across related objects, and gets exactly that back. A complex screen needs one round trip instead of five.
Pagination
Returning a long list in pages. Offset pagination (?offset=2000) is simple, but slow on deep pages, and it skips or repeats rows when data changes. A cursor (?after=token) stays fast and stable.
Idempotency-Key header
A request header that names one operation, so a retried POST is not done twice. Stripe popularised it and an IETF draft standardises it. The Idempotency guide shows how the server stores the keys.

Reglas de diseño

Pick the style by who calls the API
Public or partner APIs: REST, because every tool speaks it. Busy calls between your own services: gRPC. Many screens with different data needs: GraphQL. Telling a partner that something happened: a webhook.
Make every create call safe to retry
Accept an Idempotency-Key header on POST. Store the key with the response and return the stored response on a repeat. A timeout then never turns into a double charge.
Use cursors for large lists
Return a limit and an opaque cursor, not page numbers. Offsets get slow on deep pages, and they skip or repeat rows when data changes between calls.
Add fields; never change their meaning
Adding optional fields is safe. Removing or retyping one breaks clients. For a real break, ship v2 next to v1, in the URL (/v2/) or a header, and keep v1 until clients move.

Errores habituales

  • A POST without an idempotency key behind a client that retries. A timeout after a successful charge leads to a second charge.
  • Offset pagination on a busy table. Page 2,000 takes seconds, and new rows shift the pages, so users see duplicates or miss items.
  • GraphQL resolvers that query once per item. A list of 50 orders with customers runs 51 queries; batch them, for example with DataLoader.
  • Removing or renaming a field in place. Mobile apps that users have not updated break the same day.