Notes

Part 7 of 11

Dataflow through services: REST and RPC

The most common network setup has clients and servers. A server offers an API over the network, and that API is called a service.

  • On the web, browsers send GET requests to download pages, styles, scripts, and images, and POST requests to submit data. Shared standards (HTTP, URLs, TLS, HTML) let any browser reach any website.
  • Other clients include phone and desktop apps and JavaScript running in a page. They usually get data back, often JSON, rather than HTML. The API on top of HTTP is specific to each app, and client and server must agree on it.

A service is a bit like a database: clients send data in and ask questions. But a database accepts almost any query, while a service only allows the fixed inputs and outputs its business logic defines. This hides internals (encapsulation) and gives fine control over what each client may do.

In a service-oriented or microservices design, each service is owned by one team that can deploy and change it on its own, often, without coordinating with other teams. So servers and clients of many versions run at once, and their encodings must stay compatible across API versions. A compatible API leaves each team free to change its internals.

Web services

A web service is any service you talk to over HTTP. It is used well beyond browsers:

  • An app on a phone or laptop calling its backend over the public internet.
  • One service calling another inside the same company, on a private network (microservices).
  • A service calling another company's service over the internet, such as a payment provider or a login provider using OAuth.

REST is the most popular way to design these services. It builds on how HTTP already works: simple data formats, URLs to name resources, and HTTP features for caching, login, and choosing a response format. A service built this way is called RESTful.

Clients need to know the endpoints and data formats. So developers describe the API in an IDL, which also documents it and helps it evolve. The two most popular IDLs are OpenAPI (also called Swagger) for JSON web services, and protobuf for gRPC services.

Service frameworks take care of routing, metrics, caching, and login, so developers can focus on business logic. There are two styles:

  • Code first: write the server code, and the framework generates the IDL from it.
  • Definition first: write the IDL, and tools generate the server skeleton from it.

Either way, tools can generate client libraries in many languages, plus documentation, checks that a change is compatible, and a web page for trying the API.

The problems with remote procedure calls

The RPC idea, from the 1970s, is to make a call to a remote service look just like calling a local function. This is called location transparency. The idea is flawed, because a network call is very different from a local call:

  • It is unpredictable. The request or reply can be lost, and the remote machine can be slow or down. You must plan for failure, for example by retrying.
  • It has an extra outcome. A call can time out with no result, and then you cannot know whether the request was carried out.
  • Retries can repeat actions. If only the reply was lost, a retry runs the action twice. The protocol needs deduplication (idempotence) to stop that.
  • Delay is far higher and very uneven: anywhere from under a millisecond to many seconds.
  • No pointers. Every argument must be encoded into bytes. That is fine for small values but awkward for large or changing objects.
  • Different languages. Client and server may use different languages, so types must be translated, which can get ugly. For example, JavaScript cannot hold whole numbers above 2^53 exactly.

So do not try to make a remote service look like a local object. Part of REST's appeal is that it treats moving state over the network as clearly different from a function call.