Almost every real app needs to remember things: users, messages, orders. A server's memory is wiped when it restarts, so data has to live in a database. This chapter explains what databases are, the main types, and how SQL and NoSQL databases differ, so you understand why DevTinder uses MongoDB.
In this chapter you'll learn
- what a database and a DBMS are, and why we don't just use files
- the main types of databases, with a real example of each
- how SQL (relational) and NoSQL databases differ
- what ACID means, and what "schema" means
- how to choose between SQL and NoSQL
Before you start: nothing special. Chapter 11: Creating the Server helps, because your server is what talks to the database.
What is a database?
A database is an organized collection of data that you can store, search, update and delete efficiently, even when there's a lot of it and many people use it at once.
The software that manages the database is called a DBMS (Database Management System): MySQL, PostgreSQL and MongoDB are all DBMSs. In everyday speech, people say "database" for both.
Why not just save data in a file?
You could write users to a users.json file. It works for a toy project, but breaks down quickly:
| Problem with plain files | What a database gives you |
|---|---|
| finding one user means reading the whole file | indexes find a record instantly among millions (Chapter S2 12: Logical DB Query and Compound Indexes) |
| two requests writing at once can corrupt the file | safe concurrent access, with locks and transactions |
| a crash halfway through a write can lose data | durability: committed data survives crashes |
| no rules about what data looks like | validation and constraints |
| hard to ask questions ("users over 25 in Pune") | a query language |
Types of databases
| Type | How data is stored | Example | Typical use |
|---|---|---|---|
| Relational (SQL) | tables with rows and columns, linked by keys | MySQL, PostgreSQL | banking, orders, most business apps |
| Document (NoSQL) | JSON-like documents | MongoDB | web apps, catalogues, user profiles |
| Key-value (NoSQL) | a key and its value, like a giant JavaScript object | Redis, DynamoDB | caching, sessions, counters |
| Wide-column (NoSQL) | rows with flexible, grouped columns, spread over many machines | Cassandra, HBase | huge write-heavy data, like event logs |
| Graph (NoSQL) | nodes and the relationships between them | Neo4j | social networks, recommendations, fraud detection |
| Time-series | values indexed by time | InfluxDB, TimescaleDB | monitoring, IoT sensor data |
| In-memory | everything kept in RAM | Redis | very fast caches and leaderboards |
| Distributed SQL | relational tables spread across many machines | CockroachDB | global apps that need SQL plus horizontal scaling |
| Object-oriented | objects stored as they are in code | db4o | rare today |
| Hierarchical / network | tree or graph of records | IBM IMS, IDMS | older mainframe systems |
Notice that some databases fit more than one row: Redis is both key-value and in-memory. "NoSQL" itself usually means four families: document, key-value, wide-column and graph. Some databases (ArangoDB, Azure Cosmos DB) support several of these at once; they're called multi-model.
The two you'll meet most often as a backend developer are relational (SQL) and document (NoSQL).
Relational databases (SQL)
A relational database stores data in tables. Each row is one record; each column is one field, with a fixed type.
users table:
| id | first_name | city |
|---|---|---|
| 1 | Akshay | Dehradun |
| 2 | Aditi | Pune |
orders table:
| id | user_id | amount |
|---|---|---|
| 101 | 1 | 499 |
| 102 | 1 | 1299 |
- Every table has a primary key (
id) that identifies each row uniquely. orders.user_idis a foreign key: it points to a row inusers. This link between tables is the "relation" in "relational".- You query it with SQL (Structured Query Language):
SELECT users.first_name, orders.amount
FROM users
JOIN orders ON orders.user_id = users.id
WHERE users.city = 'Dehradun';SELECT users.first_name, orders.amount
FROM users
JOIN orders ON orders.user_id = users.id
WHERE users.city = 'Dehradun';The schema (which tables and columns exist, and their types) is defined up front. Adding a new column means changing the schema (a migration).
Data is usually normalized: each fact is stored once (a user's city lives only in users), and tables are combined at query time with a JOIN.
NoSQL and document databases (MongoDB)
NoSQL means "not only SQL": databases that don't use the classic table model.
A document database like MongoDB stores data as documents: JSON-like objects. Documents live in collections (the rough equivalent of tables).
{
"_id": "665f1c...",
"firstName": "Akshay",
"city": "Dehradun",
"skills": ["Node.js", "React"],
"address": { "pin": "248001", "state": "Uttarakhand" }
}{
"_id": "665f1c...",
"firstName": "Akshay",
"city": "Dehradun",
"skills": ["Node.js", "React"],
"address": { "pin": "248001", "state": "Uttarakhand" }
}- A document can hold arrays and nested objects, so related data often lives together (this is called embedding).
- Documents in the same collection don't have to look the same. The schema is flexible: you can add a field without a migration.
- That flexibility doesn't mean "no rules". You can enforce a structure with MongoDB's own
$jsonSchemavalidation or, as DevTinder does, with Mongoose schemas in your app (Chapter S2 06: Database, Schema, Models & Mongoose). - MongoDB has supported multi-document ACID transactions since version 4.0 (2018), so "NoSQL can't do transactions" is outdated.
Here is how SQL terms map to MongoDB terms:
| SQL | MongoDB |
|---|---|
| database | database |
| table | collection |
| row | document |
| column | field |
| primary key | _id |
| JOIN | embedding, or $lookup / Mongoose populate (Chapter S2 13: ref, Populate and Thought Process of writing API's) |
SQL vs NoSQL

| SQL (relational) | NoSQL (e.g. MongoDB) | |
|---|---|---|
| Data model | tables, rows, columns | documents, key-value, graph, wide-column |
| Schema | fixed, defined up front | flexible (can still be enforced) |
| Relationships | foreign keys + JOINs | embedding, or references + lookups |
| Query language | SQL (standard) | database-specific (MongoDB Query API) |
| Scaling | traditionally vertical (a bigger machine) | designed for horizontal (more machines, called sharding) |
| Transactions | ACID, a core feature | ACID supported in MongoDB; varies by database |
| Good for | strongly related data, complex reporting, money | changing requirements, nested data, large scale |
What does ACID mean?
ACID describes how a database keeps data correct when several things happen at once or something fails:
- Atomicity: a transaction happens completely or not at all. Transferring money takes it from A and gives it to B, never just one half.
- Consistency: the data always follows the rules (constraints) before and after the transaction.
- Isolation: transactions running at the same time don't see each other's half-finished work.
- Durability: once a transaction is committed, it survives a crash or power loss.
Vertical vs horizontal scaling
- Vertical scaling (scale up): give the one database server more CPU and RAM. Simple, but there's a limit and it gets expensive.
- Horizontal scaling (scale out): spread the data across many servers. MongoDB does this with sharding. Harder to set up, but nearly unlimited.
How to choose
There's no winner; it depends on the data:
- Choose SQL when your data is highly relational (many links between entities), when you need complex queries and reports, or when correctness is critical (payments, inventory).
- Choose a document database when your data is naturally nested (a profile with skills and an address), when the shape changes often, or when you expect to scale out.
- Many companies use both: PostgreSQL for orders, MongoDB for a product catalogue, Redis for caching.
DevTinder stores users and connection requests. A user profile is one self-contained document with a nested skills array, and the app is a JavaScript stack from end to end, so MongoDB is a natural fit. You'll set it up in Chapter 13: Creating a database & mongodb.
Key takeaways
- A database stores data safely and lets you query it efficiently; a DBMS is the software that runs it.
- Main families: relational (SQL) and NoSQL (document, key-value, wide-column, graph), plus time-series, in-memory and more.
- SQL uses tables, a fixed schema and JOINs. MongoDB uses collections of JSON-like documents with a flexible schema.
- ACID = Atomicity, Consistency, Isolation, Durability. MongoDB supports multi-document ACID transactions.
- Vertical scaling = a bigger machine; horizontal scaling = more machines (sharding).
- Choose based on the shape of your data and the queries you need, not hype.
Interview questions
Q1. What is the difference between SQL and NoSQL databases?
SQL databases store data in tables with a fixed schema and link tables with foreign keys and JOINs; they're queried with SQL and traditionally scale vertically. NoSQL databases use other models (documents, key-value, graph, wide-column), usually have a flexible schema, store related data together, and are designed to scale horizontally. The choice depends on how related and how changeable your data is.
Q2. What are ACID properties?
Atomicity (all or nothing), Consistency (data always respects its rules), Isolation (concurrent transactions don't interfere) and Durability (committed data survives crashes). They guarantee that transactions keep data correct.
Q3. When would you choose MongoDB over PostgreSQL?
When the data is naturally document-shaped (nested objects and arrays read together), when the structure changes frequently, when you need easy horizontal scaling, or when a JSON-based stack makes development faster. Choose PostgreSQL for highly relational data, complex reporting and multi-table transactions at the core of the app.
Q4. What is the difference between vertical and horizontal scaling?
Vertical scaling adds resources (CPU, RAM, disk) to one machine: simple but limited and costly. Horizontal scaling adds more machines and splits the data or traffic between them: more complex, but it can grow much further. In MongoDB, splitting data across machines is called sharding.
Q5. What is a primary key and a foreign key?
A primary key uniquely identifies each row in a table (in MongoDB, the _id field). A foreign key is a column that refers to the primary key of another table, creating a relationship (like orders.user_id → users.id). In MongoDB the equivalent is storing another document's _id, as DevTinder does with fromUserId and toUserId (Chapter S2 12: Logical DB Query and Compound Indexes).
Q6. What is normalization, and what is embedding?
Normalization (SQL) stores each fact once, in its own table, and joins tables when reading; it avoids duplicate data but needs JOINs. Embedding (MongoDB) stores related data inside one document (a user's address inside the user); reads are fast and simple, but data that's shared by many documents gets duplicated. A common rule: embed data that's read together and owned by one parent; reference data that's shared or grows without limit.
Q7. Can NoSQL databases handle transactions?
Many can. MongoDB supports multi-document ACID transactions since version 4.0 (2018), and single-document writes have always been atomic. Support differs between NoSQL databases, so check each one.
What's next
Time to set up a real database. In Chapter 13: Creating a database & mongodb you'll create a free MongoDB cluster in the cloud, connect to it with MongoDB Compass, and write Node.js code that creates, reads, updates and deletes data.