Inkdown
Start writing

Study

70 filesยท12 subfolders

Shared Workspace

Study
AI eng

05-transactions-acid

Shared from "Study" on Inkdown

05 - Transactions & ACID

What is a Transaction?

A transaction is a logical unit of work that consists of one or more database operations. It represents a complete business operation that must succeed or fail as a whole.

Real-World Analogy: Bank Transfer
Plain text

ACID Properties

ACID guarantees data integrity even in the face of errors, power failures, and concurrent access.

basic-ques
core
Revision w/ Whiteboard
CN Basics - 1
CN Basics - 2
DNS
Event loop
programming-language-concepts.md
zero-language-explanation.md
DB
Quick
databases-deep-dive.md
01-introduction.md
02-relational-databases.md
03-database-design.md
04-indexing.md
05-transactions-acid.md
06-nosql-databases.md
07-query-optimization.md
08-replication-ha.md
09-sharding-partitioning.md
10-caching-strategies.md
11-cap-theorem.md
12-connection-pooling.md
13-backup-recovery.md
14-monitoring.md
15-database-selection.md
README.md
JS
core topics
Event loop
Merlin Backend
01-Orchestration.md
02-DeepResearch.md
03-Search.md
04-Scraping.md
05-Streaming.md
06-MultiProviderLLM.md
07-MemoryAndContext.md
08-ErrorHandling.md
09-RateLimiting.md
10-TaskQueue.md
11-SecurityAndAuth.md
Orchestration-2nd-draft
Mobile
Build Alternative
Bundling
metro-bundler-deep-dive.md
OpenAI Agents Python
00_OVERVIEW.md
01_AGENT_SYSTEM.md
02_RUNNER_SYSTEM.md
03_TOOL_SYSTEM.md
04_ITEMS_SYSTEM.md
05_GUARDRAILS.md
06_HANDOFFS.md
07_MEMORY_SESSIONS.md
08_MODEL_PROVIDERS.md
09_SANDBOX_SYSTEM.md
10_TRACING.md
11_RUN_STATE.md
12_CONTEXT.md
13_LIFECYCLE_HOOKS.md
14_CONFIGURATION.md
15_ERROR_HANDLING.md
16_STREAMING.md
17_EXTENSIONS.md
18_MCP_INTEGRATION.md
19_BEST_PRACTICES.md
20_ARCHITECTURE_PATTERNS.md
opencode-study
context-handling
core
Python
Alembic
Basics
sqlalchemy - fastapi
SQLAlchemy overview
tweets
system_design_for_agentic_apps.md
Agent Loop
Plain text
A - Atomicity

Definition: A transaction is treated as a single unit. Either all operations complete successfully, or all are rolled back.

Sql

Implementation:

  • Write-Ahead Log (WAL): Changes recorded in log before applying
  • If crash occurs, database replays log to complete or rollback
Plain text
C - Consistency

Definition: A transaction brings the database from one valid state to another, maintaining all defined rules (constraints, triggers, cascades).

Sql

Important Distinction:

  • Database Consistency: Enforced by constraints, triggers
  • Application Consistency: Enforced by business logic
I - Isolation

Definition: Concurrent transactions execute independently. The result is as if transactions ran serially, one after another.

Without Isolation (Problems):

Plain text
Isolation Levels

SQL standard defines 4 isolation levels. Higher level = more isolation = less concurrency.

Plain text
Sql

READ UNCOMMITTED:

Sql

READ COMMITTED (Default in PostgreSQL, Oracle):

Sql

REPEATABLE READ (Default in MySQL):

Sql

SERIALIZABLE:

Sql
D - Durability

Definition: Once a transaction is committed, it remains committed even in case of system failure (power loss, crash).

Implementation:

  • Write-Ahead Logging (WAL)
  • fsync() to ensure data reaches disk
  • Some databases offer relaxed durability for performance
Plain text

Transaction Commands

Sql

Implementation: MVCC (Multi-Version Concurrency Control)

PostgreSQL and MySQL (InnoDB) use MVCC instead of locking for isolation.

Plain text

MVCC Benefits:

  • Readers don't block writers
  • Writers don't block readers
  • No read locks needed
  • Better concurrency

MVCC Costs:

  • Storage for multiple versions
  • Need vacuuming to clean old versions

Locking

When conflicts occur, databases use locks.

Lock Types
Plain text
Explicit Locking
Sql
Deadlocks
Plain text

Avoiding Deadlocks:

  1. Always acquire locks in the same order
  2. Keep transactions short
  3. Use lower isolation levels when possible
  4. Use advisory locks for complex operations

Transaction Best Practices

Sql

Next: NoSQL Databases