Api Design easy
An API returns 10,000 products. Returning all in one response is impractical. Which pagination approach is simple and stateless?
- A Offset-based: GET /products?limit=20&offset=40
- B Server stores the user's 'page state' in a session.
- C Return all 10,000 products and let the client paginate.
- D Use WebSockets to stream products one at a time.
Answer: A. Offset-based: GET /products?limit=20&offset=40
Offset-based pagination (e.g., limit & offset) is stateless and simple to implement because the server doesn't need to track user session state. The client simply requests a 'slice' of the data. However, it can suffer from performance issues on very large datasets (as the database must count and skip rows) and consistency issues if items are added/deleted while paging.
One system design question a day, with spaced repetition so the ones you miss come back.
Get the appRelated questions
- You're designing a REST API for a blog. Which endpoint best represents 'get all comments for post 123'?
- A client wants to update only the 'status' field of an order, leaving other fields unchanged. Which HTTP method is most appropriate?
- A client calls POST /users with valid data, and the user is created. What status code should the server return?
- A client retries a failed 'POST /payments' request due to network timeout. The server receives the request twice. Without idempotency handling, what can happen?
- Your API serves both mobile apps and third-party integrations. Which authentication method is most appropriate?
- Service A calls Service B, which is failing. Without protection, A keeps sending requests, cascading the failure. What pattern prevents this?