Database
An organized collection of data that can be easily accessed, managed, and updated. The backbone of almost every application.
What is Database?
In short
A database is an organized collection of data stored on disk or in memory, managed by software (a database management system, or DBMS) that lets applications write, read, update, and delete that data reliably and concurrently. It is the system of record almost every application depends on to remember anything between requests.
What a database actually is
A database is two things working together: the data itself, and the software that manages it. The software is the database management system (DBMS), for example PostgreSQL, MySQL, MongoDB, or Redis. When people say "the database", they usually mean the running DBMS plus the data it owns.
The job of the DBMS is to take messy raw bytes and turn them into something you can query safely while many users hit it at once. It enforces structure, handles concurrent reads and writes without corrupting data, survives crashes, and answers questions like "give me every order placed by user 4821 in the last 30 days" in milliseconds instead of you scanning a giant file by hand.
Databases split into broad families. Relational databases (PostgreSQL, MySQL) store data in tables with rows and columns and connect them with joins. NoSQL databases drop the rigid table model: document stores (MongoDB) keep JSON-like records, key-value stores (Redis, DynamoDB) map a key straight to a value, and graph databases (Neo4j) store relationships as first-class data.
How it works under the hood
At the bottom, a database stores data in fixed-size blocks called pages, usually 8KB in PostgreSQL or 16KB in MySQL InnoDB. Reading from disk is slow, so the DBMS keeps hot pages in a memory area called the buffer pool and only touches disk when it has to.
Finding one row out of ten million without scanning the whole table is the job of an index, most often a B-tree. A B-tree keeps keys sorted in a shallow, wide tree so a lookup takes a handful of page reads instead of millions. This is why a query on an indexed column stays fast as the table grows, and why an unindexed query gets slower every day.
Reliability comes from a write-ahead log (WAL). Before the database changes the actual data pages, it first appends a record of the change to an append-only log and flushes that to disk. If the server loses power mid-write, the database replays the log on restart and recovers to a consistent state. This same log is what powers transactions and the ACID guarantees, and it is also what gets shipped to replicas to keep copies in sync.
Choosing one and the trade-offs
Reach for a relational database by default. PostgreSQL or MySQL handle the vast majority of applications, give you transactions and joins, and are battle-tested for decades. You only need something else when you hit a real limit they cannot meet.
Pick a key-value store like Redis when you need sub-millisecond reads on simple lookups, such as a session cache or a leaderboard, and you can accept that the data lives mostly in memory. Pick a document store like MongoDB when your records are deeply nested and your schema changes often. Pick a wide-column or distributed store like Cassandra or DynamoDB when a single machine cannot hold your write volume and you are willing to give up joins and strong consistency for horizontal scale.
The core trade-off is consistency versus scale. A single relational node gives you clean transactions but caps out at what one machine can do. Distributed databases scale across many machines but force you to confront eventual consistency, where a write may take a moment to show up everywhere. There is no free lunch, which is what the CAP theorem formalizes.
A concrete example
Picture an e-commerce checkout. The user clicks "Place Order", and the application opens a transaction against PostgreSQL. Inside that transaction it inserts a row into the orders table, decrements stock in the products table, and inserts payment details, all as one atomic unit.
If the payment step fails, the database rolls back everything: the order disappears and the stock count returns to what it was, as if the click never happened. This is atomicity, and it is the reason you do not end up with an order that was never paid for or stock that vanished into nothing.
Meanwhile, the product catalog pages that thousands of shoppers are browsing get served from a read replica or from a Redis cache so they never touch the busy primary. The primary database is reserved for the writes that must be correct, while cheaper read paths absorb the traffic that can tolerate being a second stale. That division of labor is the everyday shape of how real systems use a database.
Where it is used in production
PostgreSQL
Open-source relational database that powers Apple, Instagram, and Reddit as their primary system of record.
Amazon DynamoDB
Fully managed key-value and document store that handles Amazon's own shopping cart and order traffic at massive scale.
Redis
In-memory key-value store used by Twitter, GitHub, and Stack Overflow for caching, sessions, and rate limiting.
MongoDB
Document database used by companies like Adobe and eBay to store flexible, nested JSON-style records.
Frequently asked questions
- What is the difference between a database and a DBMS?
- The database is the actual collection of stored data. The DBMS (database management system) is the software that creates, reads, updates, and protects that data, such as PostgreSQL or MySQL. In casual use, people say "database" to mean both together.
- What is the difference between SQL and NoSQL databases?
- SQL (relational) databases store data in tables with fixed schemas and connect them with joins, giving strong transactions. NoSQL databases drop the table model for documents, key-value pairs, or graphs, trading joins and strict consistency for flexible schemas and easier horizontal scaling.
- Why is an index important in a database?
- An index, usually a B-tree, lets the database find a specific row in a few reads instead of scanning the whole table. Without an index, a lookup gets slower as the table grows. With one, it stays fast even at millions of rows. The cost is slightly slower writes and extra storage.
- What does it mean that a database is ACID compliant?
- ACID means transactions are Atomic (all steps succeed or none do), Consistent (the data stays valid), Isolated (concurrent transactions do not corrupt each other), and Durable (committed data survives a crash). Most relational databases like PostgreSQL guarantee this.
- How does a database survive a crash without losing data?
- It uses a write-ahead log. Every change is appended to a log on disk before the actual data is modified. If the server crashes, the database replays the log on restart to recover to a consistent state, so committed transactions are not lost.
Learn Database hands-on
This page explains the idea. The full lesson lets you step through the ring as servers join and leave, read the implementation, and check yourself with a quiz. It is one of 760+ lessons in the System Design Masterclass, from your first API call to distributed consensus. Eleven Foundation lessons are free, no signup. Lifetime access is ₹499 in India or $7.99 worldwide, one payment, no subscription.
Related lessons
Lessons that touch on Database as part of a larger topic.
Database Query Caching
Letting the database cache query results so the same question gets an instant answer
foundation · caching strategies
Database Functions
Reusable logic that lives inside the database, computed columns, transformations, and business rules
foundation · database fundamentals
Database Indexing
B-trees, hash indexes, composite indexes, covering indexes, the single biggest performance lever in any database
foundation · database fundamentals
Database Connection Pooling
PgBouncer, HikariCP, and why opening a new database connection per request is a recipe for disaster
foundation · database fundamentals
Time-Series Databases
Databases built for timestamped data, metrics, IoT sensors, financial ticks, and observability
intermediate · database types storage
See also
Related glossary terms you might want to look up next.
SQL
Structured Query Language for managing relational databases. Tables, rows, columns, and powerful joins to query related data.
NoSQL
Databases that don't use traditional table-based relational models. Includes document stores, key-value, graph, and column-family databases.
ACID
Four guarantees for database transactions: Atomicity (all or nothing), Consistency (valid states only), Isolation (no interference), Durability (changes persist).
BASE
An alternative to ACID for distributed systems: Basically Available, Soft state, Eventually consistent. Trades strong consistency for availability.
Sharding
Splitting a database into smaller pieces (shards) distributed across multiple servers. Each shard holds a subset of the data.
Replication
Keeping copies of the same data on multiple servers. Improves read performance and provides fault tolerance if one server goes down.