How Databases Work: Relational vs. NoSQL and Core Concepts
Learn how databases store, organize, and retrieve data — covering relational databases, SQL, NoSQL types, indexing, transactions, ACID properties, and when to use each.
Every App You Use Reads a Database Thousands of Times Per Day
When you load Instagram, a database serves your feed. When you pay for coffee, a database records the transaction. When you search for a flight, a database returns thousands of options in milliseconds. Databases are invisible infrastructure — the memory system of the digital world. Understanding how they work is foundational knowledge for developers, product managers, system designers, and anyone building software products.
What a Database Is
A database is an organized collection of structured data, managed by a Database Management System (DBMS). The DBMS handles all the hard problems: how data is stored on disk, how queries are executed efficiently, how concurrent access is managed, and how data integrity is maintained even during system failures.
The two major families are:
- Relational databases (RDBMS): Organize data in tables with rows and columns. Use SQL (Structured Query Language) for defining and manipulating data. Enforce relationships between tables. Examples: PostgreSQL, MySQL, SQLite, Microsoft SQL Server, Oracle Database.
- NoSQL databases: "Not only SQL" — a broad category including document stores, key-value stores, column stores, and graph databases. Prioritize flexibility, scale, or specific access patterns over rigid schema enforcement.
Relational Databases: Tables, Keys, and Relationships
In a relational database, all data lives in tables (called relations). Each table represents a specific entity type — Users, Orders, Products. Each row represents one instance. Each column represents an attribute.
- Primary key: A unique identifier for each row in a table. No two rows can share a primary key. Often an auto-incrementing integer or a UUID.
- Foreign key: A column in one table that references the primary key of another table, establishing a relationship between them. An Orders table might have a customer_id foreign key referencing the Users table.
- Normalization: The process of organizing data to reduce redundancy. Properly normalized databases don't store the same information in multiple places — reducing storage and preventing inconsistency.
SQL: The Language of Relational Data
SQL is a declarative language — you specify what you want, not how to get it. The four fundamental operations:
- SELECT: Retrieve data.
SELECT name, email FROM users WHERE active = true ORDER BY name; - INSERT: Add new data.
INSERT INTO orders (customer_id, product_id, quantity) VALUES (42, 7, 3); - UPDATE: Modify existing data.
UPDATE products SET price = 29.99 WHERE id = 7; - DELETE: Remove data.
DELETE FROM sessions WHERE expires_at < NOW();
Joins — combining data from multiple tables — are SQL's most powerful feature and the heart of relational querying.
ACID Properties: The Guarantee of Reliability
| Property | What It Means | Example |
|---|---|---|
| Atomicity | A transaction is all-or-nothing — either all operations succeed or none do | Bank transfer: debit and credit both succeed, or neither does |
| Consistency | A transaction brings the database from one valid state to another, obeying all rules | A transfer cannot make an account balance negative if that's prohibited |
| Isolation | Concurrent transactions behave as if they executed sequentially — no partial reads of in-progress changes | Two simultaneous seat bookings don't result in double-booking |
| Durability | Committed transactions survive system failures — written to disk, not just memory | After "payment confirmed," a server crash doesn't lose the record |
Indexing: The Secret to Fast Queries
Without indexes, every query requires scanning the entire table — called a full table scan. On a table with 100 million rows, this takes seconds. An index is a separate data structure (typically a B-tree or hash) that maps index values to row locations, enabling the database to find rows in O(log n) time instead of O(n).
Indexes dramatically speed up queries at the cost of write overhead (the index must be updated on every INSERT, UPDATE, and DELETE) and storage. Choosing what to index requires understanding your query patterns:
- Index columns used in WHERE clauses and JOIN conditions
- Composite indexes serve queries filtering on multiple columns in the indexed order
- Over-indexing slows writes; under-indexing slows reads
NoSQL Database Types
| Type | Structure | Examples | Best Use Cases |
|---|---|---|---|
| Document Store | JSON-like documents; flexible schema | MongoDB, Firestore, CouchDB | User profiles, product catalogs, content management |
| Key-Value Store | Simple key maps to a value; extremely fast | Redis, DynamoDB, Memcached | Caching, session management, shopping carts, leaderboards |
| Column-Family Store | Data stored by column rather than row; handles massive datasets | Apache Cassandra, HBase | Time-series data, analytics, IoT telemetry |
| Graph Database | Nodes and edges represent entities and relationships | Neo4j, Amazon Neptune | Social networks, recommendation engines, fraud detection |
| Search Engine | Optimized for full-text search; inverted indexes | Elasticsearch, Solr | Site search, log analysis, content discovery |
When to Use Relational vs. NoSQL
The choice depends on your data model and access patterns, not hype. Relational databases excel when:
- Data relationships are complex and important (many-to-many joins)
- Data integrity and transactions are critical (financial systems, medical records)
- Schema is stable and well-defined upfront
- You need complex ad-hoc queries
NoSQL databases excel when:
- Schema is flexible or varies per record
- Horizontal scaling across many servers is required (petabyte-scale data)
- Access patterns are simple and predictable (key-value lookups, document retrieval)
- Write throughput requirements exceed what single-server relational databases can handle
Many modern applications use polyglot persistence — multiple database types working together. A PostgreSQL database for core transactional data, Redis for caching and sessions, Elasticsearch for search functionality, and possibly a graph database for recommendation logic.
Related Articles
software
APIs Explained: How Software Systems Talk to Each Other
Learn what APIs are, how REST, GraphQL, and gRPC work, key concepts like authentication, rate limiting, and versioning, and why APIs are the internet's building blocks.
9 min read
software
How Chess Engines Outthink Human Grandmasters at Every Level
Stockfish evaluates millions of positions per second using minimax and alpha-beta pruning. AlphaZero learned from scratch with neural networks. Here's how engines surpass human play.
9 min read
software
How Fiber Optic Cables Transmit Data at Light Speed
Fiber optic cables carry 99% of international data through hair-thin glass strands using total internal reflection. Explore single-mode vs multi-mode, submarine networks, and WDM technology.
9 min read
software
How Memory Chips Store and Retrieve Information
DRAM uses capacitor cells; NAND flash uses floating gates. Learn how SSDs differ from HDDs, why Moore's Law is slowing, and how 3D NAND stacking keeps storage density growing.
9 min read