Initializing...

ACID Transactions

Multi-document atomicity with snapshot isolation.

Atomicity

All operations succeed or all fail. No partial commits.

Isolation

Snapshot reads prevent dirty/phantom reads.

Context Manager Pattern

transfer_funds.py Auto-commit
# Automatic commit/rollback on context exit
async with client.start_session() as session:
    async with session.start_transaction() as txn:
        
        # Debit sender
        await accounts.update_one(
            {"_id": "alice"}, 
            {"$inc": {"balance": -100}}, 
            session=session
        )
        
        # Credit receiver
        await accounts.update_one(
            {"_id": "bob"}, 
            {"$inc": {"balance": 100}}, 
            session=session
        )
        
        # ✓ Commits here if no exception
        # ✗ Rolls back if exception raised

Isolation Levels

READ_COMMITTED Default

Prevents dirty reads. Sees only committed data.

REPEATABLE_READ Snapshot

Consistent reads within transaction. Prevents non-repeatable reads.

SERIALIZABLE Strict

Full isolation. Highest consistency, lowest concurrency.

Performance Trade-off

Higher isolation = lower throughput. Use READ_COMMITTED for most workloads.

Last updated: Dec 14, 2025