Databases & System Design•Part 1: Candid Database Experience & Intro Scenarios (1 - 5)
What is a Database Management System (DBMS), and what are the foundational differences between Relational (SQL) and Non-Relational (NoSQL) databases?
In Simple Words: A Database Management System (DBMS) is specialized system software designed to store, manage, query, and enforce integrity and security guarantees over data.
The two primary database paradigms are:
-
Relational Databases (RDBMS / SQL) (e.g., PostgreSQL, MySQL):
- Data Model: Tabular relations organized into fixed schemas with rows (tuples) and columns (attributes).
- Guarantees: Strict ACID (Atomicity, Consistency, Isolation, Durability) guarantees for critical transactional workloads.
- Queries: Standardized Structured Query Language (SQL) utilizing relational algebra (Joins, Aggregations, Grouping).
- Scaling: Scaled primarily vertically (upgrading CPU/RAM), complemented by read replicas and table partitioning.
- Best For: Financial ledgers, payment transactions, enterprise ERPs, and complex relational entities.
-
Non-Relational Databases (NoSQL) (e.g., MongoDB, Redis, DynamoDB):
- Data Models: Document stores (MongoDB), Key-Value caches (Redis), Wide-Column stores (Cassandra), Graph stores (Neo4j).
- Guarantees: Built around BASE (Basically Available, Soft-state, Eventual consistency), trading strict immediate consistency for high availability and partition tolerance (CAP theorem).
- Schema: Flexible / dynamic schema; polymorphic data and nested arrays stored directly in a single record.
- Scaling: Native horizontal scaling (automatic sharding and partition distribution across clusters).
- Best For: Real-time event streams, high-speed caching, user sessions, polymorphic catalogs, and distributed IoT data.
sql
-- 1. Relational SQL Schema: Enforcing relational integrity via foreign keys and constraints
CREATE TABLE accounts (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
account_holder VARCHAR(100) NOT NULL,
balance NUMERIC(12, 2) NOT NULL CHECK (balance >= 0),
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- ACID Transaction: Both updates succeed together or both roll back (Atomicity)
BEGIN;
UPDATE accounts SET balance = balance - 150.00 WHERE id = 'c7a8b411-...' AND balance >= 150.00;
UPDATE accounts SET balance = balance + 150.00 WHERE id = 'f9d3e822-...';
COMMIT;
javascript
// 2. Non-Relational NoSQL (MongoDB Document): Nested aggregate retrieved in a single read
db.orders.insertOne({
orderId: "ord_10892",
userId: "usr_4401",
items: [
{ sku: "KEY-01", name: "Mechanical Keyboard", price: 129.99, qty: 1 },
{ sku: "CBL-02", name: "Braided Type-C Cable", price: 14.99, qty: 2 }
],
shippingAddress: {
street: "123 Technology Way",
city: "Seattle",
state: "WA",
postalCode: "98101"
},
status: "CONFIRMED",
createdAt: new Date()
});