Hello! Welcome to the next lesson in our exploration of NoSQL databases.
In our previous lessons, we've looked at the "what" of specific NoSQL models—the simple efficiency of key-value stores and the flexible, application-centric nature of document databases. Now, it's time to synthesize this knowledge and tackle the crucial "when" and "why."
Today's lesson directly addresses the learning outcome: Identify scenarios where a NoSQL database may be a better choice than a relational database. This isn't about finding a single "best" database, but about learning to choose the right tool for a specific job—a critical skill in both software development and technical support. You'll learn to analyze a problem's requirements concerning its data structure, scale, and access patterns to make an informed architectural decision.
The Core Trade-Off: Normalization vs. Performance at Scale
As we've discussed, relational databases are built on the principle of normalization. The goal is to eliminate data redundancy by splitting data into multiple, well-structured tables linked by keys. This ensures data integrity and is highly efficient for storage.
However, this approach has a performance cost. To retrieve a complete entity, like a user's full profile, the database must perform JOIN operations across several tables. On a small scale, this is fine. But in a large, distributed system where these tables might live on different physical servers, these joins can become slow and complex, creating performance bottlenecks.
This is the problem that many NoSQL systems were designed to solve. To understand this core trade-off, let's watch a short video.
SQL vs NoSQL - Who Wins? | Systems Design Interview 0 to 1 with Ex-Google SWE
The video 'SQL vs NoSQL - Who Wins?' does a great job of explaining the fundamental tension between normalized data in relational databases and the denormalized approach of non-relational systems.
Please watch from 00:50 to 06:17. As you watch, focus on these key points: How relational databases use normalized data and foreign keys (00:50 - 03:01). The main downside: poor data locality, which leads to slow reads and complex writes in distributed systems (03:01 - 04:13). How non-relational databases use denormalized data to solve this, and the new trade-off it introduces (e.g., more complex write operations to update duplicated data) (04:13 - 06:17).
As the video explains, the central idea is:
- Relational (SQL): Prioritizes data integrity via normalization. It offers great flexibility for querying but can suffer from performance issues at scale due to joins.
- Non-Relational (NoSQL): Prioritizes read performance and scalability by storing related data together (denormalization). This makes reads very fast but can complicate writes.
The Scalability Imperative: Vertical vs. Horizontal Scaling
One of the most compelling reasons to choose a NoSQL database is the need for massive scalability. Imagine your application suddenly needs to support millions of users. How do you handle that load?
There are two primary ways to scale a database:
- Vertical Scaling (Scaling Up): You add more resources (CPU, RAM, SSD) to a single server. This is like adding more floors to a building. It's simple at first, but you eventually hit a physical and financial limit. Most relational databases are traditionally designed to scale vertically.
- Horizontal Scaling (Scaling Out): You add more servers to your database cluster. This is like adding more buildings to your campus. It's more complex to manage but offers almost limitless scalability. Most NoSQL databases are designed from the ground up to scale horizontally.
The following video provides a clear explanation of how NoSQL databases achieve this. Given your interest in low-level systems, you'll appreciate the mechanics of partitioning and hashing.
How do NoSQL databases work? Simply Explained!
This clip from 'How do NoSQL databases work? Simply Explained!' breaks down the technical reasons for NoSQL's superior horizontal scalability.
Please watch from 00:11 to 04:10. Focus on understanding: The analogy of vertical vs. horizontal scaling (00:11 - 01:31). How NoSQL scales by removing costly relationships and using a key-value store concept at its core (01:31 - 02:20). The concept of partitioning the data across many servers using a hash function on the item's key (02:20 - 04:10).
The example of Amazon's database handling 45 million requests per second on Prime Day is a powerful testament to the power of horizontal scaling.
A Framework for Choosing: Key Scenarios
So, when does it make sense to choose NoSQL? It's about matching the database's strengths to your application's needs. Let's explore the most common scenarios.
1. Your Data is Not Naturally Relational (Flexible or Evolving Schema)
Think back to our last lesson on document databases. If you're building a system where the data doesn't fit into a neat grid of rows and columns, NoSQL is often a better fit.
- Use Case: A fitness tracking app.
- Problem: Workouts are incredibly varied. One day you might log a run (duration, distance), the next day weightlifting (sets, reps, weight), and the day after a yoga session (poses, time held).
- SQL Challenge: Modeling this in a relational database is cumbersome. You might end up with one giant
workoutstable with manyNULLcolumns, or dozens of separate tables (running_workouts,weight_workouts, etc.) requiring complex joins. - NoSQL Solution: In a document database, each workout can be a single document, containing only the fields relevant to it. This flexible, polymorphic schema handles the data's natural variety with ease.
This flexibility is also invaluable in agile development, where application requirements change frequently. You can add new fields to your documents without having to perform costly schema migrations on a massive table.
2. Your Application Requires Massive Scale and High Throughput
If you are building the next social media giant, a popular mobile game, or an Internet of Things (IoT) platform, you're planning for scale from day one.
- Use Case: An analytics service collecting user interaction data (clicks, page views, etc.) from a high-traffic website.
- Problem: The system needs to handle a massive volume of write operations from thousands of concurrent users. The data for each event is relatively small and self-contained.
- SQL Challenge: A single relational database server would quickly become a bottleneck, unable to handle the write load.
- NoSQL Solution: A horizontally-scalable NoSQL database (like a key-value or document store) can distribute this write load across hundreds or thousands of servers, ensuring high throughput and availability.
3. Your Read/Write Patterns Favor Denormalization
It's crucial to analyze how your application will access data.
- Read-Heavy Systems: Consider a product catalog for an e-commerce site. A product's details (name, description, specs, reviews) are written once but read thousands of times. Storing all of this in a single document in a NoSQL database makes retrieving a product page extremely fast—it's a single lookup, no joins required.
- Eventual Consistency is Acceptable: In distributed systems, there's often a slight delay for a data update to propagate to all server replicas. This is called eventual consistency. For many applications, this is perfectly fine. A great example is the "like" count on a social media post; if it takes a few seconds for the count to be consistent globally, no one minds. However, for a banking transaction, you need strong consistency—the update must be immediate and guaranteed. Relational databases excel at this (via ACID transactions).
To see these scenarios discussed by other developers, have a look at the following community resource.
When should I use a NoSQL database instead of ...
This Stack Overflow discussion, 'When should I use a NoSQL database instead of a relational database?', contains several excellent, practical answers from experienced developers. We'll focus on a few key insights.
Please read through the three answers highlighted below. They provide a concise summary of key decision points: Start with the answer by user Asaf. Focus on the three 'possible reasons': high availability needs, data not fitting SQL well, and situations where you are already breaking the relational model. Next, read the answer by user Despertar and Sunny Sultan. They provide a clear list of triggers, such as dealing with vast datasets, needing a flexible schema, and when 'eventual consistency' is an acceptable trade-off (the YouTube views/likes example is excellent).
Summary: SQL vs. NoSQL at a Glance
The two images below provide a fantastic summary of the entire discussion. The first is a detailed comparison table, and the second shows the broader landscape of database options.


Test your understanding!
For each of the following systems, would you lean towards a relational (SQL) or a non-relational (NoSQL) database? Briefly justify your choice based on data structure, consistency needs, and expected scale.
- A hospital's Electronic Health Record (EHR) system, which stores patient data, medical history, appointments, and billing information.
- A real-time leaderboard for a massive multiplayer online game, tracking player scores and rankings.
- The backend for a blogging platform, storing user accounts, articles, and comments.
Show answer
-
EHR System: Relational (SQL).
- Justification: The data is highly structured and interconnected (patients, appointments, billing records). Data integrity and strong consistency are paramount—you cannot have ambiguous or eventually consistent medical or billing records. The need for complex, ad-hoc queries (e.g., "find all patients with condition X who had appointment Y and were billed for Z") also strongly favors SQL's powerful query language and joins.
-
Game Leaderboard: Non-Relational (NoSQL), likely an in-memory Key-Value store like Redis.
- Justification: This system requires extremely high write and read throughput (scores changing constantly). Scalability is key. The data model is very simple (
player_id -> score). Strong consistency is not a primary concern; eventual consistency is perfectly acceptable. Fast access is more important than complex query capabilities.
- Justification: This system requires extremely high write and read throughput (scores changing constantly). Scalability is key. The data model is very simple (
-
Blogging Platform: Hybrid approach is best, but a Document DB (NoSQL) is a strong contender.
- Justification: This is a classic mixed-use case where different choices could be made.
- User Accounts/Authentication: Could be in a relational DB for its transactional security.
- Articles & Comments: This is a perfect fit for a document database. An article and its comments are a natural document ("data accessed together is stored together"). The schema for an article might evolve (e.g., adding new metadata fields), which is handled well by a flexible schema. Read performance for displaying an article with its comments is the primary goal, making a document model's single-lookup advantage very attractive. This avoids costly joins between
articlesandcommentstables.
- Justification: This is a classic mixed-use case where different choices could be made.
Conclusion
You've now reached the end of our survey of database models. The most important lesson is that the "SQL vs. NoSQL" debate is not about a winner and a loser. It's about understanding your problem and choosing the architecture that best fits its unique constraints.
Key Takeaways:
- The choice between SQL and NoSQL often boils down to a trade-off between data consistency/integrity (SQL) and scalability/flexibility (NoSQL).
- Choose SQL (Relational) when your data is highly structured, you require ACID transactions, and you need to perform complex, ad-hoc queries. Think financial systems, ERPs, or any system where data integrity is non-negotiable.
- Choose NoSQL (Non-Relational) when you need to handle massive scale, your data schema is flexible or unstructured, and your primary goal is high-speed reads/writes where eventual consistency is acceptable. Think big data analytics, IoT, content management, and many large-scale web applications.
- Often, the best solution involves a hybrid approach, using different database types for different microservices within a larger application.
Preview of the Next Lesson:
The trade-off between consistency and availability we've discussed isn't just an informal design choice—it's governed by a fundamental principle of distributed computer science. In our final lesson of the course, we will explore the CAP Theorem, which mathematically defines the choices and sacrifices you must make when designing any distributed data system.