Highlights

Contents

BACKEND ENGINEERING / UNIT V / FIELD GUIDEAPI designand architectureResources, contracts, layers, errors and testsroutermiddlewarecontrollerservicerepositoryDBGET /orders/123200 OK{"id":123,...}one request, one clear home for each concernRESTresources, pagination, versionsRPCGraphQL and gRPCArchitecturelayers and boundariesErrors and testsmodels and pyramids
Before we start

How to read this chapter

An API is a promise to everyone who calls it, and the code behind it is a promise to everyone who will change it next year. This chapter covers both halves, designing the outside of a service so clients can rely on it and organising the inside so engineers can.

The first six parts look at the API from outside. They cover resources and REST conventions, pagination that stays fast and correct, versioning and bulk operations, idempotency keys for safe retries, the alternatives RPC, GraphQL and gRPC, and the contracts that keep clients and servers in agreement. The last four look inside. They cover the classic application layers and the architectures built around them, an error model that tells clients the truth without leaking secrets, the test suite that proves it all, and the code-quality habits that keep it changeable.

Each section starts from a Wren situation, explains the mechanism, works an example with numbers and then shows a figure. Picture this boxes give analogies, notes add detail, Watch out names real mistakes, and Interview lens gives an answer to say aloud. In one breath closes each part.

The goal is to be able to design Wren’s order API on a whiteboard, defend every URL, status code and pagination choice, and then draw where each line of the server code would live.

The running system

Meet Wren

Wren is the illustrative food-ordering service from earlier units. These are the numbers this unit uses. All are assumptions chosen for readable arithmetic.

Setting Value What it controls
Orders table 5,000,000 rows Pagination cost
Page size 20 Rows per page
Orders created at peak 50 per second Idempotency key volume
Idempotency key retention 24 hours, about 300 bytes per row Key storage
Typical list response 20 orders with restaurant and 5 items each GraphQL N+1 example
Request deadline at the gateway 300 ms gRPC deadline propagation
Test suite 1,000 unit, 200 integration, 20 end-to-end Test pyramid example
API versions v1 (deprecated), v2 (current) Versioning example

The main request in this unit is POST /orders, which user 42 sends to place a biryani order with restaurant 9. Part X follows it through every layer of the server.

The map

What is inside

Draw the shape of an API from the outside, then the shape of the code that serves it from the inside.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1REST resources2Pagination3Bulk APIs, partialupdates, versioning4Idempotency keys5RPC, GraphQL and gRPC6API contracts7Application architecture8Error handling9Backend testing10Code quality and the fullrequest
  1. 01REST resourcesSections 1 to 4. We design Wren's URLs and requests around resources, the way most public HTTP APIs are built.
  2. 02PaginationSections 5 to 8. We learn to return long lists in pages that stay fast and correct as the data grows and changes.
  3. 03Bulk APIs, partial updates, versioningSections 9 to 11. We cover operations on many items at once, precise partial updates, and how an API changes without breaking the clients already using it.
  4. 04Idempotency keysSections 12 to 13. We build the server side of the idempotency key that Unit I introduced, so that any POST can be retried safely.
  5. 05RPC, GraphQL and gRPCSections 14 to 20. We compare REST with the three other styles a backend engineer meets, and learn where each fits.
  6. 06API contractsSections 21 to 23. We make the API's promise explicit in a machine-readable contract that clients, servers and tests all share.
  7. 07Application architectureSections 24 to 27. We move inside the server and give every concern of a request a single, obvious home.
  8. 08Error handlingSections 28 to 31. We design an error model that tells clients exactly what went wrong and whether to retry, without revealing internals.
  9. 09Backend testingSections 32 to 35. We build a test suite that catches real bugs, runs fast enough to use on every change, and fails only when something is actually wrong.
  10. 10Code quality and the full requestSections 36 to 38. We close with the habits that keep backend code changeable and then trace one request through everything in the unit.
  11. +Interview prepInterview question bank, graded exercises and worked solutions.