Sub topic 6
Sketch the API and the data model
This chapter assumes you can read short Python snippets, know how HTTP APIs and relational or key-value databases work, and have already written down your requirements. The API and the data model turn those requirements into concrete access patterns. Those patterns drive your storage choice later.
Start from the operations
List the operations your functional requirements imply. Each one becomes an API call. For a simple item service, the core operations are:
- Create an item.
- Fetch one item by its identifier.
- List the items that belong to one user.
Keep the first sketch small. You can add update and delete once the core is clear. Naming only the operations that matter keeps the discussion on the design instead of on endpoint details.
Sketch the API
Write each call with its method, path, and key fields. You do not need full schemas.
POST /v1/items
Content-Type: application/json
{"owner_id": "user:42", "body": "hello"}
GET /v1/items/{item_id}
GET /v1/users/{owner_id}/items?limit=20&cursor=abc123Three details in this sketch matter.
- Identifiers. The server assigns
item_id. The client never picks it, so clients cannot collide with each other. - Versioning. The
/v1prefix lets you change the API later without breaking existing clients. - Pagination. The list call takes a
limitand acursor. A cursor is an opaque token that marks where the last page ended. It stays correct when new items arrive between requests. An offset (?page=3) shifts when rows are inserted, so it can skip or repeat items.
Read the access patterns
Compare the operations with your capacity estimate. With the assumptions from the estimation chapter (about 579 write QPS and 11,574 read QPS on average), reads outnumber writes by roughly 20 to 1. That ratio tells you to optimize the read paths: fetch by item_id, and list by owner_id.
Design the data model
A data model names the entities, their fields, and the keys used to find them. For this service there are two entities.
| Entity | Key fields | Other fields |
|---|---|---|
| User | `user_id` | name, created_at |
| Item | `item_id` | `owner_id`, body, created_at |
The two read patterns need two lookups. Fetch by ID uses the primary key item_id. Listing by owner needs an index on (owner_id, created_at), so the database can return one user's items in order without scanning the whole table.
Check the size of a record
Your estimate assumed 500 bytes per record. Confirm that the fields you chose fit that budget. If the body can be several kilobytes, the storage estimate is wrong and you should revise it. Large payloads such as images usually belong in object storage, with only a reference kept in the item record.
Common errors
- Designing tables before access patterns. You end up with a schema that needs expensive scans for the queries you actually run.
- Unbounded list calls. A list endpoint without a
limitcan return millions of rows. Always cap page size. - Leaking storage details into the API. If the cursor is a raw database offset, you cannot change the database later without breaking clients.
- Forgetting the partition key. If you plan to split data across shards, choose a key such as
owner_idthat keeps the common query on one shard.
Exercise
The list call returns a user's items, newest first. Why is a cursor safer than a page number when new items are being created, and which index supports the query?
Show answer
With a page number, a new item shifts every later item by one position, so the next page can repeat or skip items. A cursor marks the exact position of the last item returned, so it is not affected by inserts. The query is supported by an index on (ownerid, createdat), which returns one user's items in time order without a full table scan.