Recommended resources
The freely available resources this lesson will direct you to.
- Database Design Tips | Choosing the Best Database in a System Design Interviewwww.youtube.com
- Choosing a Database for Systems Design: All you need to know in one videowww.youtube.com
- Different Types of Databases & When To Use Themrivery.io
- SQL vs NoSQL: How to Answer This Interview Question in ...www.hellointerview.com
Introduction
In our last lesson, we designed the contract for our services using RESTful API principles. Now, we turn our attention to the persistence layer—where the data that flows through those APIs actually lives. As a developer, you're already skilled in using SQL databases to manage data. This lesson is about levelling up from using a database to choosing one, a critical architectural responsibility on the path to a senior role.
Today, we will dissect the fundamental differences between relational (SQL) and non-relational (NoSQL) databases. You will learn to analyze a system's requirements and select the appropriate database type for the job. We'll move beyond the simple "SQL vs. NoSQL" debate and explore how modern systems often leverage a mix of technologies to achieve their goals.
1. The Fundamental Divide: Relational vs. Non-Relational
The database world is broadly split into two paradigms: SQL and NoSQL. You're familiar with the first from your work with Java and Spring Boot, where the structured nature of relational databases is a natural fit for object-relational mapping (ORM) frameworks.
-
Relational (SQL) databases, like MySQL or PostgreSQL, store data in tables with rows and columns. They enforce a predefined schema, ensuring data consistency and integrity. Their superpower is guaranteeing transactions through ACID properties (Atomicity, Consistency, Isolation, Durability), making them the bedrock for systems where reliability is paramount, such as financial or e-commerce platforms. Their primary scaling strategy is vertical—adding more power (CPU, RAM) to a single server.
-
NoSQL ("Not Only SQL") databases were born from the need to handle massive volumes of data that didn't fit neatly into tables—think social media feeds or IoT sensor data. They offer flexible data models (documents, key-value pairs, graphs) and are designed for horizontal scaling—distributing the load across many servers. To achieve this scale and high performance, they often relax strict ACID guarantees.
This comparison table provides a concise overview of the key differences.

To formalize these concepts, let's review a brief guide that lays out the strengths and weaknesses of each approach.
Different Types of Databases & When To Use Them
This guide from Rivery clearly defines both relational and NoSQL databases, highlighting their core characteristics and common use cases.
Read the following two sections: Relational Databases: As you read, connect the strengths listed (ACID, standardized SQL) to your own experience building applications. NoSQL Databases: Pay close attention to the trade-offs mentioned, such as relaxing consistency restrictions for scalability and speed.
2. A Framework for Choosing
Knowing the differences is one thing; making a decision is another. A senior developer justifies their choice based on requirements and trade-offs, not just familiarity. A great way to start is with a simple decision-making framework.
The video below walks through a practical flowchart for choosing a database. It begins with two fundamental questions:
- How structured is your data?
- Do you need strict transactional (ACID) guarantees?
Database Design Tips | Choosing the Best Database in a System Design Interview
This segment from codeKarle presents a clear, flowchart-based mental model for deciding between relational and non-relational databases.
Watch the section from the beginning of the flowchart to its initial conclusion. Focus on how the presenter links use cases like payment systems to the need for ACID guarantees, which points directly to a relational database.
This flowchart provides an excellent starting point. For more nuanced scenarios, you can use a detailed comparison table to weigh specific requirements.
Different Types of Databases & When To Use Them
This section of the Rivery guide provides a scenario-based table that directly compares when to use NoSQL versus relational databases.
Focus on the table under the heading When to Use NoSQL. This is your go-to reference for justifying a choice. Identify a few scenarios that are clear wins for NoSQL, such as "Real-time analytics or high-velocity data," and for SQL, like "Complex relationships and joins."
3. A Tour of the NoSQL Universe
"NoSQL" isn't a single technology but a diverse family of databases, each optimized for a specific type of problem. Understanding these categories is essential for selecting the right tool. Since you have a computer science background, we'll go a layer deeper to understand not just what they do, but the underlying mechanisms that drive their performance.
The following video provides an excellent technical breakdown of the most common NoSQL categories.
Choosing a Database for Systems Design: All you need to know in one video
This video from "Jordan has no life" goes beyond surface-level descriptions to discuss the underlying technologies (e.g., data models, replication strategies, index types) that differentiate various databases.
Watch the following segments to understand the main NoSQL categories: Document Databases: Watch the section on MongoDB. Note how it uses a document model for data locality but shares some underlying technology (B-trees, transactions) with SQL databases. Wide-Column Stores: Watch the section on Cassandra. This is a classic example of a database built for massive write throughput. Pay attention to the concepts of leaderless replication and Log-Structured Merge (LSM) trees, which favor write speed. Key-Value Stores: Watch the section on Redis and Memcache. Understand why their in-memory nature and simple hash-map structure make them ideal for caching and other low-latency use cases. Graph Databases: Watch the section on Neo4j. The key insight here is why native graph databases are more efficient than SQL for traversing relationships—they use direct pointers instead of index lookups for joins.
After watching, you can use the Rivery article as a quick reference to solidify your understanding of the common use cases for each type.
4. The Modern View: Polyglot Persistence
The "SQL vs. NoSQL" debate has matured significantly over the last decade. The idea that you must choose one over the other for your entire application is now outdated. Modern SQL databases, especially PostgreSQL, have adopted features like JSONB support that allow them to handle semi-structured data effectively.
More importantly, the dominant architectural pattern today is polyglot persistence: using multiple databases in a single system, each chosen for the specific job it does best.
For example, a modern e-commerce application might use:
- A PostgreSQL database for its core transactional data (orders, payments, inventory) where ACID compliance is non-negotiable.
- An Elasticsearch cluster (a type of document database) to power product search.
- A Redis cache (a key-value store) to store user sessions and frequently accessed data.
- A Cassandra cluster (a wide-column store) to log user activity and analytics events at high volume.
This pragmatic approach acknowledges that there is no one-size-fits-all solution. The Hello Interview article provides an excellent summary of this modern perspective.
SQL vs NoSQL: How to Answer This Interview Question in ...
This article provides a sophisticated, modern take on the SQL vs. NoSQL debate, focusing on pragmatism and real-world application.
Read the following sections: A Quick History Lesson: This provides crucial context on why the NoSQL movement began, helping you understand the original problems it aimed to solve. The False Dichotomy: This section introduces the concept of "polyglot persistence" and is the key takeaway for this part of the lesson. It reflects how real-world systems are built today.
Conclusion
In this lesson, we moved from the perspective of a developer using a database to an architect choosing one. You now have a framework for analyzing requirements and making informed decisions about data storage, a cornerstone of system design.
Key Takeaways:
- It's a Game of Trade-offs: The choice between SQL and NoSQL hinges on balancing the needs for data structure, consistency, availability, and scalability.
- Start with Requirements: Frame your decision around the data model (structured vs. unstructured) and transactional needs (ACID vs. eventual consistency).
- Know the NoSQL Family: "NoSQL" is a broad category. Be specific about whether you need a Document, Key-Value, Wide-Column, or Graph database, as each is a specialized tool for a specific job.
- Embrace Polyglot Persistence: The most robust and scalable systems often use a combination of different database technologies, applying the right tool for the right task.
Our services can now communicate via REST APIs and persist their data in well-chosen databases. But what happens when these services need to communicate with each other without waiting for a direct response? In our next lesson, we will explore asynchronous communication by learning how message queues enable decoupling and improve the resilience of distributed systems.