CSE Roadmap

Core tier · about 3 hours · 8 parts

Part 2 of 8 · 2 min read

What a data model is

The shape in which data is stored, and the habit of starting from the questions.

Think of three ways to keep the records of a small bookshop. A paper ledger with columns: one line per sale. A box of index cards: one card per customer, with their orders written on it. A wall map with strings between photos of people who know each other. Each system answers some questions quickly and others slowly.

A data model is the choice of that system.

Data model

A data model is the way a database organises information: what the basic units are, how they are connected, and what questions can be asked of them.

Example: in the relational model the units are rows in tables. In the document model the unit is a whole nested record.

The book "Designing Data-Intensive Applications" stresses that a data model affects not only how software is written but how the problem is thought about. Each model has a set of relationships it handles well. So the right first step is not "which database is popular?" but "what does the data look like, and what must be asked of it?".

Three kinds of relationship appear in nearly every application.

One-to-many, many-to-one, many-to-many

In a one-to-many relationship, one item owns several others. In a many-to-one relationship, several items point to one shared item. In a many-to-many relationship, items on both sides can have several partners.

Example: one customer has many orders (one-to-many). Many customers live in one city (many-to-one). Many books have many authors (many-to-many).

Five families cover most databases.

Model Basic unit Best at Typical systems
Relational Row in a table Joins, flexible questions, many-to-many PostgreSQL, MySQL
Document Nested JSON record A whole record read at once MongoDB, CouchDB
Key-value A value found by its key Very fast lookup by key Redis, DynamoDB
Wide-column A row with many sparse columns, in sorted order Huge write volumes, time series Cassandra, HBase
Graph A vertex and an edge Deep connections Neo4j

Read it as

Write the questions first. For each model, ask: is that question one lookup, one join or a long walk?

Check yourself:

  • Which relationship is "many orders belong to one customer"?
  • Why does the book advise starting from the data and the questions, not from a product?