Skip to main content

Welcome to a Grasp lesson

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

Lesson 19

Data Storage Strategies

This lesson is built with resources from youtube.com, algomaster.io and 1 more site.

Hello! Welcome to the final lesson of our module on Distributed Data Storage & Systems.

In our previous lessons, we've taken a deep dive into specific NoSQL database paradigms. We explored document databases like MongoDB, massive-scale column-family stores like Cassandra, and lightning-fast key-value stores like Redis. You've seen how each is architected for a particular purpose.

Today, we'll synthesize that knowledge. Our goal is to choose an appropriate storage technology based on data access patterns and consistency requirements. This lesson moves from understanding what these databases are to deciding when and why to use them—a fundamental skill for any data architect. Given your background as an enterprise architect, this process of analyzing requirements and weighing trade-offs to select the right tool for the job will be a familiar one, just applied to the data storage landscape.

A Framework for Choosing a Database

There is no single "best" database. The ideal choice is always a function of your specific needs. A system designed for high-frequency financial transactions will have vastly different storage requirements from one that archives IoT sensor data.

To make an informed decision, we need a structured framework. Let's start by watching a video that outlines the key criteria architects use to evaluate and select a database.

Types of Databases | Criteria to choose the best database in the System Design Interview

The video "Types of Databases" from the Think Software channel provides a great overview of the criteria used in database selection. It sets the stage for our decision-making framework.

Please watch the segment from 02:08 to 08:11. As you watch, focus on these four core technical criteria: Data Model: How is the data structured? Scalability: How does the system handle growth? Query and Data Access Patterns: How will the application read and write data? Consistency: Does the system require strict ACID guarantees or can it tolerate the BASE model?

The video outlines several factors, but we will focus on the four technical pillars that have the greatest architectural impact:

  1. Data Model & Schema: This is about the shape of your data.

    • Structured Data: Has a predefined, rigid schema, like the tables in a relational database. Ideal for traditional business applications (e.g., accounting, HR).
    • Semi-structured Data: Has a flexible or evolving schema. Data is self-describing, like a JSON document. Perfect for content management, product catalogs where attributes vary, or user profiles.
    • Unstructured Data: Data with no inherent structure, like images, videos, or audio files. These are typically stored in object storage, with metadata stored in a database.
    • Connected Data: Data where the relationships between entities are as important as the entities themselves. Think social networks or fraud detection rings.
  2. Query & Access Patterns: This is about how your application interacts with the data.

    • Read-heavy vs. Write-heavy: Does your application perform many more reads than writes (e.g., a blog) or vice-versa (e.g., an event logging system)?
    • Query Complexity: Do you need simple key lookups (like fetching a user session), or do you need to perform complex queries with joins and aggregations (like an analytics dashboard)?
    • Access Pattern: Do you always query by a primary key, or do you need to search on various fields?
  3. Scalability: This concerns how the database handles increasing load and data volume.

    • Vertical Scaling (Scale-Up): Adding more resources (CPU, RAM, disk) to a single server. This is the traditional approach for relational databases but has physical limits.
    • Horizontal Scaling (Scale-Out): Adding more servers to a cluster. This is the foundation of most NoSQL databases and is essential for handling massive datasets and high throughput.
  4. Consistency Requirements: This brings us back to the CAP theorem and our earlier lessons.

    • Strong Consistency (ACID): Every read receives the most recent write. This is critical for transactional systems like banking or e-commerce checkouts. Relational databases excel here.
    • Eventual Consistency (BASE): The system will eventually become consistent, but reads might temporarily return stale data. This is a common trade-off in highly available and scalable distributed systems (like Cassandra).

Mapping Requirements to Technologies

Now, let's map these criteria to the database types we've discussed. The following flowchart provides a simplified but effective mental model for navigating the decision process.

Database Technology Selection Flowchart
This flowchart shows how requirements for querying, scale, and consistency can guide you toward a specific database category like Relational, Document, Key-Value, or Wide Column stores.

To add more detail to this flow, we can use a comparison table. The article "What Are the Different Types of Databases?" contains an excellent summary.

Comparison Table of Database Types

This article from DigitalOcean provides a wealth of information, but we'll focus on its superb comparison table, which summarizes the characteristics of each database type.

Please review the section titled "Comparison Table of Database Types". You don't need to memorize it, but study the rows for Relational, Document, Key-Value, and Columnar / Wide-Column databases. Compare them across columns like "Key Strengths," "Key Weaknesses," and "Typical Workloads / Use Cases."

Based on that table and our previous lessons, here's a summary of when to choose each type:

  • Choose a Relational Database (e.g., PostgreSQL) when:

    • You need strong ACID transactional guarantees (e.g., financial data, orders).
    • Your data is highly structured and relationships are well-defined.
    • You need to perform complex queries with JOINs.
  • Choose a Document Database (e.g., MongoDB) when:

    • Your data model is semi-structured and evolves over time (e.g., product catalogs, user-generated content).
    • Data maps naturally to objects in your application code.
    • Your primary access pattern is retrieving a whole document (e.g., a user profile).
  • Choose a Key-Value Store (e.g., Redis) when:

    • You need extremely low-latency reads and writes.
    • Your access pattern is always a simple lookup by a unique key.
    • The primary use case is caching, session management, or real-time counters.
  • Choose a Column-Family Store (e.g., Cassandra) when:

    • You have massive write-heavy workloads (e.g., IoT data, logging, metrics).
    • You need linear scalability and high availability across multiple data centers.
    • You can model your data around your specific queries (query-driven design).
Test your understanding!

You are designing the storage backend for a new analytics feature that tracks every click, scroll, and mouse movement on a high-traffic website. The system needs to ingest millions of events per second and must remain available even if a server or an entire data center fails. The primary query pattern will be analyzing user behavior over time ranges for specific user sessions.

Which database type would you choose and why? Justify your choice based on the four key criteria (Data Model, Access Patterns, Scalability, Consistency).

Show answer

A column-family store like Apache Cassandra is the ideal choice.

  • Data Model: The data (user events) is time-series data. We can model this with a partition key like session_id and clustering columns like event_timestamp. Each event can have different attributes (e.g., click_target, scroll_depth), which fits the flexible column model.
  • Access Patterns: The workload is extremely write-heavy. Cassandra's architecture is optimized for high write throughput. Queries are based on time ranges within a session, which is highly efficient using clustering columns.
  • Scalability: The system requires massive horizontal scalability to handle millions of events per second, which is a core strength of Cassandra. Its peer-to-peer architecture also allows for multi-datacenter replication.
  • Consistency: For this use case, high availability is more critical than strong consistency. It's acceptable if an event takes a few milliseconds to be visible across all replicas. Cassandra's tunable consistency and BASE model are perfect for this trade-off.

Polyglot Persistence: The Right Tool for the Job

In modern systems, especially those built on a microservices architecture, it's rare for a single database to meet all needs. The practice of using multiple database technologies within a single application is called polyglot persistence.

An e-commerce application is a classic example:

  • Relational DB (PostgreSQL): Manages core transactional data like customer orders, payments, and billing information where ACID compliance is non-negotiable.
  • Document DB (MongoDB): Stores the product catalog, where each product might have a different set of attributes, making a flexible schema ideal.
  • Key-Value Store (Redis): Caches user sessions and shopping carts for fast, low-latency access.
  • Search Engine (Elasticsearch): Powers the product search functionality, providing full-text search and filtering.

Let's see this concept illustrated in a real-world context.

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

In this segment from her NDC Oslo talk, Zoe Steinkamp walks through building an e-commerce site, demonstrating polyglot persistence by adding different databases as the application's needs evolve.

Please watch from 14:30 to 21:06. Observe how the architecture starts with a relational database for the foundation (customers, orders) and then incorporates a document database to handle the complexity and flexibility of product data and user recommendations.

This practical example reinforces that as an architect, your job isn't to pick one database to rule them all. It's to decompose a system into its functional parts and choose the optimal storage solution for each part.


Conclusion

We've now completed our journey through distributed storage systems. You've seen the trade-offs of the CAP theorem, learned about different consistency models, and explored the architecture of the major NoSQL database families. Today, we tied it all together into a decision-making framework.

Key Takeaways:

  • Database selection is a process of balancing trade-offs across four key axes: Data Model, Access Patterns, Scalability, and Consistency.
  • There is no "best" database, only the best fit for a specific workload.
  • Relational databases are for structured data needing ACID transactions. Document stores are for flexible, semi-structured data. Key-Value stores are for simple, high-speed lookups. Column-family stores are for massive, write-heavy distributed workloads.
  • Polyglot persistence—using multiple database technologies in one system—is a standard practice in modern architecture, allowing you to use the right tool for each job.

This concludes Module 3. In our next lesson, we will begin Module 4: Data Processing with Apache Spark. We will shift our focus from storing data to processing it efficiently at a massive scale, starting with an introduction to the Spark architecture and how it improves upon older paradigms like MapReduce.