Skip to content

REST, GraphQL or gRPC: how to choose an API style

B2Bnetwork team · · 6 min

Integrations, data and cloud

In brief

REST is the default choice for public and partner APIs, because every HTTP client can use it and responses can be cached. GraphQL pays off when many front-end applications need different slices of the same data. gRPC works well for communication between services inside a company, where low latency, a strict contract and streaming matter. Many companies use all three: each where it fits the client and the traffic.

Three styles in one table

The styles differ above all in who decides the shape of the response and how the contract is described.

FeatureRESTGraphQLgRPC
Modelresources and HTTP methodsone query to a schema, the client chooses fieldsa procedure call defined in a .proto file
Contractusually OpenAPIa GraphQL schema, always typedProtocol Buffers
TransportHTTP, any versionusually HTTP, a single endpointHTTP/2
Response cachingnatural, through HTTP headersharder, usually on the client sideno standard HTTP mechanism
Browserno add-onsno add-onsrequires gRPC-Web and a proxy
Streaminglimitedsubscriptions, depending on the implementationbuilt in, in both directions

REST: the default choice for partners and public channels

REST is based on resources (a customer, a policy, an order) and on the meaning of HTTP methods described in RFC 9110: GET reads and does not change state, PUT replaces a resource, DELETE removes it. Thanks to this, intermediaries, API gateways and caches understand the traffic without knowing the application. The contract is usually described in an OpenAPI specification, from which documentation, clients and tests are generated.

Choose REST when:

  • the APIs will be called by external partners, for example insurance comparison sites or other operators;
  • the industry has its own REST-based standard, such as TM Forum Open API in telecommunications or open banking APIs;
  • responses are worth caching, for example a product catalog or a price list.

REST does not impose an error format or pagination, so every team invents its own. It is worth settling this once for the whole company. A ready standard exists for errors: RFC 9457 (Problem Details for HTTP APIs) describes a response with an error type, title, status code and details that a client can handle with a single piece of code. For long lists, cursor-based pagination works better than page numbers, because it neither drops nor duplicates records when data is added while browsing.

The weakness of REST is many calls for complex screens and responses that contain more fields than the client needs. With several mobile and web applications, this often leads to a separate aggregation layer for each of them.

GraphQL: when many screens need different data

In GraphQL the server publishes a typed schema, and the client specifies in a single query which fields and related objects it wants. The account summary screen in a banking app can fetch the customer, the three latest transactions and the card status in one query, without three separate calls.

This flexibility has a price:

  • The N+1 problem. A naive implementation for a list of a hundred customers will run a hundred separate queries for their accounts. You need batching of reads, for example with the DataLoader pattern.
  • Query cost. A client can build a very deep query, so the server has to limit depth and complexity.
  • Caching and monitoring. All queries go to one endpoint, so HTTP caching and per-operation metrics require extra work.

GraphQL works best as a layer over existing services for your own front-end applications, not as a public API for partners who expect stable, simple endpoints.

gRPC: communication between services

gRPC is a remote procedure call framework that by default uses Protocol Buffers to describe services and for serialization, and HTTP/2 as the transport. Client and server code in many languages is generated from a .proto file, so the contract is verified at compile time. Binary serialization produces smaller messages than JSON, and HTTP/2 allows streaming in both directions.

Choose gRPC when:

  • internal services exchange a lot of traffic, for example a pricing engine called on every premium calculation;
  • streaming is needed, for example sending readings from devices or the progress of a long operation;
  • teams use different languages and you want a single contract.

A browser does not support gRPC directly, so web applications need gRPC-Web with a proxy or a separate REST or GraphQL layer. Binary messages are also harder to inspect during diagnosis, so tooling and logging have to be prepared in advance.

Versioning, security and testing

Regardless of style, three things decide whether an API survives several years:

  1. Change rules. In REST, new fields are usually safe, and removing a field requires a new version. In GraphQL, fields are marked as deprecated (`@deprecated`) and removed only when nobody uses them. In Protocol Buffers, you must not reuse the number of a removed field.
  2. Security. Authentication, object-level authorization and rate limits apply to every style. The details are covered in the articles API security and OAuth 2.0 and OpenID Connect demystified.
  3. Contract tests. An OpenAPI contract, a GraphQL schema or a .proto file can be checked automatically in the CI pipeline before a change breaks a client. How to organize this is described in the article API contract tests, and in practice in the scenario automating API and contract tests in a microservices program.

Older integrations at banks and insurers still run on SOAP. When and how an old integration bus is replaced is described in the article ESB or event-driven architecture.

Questions and answers

Will GraphQL replace REST?

No. GraphQL solves a specific problem: many front-end applications that need different combinations of data. For partner and public APIs, and for those where HTTP caching matters, REST remains simpler to maintain and better understood by API gateways and by the teams on the other side of the integration.

Is gRPC always faster than REST?

Messages are usually smaller and the HTTP/2 connection is better utilized, but response time is more often decided by the database or the called system than by the message format. Before you change an API style for performance, measure where the time really goes. Without measurement, switching from REST to gRPC may bring no noticeable effect.

Can you have REST and gRPC for the same service?

Yes. A common setup is gRPC between services inside the company and REST outside, generated from the same contract, for example with gRPC-Gateway or transcoding in an API gateway. One contract in a .proto file reduces the risk that the two interfaces start to differ.

Where do you start when a company has no API standards at all?

With one document of rules: the default style (usually REST with OpenAPI), resource naming, error format, versioning, authentication and pagination. Exceptions, that is GraphQL for the front end or gRPC between services, are allowed deliberately and with justification. Such a document saves months of discussion on every new project.

What it looks like in a project

Connecting systems or moving them to the cloud? Talk to an architect.

Let's talk

Read more: Integrations, data and cloud