Skip to main content

Welcome to a Grasp lesson

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

Lesson 16

SQL vs. NoSQL: Choosing the Right Database

Recommended resources

Hello! Welcome to the third module of your course, Data Storage and Database Design.

In the last module, we focused on scalability and performance, culminating in a deep dive on designing a distributed rate limiter. We saw how a central data store like Redis was critical for sharing state across a distributed system, and we touched on concepts like sharding and replication. That discussion was a perfect setup for this module, where we'll focus entirely on the data layer.

Today, we tackle one of the most fundamental decisions in any system design interview: choosing the right type of database. Your choice between a relational (SQL) or non-relational (NoSQL) database dictates your system's capabilities regarding consistency, scalability, and the complexity of the data it can handle.

Our learning outcome is to articulate the trade-offs of SQL vs. NoSQL databases with specific selection criteria for different scenarios. For a senior engineer, simply listing pros and cons isn't enough. You're expected to lead a nuanced discussion, justifying your choice based on the specific requirements of the problem at hand. This lesson will equip you with a structured framework to do just that.

1. The Decision-Making Framework

Before comparing specific databases, a senior engineer should first establish the criteria for the decision. This demonstrates a requirements-driven approach. In an interview, you should begin by clarifying the system's needs along several key axes.

Exploring trade-offs between NoSQL and SQL in system design interviews

This article from Design Gurus provides a concise set of questions that form an excellent framework for evaluating database needs. These are the exact questions you should be thinking about when you first hear a system design problem.

Read the section titled '1. Defining the Context and Requirements'. Focus on the five key questions it poses, as we will use these as our guiding principles for the rest of the lesson.

To summarize and structure these points, your decision-making framework should always be grounded in these four pillars:

  1. Data Structure & Schema: Is the data highly structured and relational (like user profiles and their orders), or is it semi-structured/unstructured (like JSON blobs, logs, or user-generated content)? How likely is the schema to change over time?
  2. Query Patterns & Access: Will the system perform complex queries with JOINs across multiple entities? Or will access primarily be simple lookups by a key (e.g., fetching a user profile by user_id)?
  3. Scale & Performance: What is the expected volume of data (terabytes? petabytes?) and the required throughput (reads and writes per second)? Is low latency critical?
  4. Consistency Requirements: Does the system require strict, immediate consistency (like in a banking transaction)? Or can it tolerate eventual consistency (like a social media feed)?

With your backend experience, you've likely developed an intuition for these factors. The goal here is to formalize that intuition into a communicable framework for your interviews.


2. Comparing SQL and NoSQL

Now, let's analyze SQL and NoSQL databases through the lens of our four pillars.

SQL vs. NoSQL: Comparison
This table provides a high-level summary of the core differences between SQL and NoSQL databases across various dimensions, including their data models, scaling strategies, and consistency guarantees. It also helpfully breaks down the main categories of NoSQL databases.

SQL (Relational) Databases

Think of PostgreSQL, MySQL, or Oracle. These are the workhorses of the software industry.

  • Data Structure: They enforce a predefined, rigid schema. Data is organized into tables with rows and columns. This is excellent for ensuring data integrity and works best for well-understood, structured data.
  • Query Patterns: Their strength is the ability to perform powerful, complex queries using SQL, including JOINs and aggregations across multiple tables. They support ACID transactions (Atomicity, Consistency, Isolation, Durability), which guarantee that operations like a bank transfer either complete fully or not at all.
  • Scale: Traditionally, SQL databases scale vertically (upgrading to a more powerful server). While horizontal scaling (sharding) is possible, it is often complex to implement and manage. They are generally better suited for read-heavy workloads where you can add multiple read replicas.
  • Consistency: They provide strong consistency by default. When you write data, any subsequent read will see that data.

NoSQL (Non-Relational) Databases

NoSQL is not a single thing but a family of databases designed to address the limitations of SQL, particularly around scale and schema flexibility. The term "NoSQL" is often said to mean "Not Only SQL".

Let's watch a video that breaks down the main types.

Data Modeling in System Design Interviews w/ Meta Staff Engineer

This video from a former Meta Staff Engineer provides an excellent, interview-focused overview of the different NoSQL database types. It gives practical advice on when you might (and might not) choose each one.

Watch the segment from 03:28 to 14:35. Pay attention to the four main types discussed and their typical use cases: Relational (Postgres): The baseline for comparison. Document (MongoDB): For flexible, JSON-like data. Key-Value (Redis, DynamoDB): For simple, fast lookups. Wide-Column (Cassandra): For massive write volumes. Graph (Neo4j): For highly connected data (and why you should be cautious about choosing it in an interview).

To summarize the key NoSQL categories and their trade-offs:

Type Strengths Weaknesses Common Use Cases
Document Flexible schema, good for hierarchical data (JSON). JOIN operations are limited or non-existent. Product catalogs, user profiles, content management.
Key-Value Extremely fast lookups, simple model. Can only query by key; requires data duplication for other access patterns. Caching, session stores, real-time leaderboards.
Wide-Column Optimized for massive write throughput and queries over large datasets. Horizontally scalable. Can be more complex to model data compared to relational DBs. Time-series data (metrics, logs), IoT data, messaging apps.
Graph Excellent for managing and querying highly interconnected data. Niche use case; can be overkill. Relational DBs can handle many graph-like queries. Social networks, recommendation engines, fraud detection.

NoSQL databases generally favor the BASE model (Basically Available, Soft state, Eventually consistent) and scale horizontally by adding more servers. This makes them ideal for large-scale applications where high availability and performance are prioritized over strict consistency.


3. Application: Scenarios and Hybrid Approaches

Let's put this framework into practice. For each scenario below, pause and decide whether SQL or NoSQL would be a better fit, and why, based on our four pillars.

System Design Interview Prep: SQL vs NoSQL Databases - Exponent

This article from Exponent provides a few quick exercises to test your understanding. Try to answer each question yourself before looking at the solution.

Read the three questions in the 'Short Exercises' section. For each one, apply our decision framework (Data Structure, Query Patterns, Scale, Consistency) to arrive at your own conclusion. Then, compare your reasoning with the provided solution.

Beyond a Single Choice: Hybrid Architectures

In a real-world, complex system, the answer is rarely "just SQL" or "just NoSQL". A key mark of seniority is recognizing that different parts of a system have different needs and proposing a polyglot persistence strategy—using multiple database types.

Database Design Tips | Choosing the Best Database in a System Design Interview

Let's watch a segment that illustrates how a complex platform like Amazon might use multiple database types in concert to satisfy all of its requirements.

Watch from 18:59 to 22:31. Notice how the design leverages: An RDBMS (SQL) for inventory and recent orders, where ACID transactions are critical. A Columnar DB (Cassandra) for archiving historical orders, handling ever-increasing data. A Document DB (MongoDB/Elasticsearch) to power complex search and reporting queries over order data.

This hybrid approach allows you to use the best tool for each job, rather than forcing one database to handle workloads it's not designed for. This is a powerful concept to bring up in a senior interview.


4. How to Communicate Your Decision

Knowing the right answer is only half the battle. You need to articulate your thought process clearly.

A pragmatic interview tip from the first video (LINK) is to default to a relational database like PostgreSQL unless there's a compelling, specific reason to choose NoSQL. This is a safe and robust starting point. It shows you value consistency and data integrity, and it forces the discussion to be about explicit trade-offs.

Here is a step-by-step playbook for presenting your database choice in an interview.

Exploring trade-offs between NoSQL and SQL in system design interviews

This final reading from Design Gurus provides a fantastic script for how to structure your database discussion in an interview. It's about showing your work, not just giving an answer.

Read the section titled '9. Communicating Your Decision in an Interview'. Internalize the four steps it outlines. This is the communication framework you've been looking for.

The four steps are:

  1. Lead with Requirements: "Based on our discussion, this system needs to handle X writes/sec, ensure financial transactions are consistent, and allow for flexible user profile attributes..."
  2. Discuss Both Options: "A SQL database would give us the ACID guarantees we need for financial data. However, a NoSQL document store would better handle the flexible profile schema..."
  3. Make a Reasoned Recommendation: "For the core transaction service, I'd choose PostgreSQL for its strong consistency. For the user profiles, I'd recommend MongoDB to allow for future flexibility without expensive schema migrations."
  4. Mention Mitigation Strategies: "To scale the SQL database, we'll start with a master-slave replication setup for read-heavy loads. If we need to handle complex queries in NoSQL, we can use a search index like Elasticsearch."

Following this structure demonstrates deep, critical thinking and a mature engineering mindset.


Conclusion

Today you've developed a robust framework for one of the most important decisions in system design. You're now equipped not just with knowledge, but with a strategy to apply and communicate it effectively under interview pressure.

Key Takeaways:

  • Framework First: Always start by analyzing the problem's requirements along four axes: Data Structure, Query Patterns, Scale, and Consistency.
  • Know the Trade-offs: SQL offers structure and strong consistency (ACID) at the cost of scaling complexity. NoSQL offers scale and flexibility (BASE) at the cost of weaker consistency and limited query capabilities.
  • "NoSQL" is a Category: Differentiate between Key-Value, Document, Wide-Column, and Graph databases and their specific use cases.
  • Think Hybrid: Senior-level designs often use multiple database technologies (polyglot persistence), leveraging the best tool for each part of the system.
  • Communicate with Structure: Use the four-step playbook (Requirements -> Discuss Options -> Recommend -> Mitigate) to articulate your choice clearly and defend it.

In this lesson, we made high-level choices between database types. The next logical step is to dive into how we organize data within our chosen database. In our next lesson, we will compare schema design principles for read-heavy vs. write-heavy workloads, a topic that directly builds on the access patterns we discussed today.