How services talk
Your shop has grown into an app plus an internal Orders service. Phones, the website and partners all call in, 6,000 req/s, straight at your servers.
Play this levelFree with an account. It runs in your browser: build the design, press Run, and watch what breaks.
The lesson
REST names things with URLs and uses HTTP verbs: GET /orders/42. It's simple and every tool understands it, which makes it the usual choice for public APIs.
RPC, such as gRPC, calls a function on another machine with a compact binary format. It's fast and strictly typed, so it suits calls between your own services. GraphQL lets a client ask for exactly the fields it needs in one request, which suits apps with many different screens.
Services move around as machines come and go, so callers look them up by name in a registry, which is service discovery. An API gateway is the single front door that checks, limits and routes calls from outside.
What you have to achieve
6,000 requests a second at the busiest hour, 10% of them writes.
- No broken or illegal connections
- Use an API Gateway
- Use an Internal Service
- No component is overloaded
- Fewer than 0.1% of requests fail
What you learn
Outside callers now come through one front door, and your services find each other by name. Each style has its place: REST at the edge, RPC between services, GraphQL for many screens.
Check yourself
Which style suits fast calls between your own services?
- REST with JSON
- RPC, such as gRPC
Show the answer
RPC, such as gRPC. RPC uses a compact binary format and strict types, so calls are quick and mistakes show up early. You control both ends, so the tight coupling is fine.
A phone screen needs three fields from three resources. What does GraphQL give you?
- One request for exactly those fields
- Faster servers
- A free cache
Show the answer
One request for exactly those fields. GraphQL lets the client describe what it needs, so it avoids both extra round trips and fetching data it never shows.
Orders runs on machines that come and go. How do callers find it?
- A list of addresses in the code
- Service discovery: look it up by name
- Ask the user
Show the answer
Service discovery: look it up by name. A registry, or the DNS your platform provides, keeps the current addresses, so callers ask by name and never hold a stale address.