Transacciones Distribuidas

Mecanismos que garantizan que un conjunto de operaciones de base de datos distribuidas entre varios nodos independientes se ejecute completamente (commit) o se revierta por completo (rollback) de forma atómica.

Teorema CAP (Brewer, 2000): un sistema distribuido solo puede garantizar dos de tres — Consistencia, Disponibilidad, Tolerancia a Particiones.

        graph LR
  Center["Transacciones Distribuidas"]:::main
  Rel_decentralized_derivatives_pricing_models["decentralized-derivatives-pricing-models"]:::related -.-> Center
  click Rel_decentralized_derivatives_pricing_models "/terms/decentralized-derivatives-pricing-models"
  Rel_transaction_sharding["transaction-sharding"]:::related -.-> Center
  click Rel_transaction_sharding "/terms/transaction-sharding"
  Rel_decentralized_exchange_dex_order_book["decentralized-exchange-dex-order-book"]:::related -.-> Center
  click Rel_decentralized_exchange_dex_order_book "/terms/decentralized-exchange-dex-order-book"
  classDef main fill:#7c3aed,stroke:#8b5cf6,stroke-width:2px,color:white,font-weight:bold,rx:5,ry:5;
  classDef pre fill:#0f172a,stroke:#3b82f6,color:#94a3b8,rx:5,ry:5;
  classDef child fill:#0f172a,stroke:#10b981,color:#94a3b8,rx:5,ry:5;
  classDef related fill:#0f172a,stroke:#8b5cf6,stroke-dasharray: 5 5,color:#94a3b8,rx:5,ry:5;
  linkStyle default stroke:#4b5563,stroke-width:2px;

      

🧒 Explícalo como si tuviera 5 años

Imagina que compras una entrada de concierto online. El sitio debe hacer dos cosas al mismo tiempo: (1) cobrar tu banco, (2) darte la entrada. Si el ordenador del banco cobra pero el de las entradas falla, habrás pagado sin recibir nada. Una transacción distribuida es la regla: 'O ambas operaciones tienen éxito, o ambas se cancelan por completo como si nada hubiera pasado.' Todo o nada en varios ordenadores.

🤓 Expert Deep Dive

2PC — Protocolo Bloqueante: Si el coordinador falla tras recibir todos los votos YES, los participantes mantienen bloqueos exclusivos indefinidamente. SAGA: Cada paso T_i tiene una transacción compensatoria C_i. Un fallo en T_3 ejecuta C_2, luego C_1. Las SAGAs sacrifican el Aislamiento por disponibilidad. HTLC: Alice bloquea BTC con hash(secret); Bob bloquea ETH con el mismo hash. Alice revela el secret para reclamar el ETH, lo que permite a Bob reclamar los BTC. Teorema CAP: Consistencia vs. Disponibilidad durante particiones de red.

❓ Preguntas frecuentes

What is the main problem with Two-Phase Commit (2PC)?

It is a blocking protocol. If the coordinator crashes after collecting all 'YES' votes but before sending the final COMMIT or ABORT decision, all participant nodes freeze, holding database locks indefinitely, until the coordinator recovers.

What is the SAGA pattern and why is it preferred in microservices?

A SAGA breaks a long transaction into smaller local transactions, each with a compensating transaction that undoes it. It avoids distributed locks, making services more available, but sacrifices strict isolation (intermediate states may be visible to other services).

How does a Hash Time Lock Contract (HTLC) enable trustless atomic swaps?

Both parties lock their assets using the same cryptographic secret hash. The first party reveals the secret to claim the other's asset, and this revelation automatically unlocks the secret for the second party to claim the first's asset. If either party doesn't act within the time limit, funds are refunded.

📚 Fuentes