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.

Online Analytical Processing (OLAP)

OLAP systems are optimized for complex queries that analyze vast amounts of historical data, typically for business intelligence and reporting.

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:

  1. Consistency (C): Every read receives the most recent write or an error. All nodes see the exact same data at the same time.
  2. Availability (A): Every request receives a (non-error) response, without the guarantee that it contains the most recent write.
  3. 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).

LSM-Trees (Log-Structured Merge-Trees)

The standard for modern, high-write NoSQL databases (like Cassandra).