Building Transactions: A Preview

How Do Databases Handle Concurrent Requests? 🍽️

Imagine you're at a busy restaurant where your 10 friends all place their orders at exactly the same time. Each order involves multiple steps: checking ingredients, preparing food, plating, and serving.

How does the kitchen handle this efficiently while ensuring each person gets exactly what they ordered?


The Kitchen Analogy

Busy restaurant kitchen with multiple orders

The Challenge: 10 Orders at Once

Option 1: One Order at a Time (Serial)

πŸ”’ Complete Isolation

Process:

  • Take Order #1 β†’ Prepare completely β†’ Serve
  • Take Order #2 β†’ Prepare completely β†’ Serve
  • Continue until all 10 orders done
βœ… Pros
  • No confusion or mix-ups
  • Easy to manage
  • Guaranteed correctness
❌ Cons
  • Very slow (10x longer!)
  • Kitchen staff idle most of the time
  • Customers wait unnecessarily

Option 2: Smart Interleaving (Concurrent)

⚑ Optimized Efficiency

Process:

  • Start drinks for all orders (fast step)
  • Put fries in fryer for multiple orders
  • Grill burgers while fries cook
  • Assemble orders as components finish
βœ… Pros
  • Much faster overall
  • Efficient resource usage
  • Better customer experience
⚠️ Challenges
  • Risk of mixing up orders
  • Complex coordination needed
  • Need careful tracking

The Key Insight: Isolation Guarantee

🎯 Each Customer's Expectation

Regardless of how the kitchen works internally, every customer must receive their complete, correct order β€” as if they were the only customer being served.

βœ… Good Isolation

Alice orders a burger and fries. She gets exactly her burger and fries, even though the kitchen prepared drinks for 5 other customers while her fries were cooking.

❌ Broken Isolation

Alice's fries accidentally go to Bob's order. Now Alice is missing fries and Bob has extra fries he didn't order.


The Database Version: Transaction Manager

nanoDB transaction processing overview

The Three Types of Schedules

πŸ“‹ Serial Schedule 1

Complete Transaction 1, then Transaction 2, then Transaction 3...

T1: β–ˆβ–ˆβ–ˆβ–ˆ
T2: β–ˆβ–ˆβ–ˆβ–ˆ
T3: β–ˆβ–ˆβ–ˆβ–ˆ

Safe but slow

πŸ“‹ Serial Schedule 2

Different order: Transaction 2, then Transaction 1, then Transaction 3...

T2: β–ˆβ–ˆβ–ˆβ–ˆ
T1: β–ˆβ–ˆβ–ˆβ–ˆ
T3: β–ˆβ–ˆβ–ˆβ–ˆ

Also safe but slow

⚑ Interleaved Schedule

Mix operations from different transactions smartly

T1: β–ˆ T2: β–ˆβ–ˆ T1: β–ˆ T3: β–ˆβ–ˆ T1: β–ˆβ–ˆ T2: β–ˆβ–ˆ

Fast if done correctly


What's Coming Next? πŸ—ΊοΈ

The Journey Ahead

1

πŸ—οΈ Foundation

ACID Properties

The four guarantees every transaction needs: Atomicity, Consistency, Isolation, Durability

πŸ’­ "What promises do we make to users?"

2

πŸ”€ Concurrency Control

Isolation in Practice

How to allow interleaving without breaking isolation: Locks, Timestamps, Snapshot Isolation

πŸ’­ "How do we coordinate multiple transactions?"

3

πŸ”„ Recovery

When Things Go Wrong

How to guarantee durability even when systems crash: Write-Ahead Logging, Checkpointing

πŸ’­ "How do we survive failures?"

4

🌐 Distributed Transactions

Multiple Databases

Coordinating transactions across multiple systems: Two-Phase Commit, Consensus

πŸ’­ "What about multiple restaurants in a chain?"


The Challenge Ahead

πŸ€” The Million-Dollar Question

How can we be ultra-fast while maintaining perfect correctness?

This is what transaction processing is all about.