SystemTutor
About
Sign in
Loading course documentation…
SystemTutor

COURSES

  • Data Structures & Algorithms
  • CSS MasterclassNew course
  • System DesignNew course

PRACTICE

  • Algorithm practice
  • CSS practice

LEGAL

  • Privacy Policy
  • Terms of Service
  • About

© 2026 SystemTutor. All rights reserved.

Built with 🫶 by Shatlyk

System design
API ContractsRESTGraphQLRPCChoosing an API contract
API design

RPC

Follow a remote procedure call and reason about a result that arrives after the client’s deadline.

Lesson goal

Trace a unary call through a stub and handler, then choose between a resource-based contract and a named operation.

Before you begin — GraphQL.

A profile service exposes a named operation, GetUser. A caller supplies user ID 42 and expects a user record. Remote procedure call (RPC) lets the caller express this action as a function call while a runtime carries the request to the service that performs it.

Trace one call across the network

A stub is a local object or function that exposes the remote method to client code. Calling the stub starts work on the caller’s machine before anything reaches the server.

The client calls getUser through an RPC request. The remote server handles it and sends the result back.

  1. The client calls GetUser with ID 42. Its stub encodes the input as a message.
  2. The runtime sends the message and identifies the target method.
  3. The server decodes the request and invokes its handler. The handler looks up the user.
  4. The server sends back a result and call status. The client runtime checks the status and decodes the result for its caller.

This is a unary call, with one request and one response. A deadline limits how long the caller will wait. If it expires first, the caller can see a failure even when the server finished the operation. gRPC call lifecycle

Describe the contract with gRPC

gRPC is one implementation of RPC. Its usual setup uses Protocol Buffers to describe methods and messages and to encode their fields on the wire. From the same definition, teams can generate client stubs and server interfaces in different programming languages.

syntax = "proto3";

service UserService {
  rpc GetUser (GetUserRequest) returns (User);
}

message GetUserRequest {
  string id = 1;
}

message User {
  string id = 1;
  string name = 2;
}

Read the method signature from left to right. It accepts one GetUserRequest and returns one User on success. Generated code supplies the message and call machinery. The team still implements the handler that finds the user. gRPC commonly carries calls over HTTP/2 and also supports server streaming, client streaming, and bidirectional streaming. gRPC service definitions

Choose what the contract should expose

Client needOne possible interfaceDesign consequence
Fetch a user profile through a standard HTTP interfaceGET /users/42The resource has a URI, and the response can specify HTTP caching rules.
Select fields for a profile screenA GraphQL user query selecting nameThe schema defines client choices, and resolvers supply the values.
Call a known service operation from multiple languagesA generated GetUser stubThe service definition fixes the input and output types for callers.

If public web clients need a profile representation they can cache, the first contract fits that access pattern. If internal teams need a named operation with generated interfaces, gRPC offers that tooling. In either case, the service must define what happens when a user is missing or the caller stops waiting.

Check your understanding

A CreateUser RPC times out after the handler saves the new record. Can the caller assume the operation had no effect and send a second creation?

Compare your reasoning

The caller knows only that it did not receive a successful result in time. The first creation may have committed. A repeat that must avoid duplicates needs a contract for recognizing the same logical request or looking up its outcome.

Key Takeaways

  • RPC gives remote work a named method, input, and result that a caller can use through a local stub.
  • gRPC uses a service definition to generate matching interfaces and offers unary and streaming calls.
  • Deadlines bound the caller’s wait. A timed-out write can still have taken effect on the server.

Take the quiz · Choosing an API contract

How is this lesson?

Previous lessonGraphQLNext lessonChoosing an API contract
Edit on GitHubReport on GitHub
On this page
%

On this page

Trace one call across the networkDescribe the contract with gRPCChoose what the contract should exposeCheck your understandingKey Takeaways