Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 13

Choosing the Right NoSQL Data Model

This lesson is built with resources from youtube.com, dataversity.net and 1 more site.

Hello! Welcome back to our module on Advanced Data Modeling.

In our last lesson, we contrasted the rigid, table-based world of relational databases with the flexible paradigms of NoSQL: key-value, document, and column-family stores. We focused on what these models are and how they differ structurally.

Today, we transition from theory to practice. Your role as a data architect isn't just to know these models exist, but to know precisely when and why to use them. This lesson directly addresses the learning outcome: Identify business scenarios suitable for different NoSQL data models. We will explore real-world problems and map them to the optimal NoSQL solution, a core skill for designing effective, scalable systems. This practice of using multiple database types within a single system is often called "polyglot persistence," and it's a key philosophy in modern architecture.

A Practical Walkthrough: Architecting an E-commerce Platform

To make this tangible, we will analyze the database needs of a fictional e-commerce website. Different features of the site will present different challenges, making them perfect candidates for different types of NoSQL databases.

We'll be guided by a great presentation that uses this exact approach. It's a fantastic way to see how an architect thinks through a problem.

How to choose the right database for your application - Zoe Steinkamp - NDC Oslo 2023

Watch this overview from the talk 'How to choose the right database for your application' by Zoe Steinkamp. It sets the stage by showing how different database types emerged to solve specific problems over time.

Please watch the segment from 00:47 to 05:32. Pay attention to the timeline of how document, graph, and time-series databases appeared in response to new application needs like complex data, social media, and IoT.

Now, let's break down the e-commerce platform feature by feature and choose the right database for each job.

1. The Product Catalog: Document Databases

An e-commerce site's most crucial asset is its product catalog. Consider the variety: a shirt has sizes and colors, a jar of jam has ingredients and an expiration date, and a piece of electronics has technical specifications.

How would you model this? A relational database would require a complex web of tables (products, product_attributes, attribute_types, etc.), leading to many JOIN operations just to display one product page.

This is a classic scenario for a document database.

How to choose the right database for your application - Zoe Steinkamp - NDC Oslo 2023

This segment of the same talk explains why a document database is ideal for a product catalog.

Watch from 17:28 to 21:06. Focus on how the flexible, nested structure of a JSON document allows you to store all the varied information for a single product in one place.

As the video illustrates, storing each product as a single JSON document (e.g., in MongoDB or Azure Cosmos DB) has two huge advantages:

  • Data Locality: All information needed to render a product page is retrieved in a single, fast read operation. No expensive JOINs.
  • Schema Flexibility: You can easily add new attributes to new products (e.g., adding a "farm_sourced" field to the jam) without altering a rigid table structure and affecting millions of other product records.

This makes document databases a go-to choice for:

  • Product Catalogs
  • Content Management Systems (e.g., storing articles, blog posts)
  • User Profiles (where you might store a user's preferences, addresses, and other related info together)

2. The Shopping Cart: Key-Value Stores

Now, think about the shopping cart. It's temporary, specific to a user's session, and needs to be updated and read very quickly. When a user adds an item, they expect it to appear instantly. During a high-traffic sales event, thousands of carts are being modified simultaneously.

This is a perfect use case for an in-memory key-value store like Redis.

How to choose the right database for your application - Zoe Steinkamp - NDC Oslo 2023

This next segment discusses in-memory databases, which are typically key-value stores, for handling high-speed, transient data like shopping carts or ticket sales.

Watch from 23:57 to 26:57. Notice the emphasis on speed (hot storage/cache), data volatility, and handling high-concurrency events.

The model is simple: the key is the user's session ID, and the value is an object representing their cart's contents. Because the data resides in RAM, read/write operations are sub-millisecond.

Key-value stores excel in scenarios defined by extreme low-latency lookups:

  • Caching: Storing copies of frequently accessed data (like a popular product's details) to avoid hitting a slower, disk-based database every time.
  • User Session Management: Storing login information and user state for web applications.
  • Real-time applications: Leaderboards in games or counting votes in a live poll.

3. Recommendations & Fraud Detection: Graph Databases

Our e-commerce site wants to be smart. It should recommend products by saying, "Customers who bought X also bought Y." It also needs to detect fraudulent activity, like a network of fake accounts making suspicious purchases.

Both problems are not about the data items themselves, but about the relationships between them. This is the domain of graph databases like Neo4j.

How to choose the right database for your application - Zoe Steinkamp - NDC Oslo 2023

This part of the talk dives into how graph databases are used to model and query complex relationships.

Watch from 30:59 to 35:31. Focus on the concepts of modeling users, products, and purchases as nodes and edges to uncover patterns for recommendations and fraud detection.

In a graph model:

  • John (a user) is a node.
  • Pepsi (a product) is a node.
  • An edge labeled BUYS connects John to Pepsi.

By traversing these connections, the database can efficiently answer questions like:

  • "What other products are bought by people who also bought Pepsi?" (Recommendation)
  • "Does this new purchase come from a user connected to a known network of fraudulent accounts?" (Fraud Detection)

Graph databases are the best fit for:

  • Social Networks
  • Recommendation Engines
  • Fraud and Anomaly Detection
  • Supply Chain and Logistics (modeling dependencies)

4. Logging & Telemetry: Column-Family Databases

Our platform generates a massive amount of event data: every click, every page view, every server log entry. This data is write-heavy and has a time component. We want to analyze it to understand user behavior, but we might only care about specific metrics (e.g., "what page was viewed?" and "how long did it take to load?") across billions of events.

This "wide" and "sparse" data is a prime candidate for a column-family (or wide-column) store like Apache Cassandra or HBase.

While the video doesn't focus on this model, we can draw from our reference articles. The key idea is that data is optimized for writes and for reading specific columns from many rows.

To solidify this, let's review a clear list of use cases.

How to Choose the Right NoSQL Database

The article 'How to Choose the Right NoSQL Database' provides a concise checklist of use cases for each major NoSQL type. We'll use it to pinpoint when to use a column-oriented database.

Please read the section 'Determine NoSQL Database Type'. Specifically, review the bullet points under 'Choose Column-Oriented Database if:'.

As the article confirms, column-family databases are excellent for:

  • Logging and event management
  • Time-series data like IoT sensor readings or application metrics
  • Write-heavy applications where you need to ingest data very quickly
Test your understanding!

Scenario: You are building a new feature for the e-commerce site: a Q&A section on each product page where users can ask questions and other users can answer them.

Question: Which NoSQL model (Document, Key-Value, or Graph) would be the most appropriate for storing the questions and their associated answers for a given product? Justify your choice.

Show answer

Most Appropriate: Document Database.

A document model is the best fit. You could have a "Q&A" collection where each document corresponds to a single product. This document could contain an array of question objects. Each question object, in turn, could contain the question text, user info, and a nested array of answer objects.

  • Why it works: All Q&A data for a single product is stored together, allowing you to fetch it with one efficient read when a user loads the product page. The flexible schema easily accommodates questions with zero, one, or many answers.

  • Why others are less suitable:

    • A Key-Value store would be inefficient, as you couldn't easily query for all questions for a product or add/edit a single answer without retrieving and overwriting the entire Q&A value object.
    • A Graph database would be overkill. While you could model users, questions, and answers as nodes, the primary access pattern is simply to retrieve all Q&A for one product. The relationships are hierarchical, not a complex web, making the document model a more natural fit.

A Framework for Choosing

We've seen several scenarios, but how do you generalize this into a decision-making framework? The key is to analyze the characteristics of your data and how you need to access it.

This summary table gives a great at-a-glance comparison.

NoSQL Database Types Comparison
This table compares Key-value, Document, Graph, and Column-oriented databases across their data models, query capabilities, and typical use cases.

For a more direct guide, the Azure Architecture Center provides a powerful heuristic table.

Understand Data Models - Azure Architecture Center

The 'Understand Data Models' article from our previous lesson contains a 'Choose among models (heuristics)' table. This is an excellent cheat sheet for an architect.

Review the table under the heading 'Choose among models (heuristics)'. This table maps a specific data 'Need' to a preferred data model.

As an architect, your final decision also involves non-technical factors, such as the team's skillset, hosting costs, and community support for the database. These are crucial for successful implementation.


Conclusion

Today we put on our architect hats and moved from theory to application. We saw that for a single application like an e-commerce platform, the best solution involves using multiple, specialized databases—a document store for the catalog, a key-value store for the shopping cart, and a graph database for recommendations.

Key Takeaways:

  • Document databases are ideal for semi-structured data where you want to keep related information together, like product catalogs or user profiles. Need: Evolving aggregate shape.
  • Key-value stores are unmatched for speed and are perfect for caching, session management, and other scenarios requiring extreme low-latency lookups. Need: Extreme low-latency key lookups.
  • Graph databases shine when the relationships between data points are as important as the data itself, enabling use cases like social networks and fraud detection. Need: Deep relationship traversal.
  • Column-family databases are built for high-throughput writes and analytics on wide, sparse datasets, making them suitable for telemetry and event logging. Need: Wide, sparse, write-heavy data.

You don't choose one database to rule them all; you choose the right tool for each specific job.

In our next module, Distributed Data Storage & Systems, we will delve into the fundamental principles that make these NoSQL databases possible. We'll start by exploring the CAP theorem, a foundational concept that explains the trade-offs every distributed system must make between Consistency, Availability, and Partition Tolerance. This will give you an even deeper understanding of why these databases behave the way they do.