Choosing a data model

Begin with the reads, writes, and guarantees the product needs.

Written deep dive25% previewBy Daniyal Saqib

Read the opening here. A free account gives you the full guide.

A customer buys two desk lamps at $30 each. The seller later raises the price to $35. When the customer opens their order history, it shows $70. Every query might have executed correctly; the model remembered which product was purchased but lost the price agreed to for that purchase.

We will build a small store model from its required behavior, then change one requirement. The goal is to derive and defend the important identities, relationships, and facts, not to produce an exhaustive production schema. You should be comfortable reading a table and following an ID between records.

01

Why the data model matters

A data model specifies what the system remembers, what each fact means, how records relate, and which states are valid.

A record represents something the product needs to distinguish, such as a particular order. Its fields describe that thing. Relationships connect it to other records, while rules state what combinations make sense. A schema expresses parts of this model through tables or documents, fields, keys, and constraints.

The difficult decision in our broken receipt is what “price” means. Today's catalog price and the price accepted yesterday answer different questions. Choosing a database or making the query faster cannot recover a fact we never retained.

For engineering work, a model makes these decisions explicit before different features develop conflicting assumptions. It gives us a way to check whether a change preserves the product's promises. In an interview, a small model makes the same reasoning inspectable: the interviewer can see what you think the system does, question an assumption, and follow your adaptation. In both settings, you should be able to connect each important modeling decision to the behavior it supports.

02

Start with what the product needs to do and remember

The operations a product supports and the questions it must answer determine which facts are worth storing.

Our store needs to show today's products, accept purchases containing several items, and display previous receipts. Those are different reads and writes. Opening a product asks, “What is offered now?” Opening a receipt asks, “What did this customer purchase on that occasion?”

We will require each receipt to retain purchased quantities, agreed unit prices, and the product titles shown at checkout. That last requirement is a choice: another product might show current titles on old orders. We need to establish the intended behavior before deciding where the title belongs.

Each order uses one currency. Prices are stored in that currency's minor unit; in our USD examples, 3000 means $30. Checkout must establish that the accepted item prices use the order's currency. Inventory, tax, shipping, and external payment processing are outside this initial model.

Start with Product and Order. An order containing only product IDs can identify the catalog entries involved. But where would we put the fact that this order bought two lamps? Quantity describes the product's participation in a particular purchase. We have found a missing record by tracing an operation, rather than by guessing more nouns.

5 more sections to read

Sign in to keep reading

Choosing a data model

Continue this section and read the complete guide and explore its visuals.

Free account. No payment required.

Read how we handle account information in our privacy notice.

5 more sections to read

There’s more in the full article

Continue reading with a free account.

Keep reading

Free account. No payment required.

Inside the full article

Continue the guide with 5 more sections.

  1. Give things identities and build their relationshipsRead after sign-in
  2. Decide who owns each fact and how long its meaning lastsRead after sign-in
  3. Trace an operation and identify what must remain trueRead after sign-in
  4. Evolve the model while preserving meaningRead after sign-in
  5. Build and explain a useful model in an interviewRead after sign-in
Continue reading

5 more sections to read

Sign in to keep reading

Choosing a data model

Continue this section and read the complete guide and explore its visuals.

Free account. No payment required.

Read how we handle account information in our privacy notice.