System Design CourseSystem Design Course1

Relational vs NoSQL Databases: A Beginner’s Guide



Introduction

When designing a software system, choosing the right database is a critical decision. Two major categories of databases are Relational (SQL) and NoSQL databases. Each has its own strengths and use cases. In this tutorial, we'll explore what they are, how they differ, and when to use each — all explained in a beginner-friendly way with clear examples and intuitive questions.

What is a Relational Database?

A Relational Database is a type of database that stores data in tables (rows and columns). These databases use Structured Query Language (SQL) for querying and managing data.

Example: Online Bookstore

Imagine you're building an online bookstore. You might create tables like:

Each table is linked using keys — for example, AuthorID links the Books and Authors tables. This structure ensures data consistency and integrity.

Question:

Why do relational databases use keys like AuthorID?

Answer:

To maintain relationships between tables and avoid data duplication. This is called normalization.

What is a NoSQL Database?

NoSQL databases are designed for flexibility, scalability, and handling unstructured or semi-structured data. They do not use fixed table schemas and can store data as key-value pairs, documents, graphs, or wide-columns.

Example: User Profiles in a Social Media App

Let's say you're designing a social media app with millions of users. A user's profile may contain their name, bio, photos, followers, following, settings, and preferences — all of which can vary significantly from user to user.

A NoSQL document database like MongoDB can store this profile as a single document:

{
    "user_id": "u123",
    "name": "Alex",
    "bio": "Photographer & traveler",
    "followers": ["u456", "u789"],
    "settings": {
      "theme": "dark",
      "notifications": true
    }
  }

This flexible schema allows you to store different attributes for different users without changing the database structure.

Question:

Why is a NoSQL database better for user profiles?

Answer:

Because each user may have a different set of attributes, and NoSQL allows storing such dynamic data without strict schemas.

Key Differences Between Relational and NoSQL Databases

Feature Relational Database (SQL) NoSQL Database
Data Model Tables with fixed schema Flexible (Document, Key-Value, Graph, etc.)
Query Language SQL Varies (e.g., JSON-like queries for MongoDB)
Schema Rigid and predefined Schema-less or flexible
Scalability Vertical (scale-up) Horizontal (scale-out)
Consistency Strong consistency (ACID) Eventual consistency (BASE)
Examples MySQL, PostgreSQL, Oracle MongoDB, Cassandra, Redis, DynamoDB

When to Use Relational Databases

Example: Banking System

In a banking application, every transaction must be accurate and consistent. You cannot afford to lose or duplicate records. Relational databases offer ACID properties which ensure transactional safety and strong data integrity.

Question:

What might happen if eventual consistency (as used in NoSQL) is used in banking?

Answer:

A user might see an incorrect balance after a transaction due to replication delay. This can cause major issues in financial systems.

When to Use NoSQL Databases

Example: Logging System

Imagine collecting log data from thousands of servers. Each log might have different fields depending on the service. NoSQL document stores like Elasticsearch or MongoDB can store this data efficiently without a rigid schema.

Question:

Why is a schema-less structure useful for logging systems?

Answer:

Because log formats vary, and a schema-less design allows flexibility to store different types of logs without changing the database.

Summary

Relational and NoSQL databases serve different purposes. One is not better than the other — the right choice depends on your application’s requirements. Beginners should start by understanding their project’s data characteristics before choosing a database.

Questions to Build Intuition

  1. Would you use a relational database for a real-time chat app? Why or why not?
  2. How would you handle frequent schema changes in an e-commerce product catalog?
  3. Why might a relational database struggle with petabytes of sensor data from IoT devices?

Final Thought

As a system designer, you should always ask: What kind of data am I storing, and how is it accessed? Your answer will often lead you to the right type of database.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

Mention your name, and programguru.org in the message. Your name shall be displayed in the sponsers list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M