LearnThatStack Ace your next interview
API Design · question
Question 74 of 92

How do you design APIs for scalability?

expert Pro
← All API Design questions

The full answer covers the core mechanics, when this pattern wins, and the trade-offs interviewers most often probe. It includes a code sample. Read it once at your own pace, then try to recall the structure from memory before the interview. Pro also includes this question's interview lens - the likely follow-up probes and what you can say in the room.

Full answer + code samples + AI explanations

Unlock to read the complete answer for this premium question.

Guided practice for API Design One question at a time. Answer out loud, get graded, see what you missed.
Related concept

Scale and availability: what statelessness makes possible, and what still fails

Holding no session is what lets the servers scale out. For availability, the other half, assume parts fail and stop one failure from spreading.

For this question · chapters 1 and 2 of 3

1/3 Session in memory breaks the balancer
statelessSession in memory breaks the balancerany request, any boxboxes24can answer24holding you0browserGET /cartBearer eyJhbGciOiany box can check itwhat the user gotGET /cart200GET /cart200GET /cart200load balancerpicks a boxround robinbox Aholds nothinga.internalin memory: nothingbox Bholds nothingb.internalin memory: nothingbox Cholds nothingc.internalin memory: nothingbox Djust startedd.internalin memory: nothingwhere the session livesa shared store, or the tokenthe two homes that are not a box's memorypick either one and every box is interchangeablea box's memorya shared storethe tokenadding a box is a number in a config file, not a project
statelessSession in memory breaks the balancerboxes24can answer24holding you0browserBearer eyJhbGciOi200load balancerround robinbox Aholds nothingbox Bholds nothingbox Cholds nothingbox Djust startedwhere the session livesa shared store, or the tokena shared store or the token, never the boxadding a box is a number, not a project

Session in memory breaks the balancer

  1. Most systems start with one server, box A, that keeps each session in its own memory. Box A holds sid 7f3a, the session ID for user 124, plus 811 other sessions. Nothing is wrong yet, and every request from that browser comes back with status 200. But watch two counts: how many servers are running, and how many servers can answer you.
  2. The whole chapter is about one gap: the servers that are running, and the servers that can answer you. Traffic grows, so you add a second server and put a load balancer in front of both. Now two servers are running, but only the first server, box A, can answer you. The gap opened as soon as there were two of something.
  3. The request was correct, and it still came back 401. The load balancer sent the request to box B, a server that had never seen session sid 7f3a. The user did nothing, but the load balancer signed them out. The balancer will sign them out again on roughly half of their requests.
  4. Sticky sessions are popular because the cookie pins each user to box A, the server holding the session, so the error goes away. Then box A is deployed and loses everything in its memory, so 812 people are signed out at once. Sticky routing still sends those 812 people to box A, the server that lost their sessions. So the sticky-session patch fails at exactly the moment you needed it.
  5. In practice, stateless does not mean there is no state. It means no single server owns the state. The session moves out of the server process into Redis, which keeps sid 7f3a with a thirty minute expiry. Both servers read the session from Redis, so both return 200.
  6. The other place a session can live is the client itself. The client holds a signed token that carries user 124 and an expiry time. Each server checks the signature on its own, so there is no session store to run. The trade-off is revocation, which means cancelling a session early. A shared store lets you delete a session right away. But a token works until it expires, so you keep the expiry short.
  7. This is what you mean when you say a service scales horizontally. None of the four servers holds a session, so any request can go to any server. Four servers are running, and at last all four can answer requests. A fifth server needs only a new number in a config file, because the fourth server holds nothing worth copying.
Chapter 1 · step 1 of 7

Most systems start with one server, box A, that keeps each session in its own memory. Box A holds sid 7f3a, the session ID for user 124, plus 811 other sessions. Nothing is wrong yet, and every request from that browser comes back with status 200. But watch two counts: how many servers are running, and how many servers can answer you.

1 of 7 Use ← → or swipe See the concept: all 3 chapters in it

© LearnThatStack - diagrams may not be republished without permission.

Want a quick review of the fundamentals? See the API Design cheatsheet.

← Back to all API Design questions
Pro · $10/mo

74 of 92 API Design answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime