Database Paradigms
Before diving into specific database technologies (SQL, NoSQL, In-Memory), it's crucial to understand the foundational paradigms that dictate how data is stored, processed, and maintained in distributed systems.
1. OLTP vs. OLAP
Databases are generally optimized for one of two distinct workloads: transaction processing or analytical processing.
[!TIP] ELI5: The Grocery Store
- OLTP (The Cash Register): Fast, simple transactions. "Add milk, add eggs, take $10." It needs to be instantly updated and completely accurate so the next customer can be rung up.
- OLAP (The Corporate Office): Slow, massive analysis. "What were the average egg sales across all 500 stores over the last 5 years on rainy Tuesdays?" This takes hours and involves looking at millions of past receipts.
Online Transaction Processing (OLTP)
OLTP systems are optimized for managing a large volume of short, fast, and atomic transactions. These are your primary, operational databases.
- Workload Profile: High frequency of simple reads, inserts, updates, and deletes (CRUD).
- Response Time: Milliseconds.
- Examples: PostgreSQL, MySQL, Amazon DynamoDB.
Online Analytical Processing (OLAP)
OLAP systems are optimized for complex queries that analyze vast amounts of historical data, typically for business intelligence and reporting.
- Workload Profile: Low frequency of massive, complex read queries; periodic bulk data loads (ETL/ELT).
- Response Time: Seconds to minutes (or hours for massive batches).
- Examples: Snowflake, Google BigQuery, Amazon Redshift.
2. The CAP Theorem
When designing distributed databases, you must navigate the constraints defined by the CAP Theorem.
architecture-beta
group cap(cloud)[The CAP Theorem Trade-off]
service c(database)[Consistency] in cap
service a(database)[Availability] in cap
service p(database)[Partition Tolerance] in cap
c:R -- L:a
a:B -- T:p
p:L -- R:c
The theorem states that a distributed data store can guarantee at most two of the following three properties simultaneously during a network failure:
- Consistency (C): Every read receives the most recent write or an error. All nodes see the exact same data at the same time.
- Availability (A): Every request receives a (non-error) response, without the guarantee that it contains the most recent write.
- Partition Tolerance (P): The system continues to operate despite an arbitrary number of messages being dropped (or delayed) by the network between nodes.
[!TIP] ELI5: The Twin Telemarketers Imagine two identical twins, Alice and Bob, acting as your "database" taking phone orders.
- Network Partition (P): A storm hits, and the phones between Alice and Bob go down. They can't talk to each other to sync their order books.
- Consistency vs. Availability: A customer calls Alice. Does she accept the order (Availability), knowing Bob might have sold the last item to someone else (risking inconsistency)? Or does she tell the customer "Sorry, we are closed until I can talk to Bob" (Consistency), ensuring they never oversell? She cannot have both while the phones are down.
3. Storage Engines: B-Trees vs. LSM-Trees
At the lowest level, databases use specific data structures to store data on disk efficiently.
B-Trees (Balance Trees)
The standard for traditional relational databases (like Postgres).
- Mechanism: Data is stored in fixed-size blocks. The tree is continuously rebalanced as data is inserted or deleted.
- Strengths: Excellent for read-heavy workloads. Predictable read performance.
- Weaknesses: Modifying a small piece of data requires rewriting entire blocks, which can be slow for high-throughput write workloads.
LSM-Trees (Log-Structured Merge-Trees)
The standard for modern, high-write NoSQL databases (like Cassandra).
- Mechanism: Writes are appended sequentially to memory. Once full, it's flushed to disk as an immutable file. Background processes merge these files over time.
- Strengths: Exceptional write performance because all writes are sequential appends (no random disk jumping).
- Weaknesses: Reads are slightly slower because finding a record might require checking multiple files.