Service communication — REST, gRPC, GraphQL and messaging

Topic 7 of 9 in Week 5: Distributed systems and service communication10 min readIntermediateFree course

Overview

Once a system has more than one service, every arrow between boxes is a choice: wait for an answer, or send a message and move on? Use JSON over HTTP, a binary protocol, or a query language? This lesson compares the common options so you can justify each arrow in a design.

Synchronous vs asynchronous

How services talk. Synchronously, the order service calls the stock service and waits for the reply; the caller waits and failures propagate; examples are REST, gRPC and GraphQL. Asynchronously, the order service puts a message on a queue and the email service processes it later; the caller moves on and work happens later; examples are Kafka, SQS, RabbitMQ and Pub/Sub. REST with JSON is best for public APIs and browsers but can be chatty and over- or under-fetch. gRPC is best for internal service calls but needs HTTP/2 and is harder in browsers. GraphQL suits many client screens but needs care with expensive queries and caching. Messages and events suit background work and fan-out but bring eventual consistency and duplicates.

Synchronous (request/response): the caller sends a request and waits.

  • Natural when the caller needs the answer to continue (“is this item in stock?”).
  • Couples availability: if the callee is down or slow, so is the caller. Long chains of synchronous calls multiply latency and failure rates (week 1’s availability maths).

Asynchronous (messages and events): the caller publishes a message and continues.

  • Natural for side effects, background work and notifying many services.
  • Decouples availability and absorbs spikes, but the result arrives later and you must handle duplicates and ordering (week 4).

A healthy architecture uses synchronous calls for the few things that need immediate answers and asynchronous messaging for everything else.

The synchronous options

REST over HTTP/JSON

Resources, verbs, status codes (week 1). Universally supported, easy to debug with a browser or curl, cache-friendly for GET requests. Drawbacks: text payloads are larger and slower to parse, there’s no enforced schema unless you add OpenAPI, and clients often need several calls (under-fetching) or get more than they need (over-fetching).

Best for: public and partner APIs, browser clients, simple internal APIs.

gRPC

Calls are defined in a .proto schema; messages are compact binary Protocol Buffers sent over HTTP/2, with generated client and server code in many languages. Supports deadlines, cancellation and streaming in both directions.

Best for: internal service-to-service calls with high volume and tight latency budgets. Less convenient for browsers (needs gRPC-Web or a gateway) and harder to inspect by hand.

GraphQL

Clients send a query describing exactly the fields they want, possibly across several related types, and get exactly that back in one round trip. One endpoint serves many screens.

Best for: apps with many screens and evolving data needs, aggregating several backend services for a frontend. Watch for expensive nested queries (use depth and cost limits), N+1 lookups on the server (use batching such as DataLoader) and harder HTTP caching.

Streaming and push

  • WebSockets: a full-duplex connection for chat, collaboration, live games and live locations.
  • Server-sent events: a simple one-way stream from server to browser for notifications and live updates.
  • gRPC streaming: for long-lived streams between services.

All of them need servers that can hold many open connections and a way to route a message to the server holding the right connection.

Service discovery

Instances come and go, so callers can’t hard-code addresses. Options:

  • DNS names that resolve to a load balancer or to healthy instances (Kubernetes services work this way),
  • a registry such as Consul or etcd that services register with and clients query,
  • a service mesh (Istio, Linkerd) where a sidecar proxy next to each service handles discovery, load balancing, retries, timeouts, TLS between services and metrics.

Contracts and compatibility

Services are deployed independently, so their interfaces must evolve without breaking each other:

  • define contracts explicitly: OpenAPI for REST, .proto files for gRPC, a schema for GraphQL, a schema registry (Avro, Protobuf, JSON Schema) for events,
  • make backward-compatible changes: add optional fields; never remove or repurpose fields in place,
  • version when breaking changes are unavoidable, and run both versions during migration,
  • consider consumer-driven contract tests to catch breaking changes before deployment.

In an interview

Label the important arrows: “The mobile app talks to the gateway over REST; internal calls from the trip service to pricing use gRPC with a 100 ms deadline; trip completion publishes an event to Kafka that receipts, analytics and payouts consume; live driver location streams over WebSockets.”

Recap

Use synchronous calls when the caller needs an answer now, and asynchronous messages for background work and events. REST is the universal default, gRPC is the efficient choice between services, GraphQL serves varied client needs, and WebSockets or SSE push live data. Let service discovery find instances, and keep contracts explicit and backward compatible.

Exercise

Pick how they talk

A ride-sharing platform has these interactions. Choose a communication style for each (REST, gRPC, GraphQL, WebSocket/streaming, or async messaging) and give one reason: (a) the rider app loading a trip-history screen that shows trips, driver names, ratings and receipts; (b) the trip service asking the pricing service for a fare estimate, thousands of times per second; (c) sending a receipt email after a trip ends; (d) showing the driver's car moving on the rider's map; (e) a partner company's API for booking rides for their employees.

Text
(a)
(b)
(c)
(d)
(e)

Summary

  • Synchronous calls (REST, gRPC, GraphQL) suit requests that need an answer now; they couple the caller to the callee's availability and latency.
  • Asynchronous messaging suits background work and events; it decouples services but makes results eventual.
  • REST is the default for public APIs; gRPC excels for internal service-to-service calls; GraphQL helps clients with varied data needs.
  • WebSockets, server-sent events and gRPC streaming push data continuously.
  • Services find each other through service discovery, and agree through versioned API contracts.