APIs and communication: recap and quiz
The module in six sentences
Section titled “The module in six sentences”- REST and HTTP APIs. An HTTP API names resources, keeps no conversation between requests, and relies on the meaning of each method: GET, PUT and DELETE are idempotent and POST is not, so only the first three may be repeated after a timeout.
- RPC and gRPC. An RPC makes a remote call look local, and gRPC adds a contract, four kinds of call on HTTP/2 and deadlines, which have to be passed down the chain as the time that is left.
- Polling and long polling. A client that cannot be called has to ask: short polling asks every interval and mostly gets empty answers, long polling holds the request until there is news.
- Server-sent events and WebSockets. Server-sent events stream updates one way over a response that does not end and resume with
Last-Event-ID; a WebSocket upgrades the connection so that both sides can send. - Webhooks. A provider calls your endpoint when something happens: verify the signature on the raw body, answer 2xx first, and expect repeats and disorder.
- Synchronous or asynchronous. Wait when the next step needs the answer, hand over when it does not, and remember that every call you wait for takes a little availability.
How each one breaks
Section titled “How each one breaks”| Idea | What breaks | What you see | What helps |
|---|---|---|---|
| HTTP API | A POST is repeated after a timeout | Two orders, two charges | An idempotency key, or a PUT to a client-chosen address |
| Remote call | No deadline, or a fresh one at each hop | Calls pile up behind one slow service | A deadline set by the client and passed on as the time left |
| Polling | Many clients, a short interval | Thousands of empty requests a second | A longer interval, or long polling |
| Open channel | A dropped connection | Missed updates | Event ids and Last-Event-ID, or ids of your own on a WebSocket |
| Webhook | A slow handler, a repeat, a forged request | Retries, double effects, false events | Verify the signature, answer first, record the event ids |
| Waiting chain | Several services in a row | Availability and speed fall with each one | Hand over what can wait, and copy the data you need |
What the flow measured, and what it did not
Section titled “What the flow measured, and what it did not”The message flow of lessons 3 and 4 gave the time for two updates, at a round-trip time of 80 ms and a polling interval of 1 s: 2.2 s for short polling with 6 messages, 160 ms for long polling with 4, and 120 ms for server-sent events and for a WebSocket, with 3 each. These are floors set by round trips and by the interval. They do not include the server’s work, the headers, or the cost of holding a connection for each client, so they say which technique is faster in the best case, not what a real system will measure.
A checklist for how two parts talk
Section titled “A checklist for how two parts talk”Each question points back to the lesson that answers it.
- Does the caller need the answer to go on? If so, call and wait, with a deadline. If not, hand it over. (Synchronous or asynchronous)
- Is it a resource that is read and changed, or an action? Methods and resources, or a contract with calls. (REST and HTTP APIs, RPC and gRPC)
- May the call be repeated? GET, PUT and DELETE may; for a POST or an action, add a key. (REST and HTTP APIs)
- What deadline does the client set, and does each hop pass on the time that is left? (RPC and gRPC)
- How does a client with no open connection hear about news? Poll, hold the request, or open a channel. (Polling and long polling)
- Does the data flow one way or both ways, and what happens to updates missed during a drop? (Server-sent events and WebSockets)
- If a provider calls you, do you check the signature, answer first and ignore repeated event ids? (Webhooks)
- What does a request through several services cost in availability, and which waits can become hand-overs? (Synchronous or asynchronous)
Check your understanding
Section titled “Check your understanding”Nine questions cover the whole module. Every option is explained, whether it is right or wrong.
APIs and communication: module quiz
This quiz needs JavaScript to check your answers.
Where this goes next
Section titled “Where this goes next”The next modules build on these choices. Rate limiting decides how many requests an API accepts before it refuses, and a refusal is one more answer a client has to handle. Observability shows what the calls between services did, and security covers who is allowed to make them. The question to bring with you is the one from this module: who waits for whom, and what happens when the other side does not answer.
Your progress
Built by GhaziAlibi.
© ShardCraft. All rights reserved.