No history yet

Data Models

Choosing Your Blueprint

Before building a house, you need a blueprint. It shows where the walls go, how the rooms connect, and how the whole structure fits together. Data-intensive applications need a blueprint, too. This blueprint is called a data model. It’s a plan for how information will be organized, stored, and accessed.

The choice of data model isn't just a technical detail; it shapes how the application behaves and how easy it is to build and maintain. Different applications have different needs, so there isn’t one blueprint that fits all projects. Let's look at the most common options.

The Relational Model: A World of Tables

The most traditional blueprint is the relational model. Think of it as a collection of neatly organized spreadsheets, or tables. Each table stores a specific type of information, like customers or products. Each row in a table represents a single item (like one customer), and each column holds a specific piece of information about that item (like their name or email address).

This structure is predictable and reliable. The key to its power is how these tables connect. For example, an Orders table doesn't need to repeat a customer's full name and address for every order. Instead, it just needs a unique CustomerID to link back to the Customers table. This avoids duplication and keeps the data consistent.

Here’s a simplified look at how a Customers table might be structured:

CustomerIDFirstNameLastNameEmail
101AnyaSharmaanya.s@email.com
102BenCarterben.carter@email.com

A separate Orders table could then reference these customers using just their CustomerID.

Relational models excel when your data is highly structured and you need strong guarantees about its consistency.

But this rigid structure can create friction. Most modern programming languages think in terms of objects—flexible containers of data. A Customer object might hold not just the customer's info, but also a list of their past orders. Trying to map this single, rich object to multiple, separate tables is a classic challenge.

Impedance Mismatch

noun

The difficulty that arises when trying to map the object-oriented model of an application to the relational model of a database. It's like trying to fit a square peg into a round hole.

This mismatch forces developers to write extra code to translate between the application's objects and the database's tables. That translation layer can become complex and slow things down, which led to the creation of other models.

The Document Model: Self-Contained and Flexible

What if you could store your data in a format that looks just like the objects in your application? That's the idea behind the document model. Instead of splitting data across multiple tables, you store related information together in a single record, or “document.”

These documents are often stored in a format like JSON (JavaScript Object Notation), which is both human-readable and easy for machines to parse. A single document for a user could contain all their profile information and even nest their order history inside it.

{
  "customerID": 101,
  "firstName": "Anya",
  "lastName": "Sharma",
  "email": "anya.s@email.com",
  "orders": [
    {
      "orderID": 5001,
      "date": "2023-10-26",
      "total": 49.99
    },
    {
      "orderID": 5002,
      "date": "2023-11-15",
      "total": 124.50
    }
  ]
}

See how everything about Anya, including her orders, is in one place? This approach eliminates the impedance mismatch. Developers can often take an object from their code and save it directly to the database. This flexibility makes development faster, especially in the early stages of a project.

However, if you need to run queries that involve complex relationships between different types of documents (like analyzing sales trends across all products from all orders), it can be less efficient than a relational model.

The Graph Model: All About Relationships

Some applications are all about connections. Think of social networks (who is friends with whom?), fraud detection (how is this transaction linked to known fraudulent accounts?), or recommendation engines (what do people who bought this item also buy?). For these, the relational model can become a tangled web of tables, and the document model isn't a natural fit.

Enter the graph model. This model has two main components:

  • Vertices (or nodes): These represent the entities, like a person, a product, or a place.
  • Edges (or relationships): These are the lines that connect the vertices, describing how they are related. An edge can have properties, too, like a FRIENDS_WITH edge that includes the date the friendship started.

Graph databases are optimized for exploring these relationships. A query like “Find all friends of Anya who rated the same book as her with 4 stars or more” is incredibly fast and straightforward in a graph model. In a relational database, the same query could require complex, multi-table joins that slow down significantly as the data grows.

Adapting the Blueprint

A blueprint for a house might need changes during construction. Similarly, an application's data needs often change over time. A business might need to start collecting new information, or existing data might need to be restructured. This is called schema evolution.

How easy it is to change the schema depends on the data model. In a relational database, changing the schema—like adding a new column to a table with millions of rows—can be a slow and delicate operation. The database needs to update every single row, which can lock the table and cause downtime.

Document databases are often called “schemaless,” which isn't entirely true. They don't enforce a schema at the database level, meaning you can have documents with different structures in the same collection. This offers huge flexibility. If you need to add a new field, you can just start writing new documents that include it. The application code is then responsible for handling documents that might or might not have that new field.

This flexibility is a trade-off. While it makes evolution easier, it puts the burden of managing data consistency on the application itself.

Design Based on Application Needs: Model the data according to how it will be accessed (e.g., frequent queries, updates, etc.).

Choosing a data model involves understanding these trade-offs. The relational model offers structure and consistency, the document model provides flexibility and speed of development, and the graph model excels at navigating complex relationships. The right choice is the one that best fits the problem you're trying to solve.

Quiz Questions 1/5

What is the primary purpose of a data model in software development?

Quiz Questions 2/5

Which data model is best suited for an application where the most important queries involve analyzing complex connections, such as a social network or a fraud detection system?