原书出处
Building Distributed Systems Designing scalable architectures with microservices, event-driven patterns, and robust deployment…
09 Data Storage Strategies
To handle these complex scenarios, we implement a two-phase commit protocol. Following is how it works in practice: public class DistributedTransactionManager { public TransactionContext begin() { String txId = generateTransactionId(); // Phase 1: Prepare Map<String, ServiceStatus> serviceStatuses = new HashMap<>(); for (DistributedService service : participatingServices) { try { boolean prepared = service.prepare(txId); serviceStatuses.put(service.getName(), new ServiceStatus(prepared)); } catch (Exception e) { // If any service fails to prepare, abort the transaction rollback(txId, serviceStatuses); throw new TransactionPreparationException(e); } } // Phase 2: Commit boolean success = true; for (DistributedService service : participatingServices) { try { service.commit(txId); } catch (Exception e) { success = false; // Log the failure for manual recovery if needed logFailure(txId, service.getName(), e); } } if (!success) { // Initiate recovery process for partial commit initiateRecoveryProcess(txId, serviceStatuses); throw new TransactionCommitException(); } return new TransactionContext(txId); } } This implementation guards against partial failures.
原书定位详情
来源:a19c7933-4eda-4176-aebd-30fe7e1ad292
正文位置:extracted/Building Distributed Systems Designing scalable architectures with microservices, event-driven patterns, and robust deployment…/_epub-src/OPS/ch09.xhtml
EPUB SHA-256:357a47122841cb03cd2ff1d58b4eb7ee46e4e6e2e56a004d65a18587568e05ff