Course contentsShow
← Databases & SQL
Lesson 820 of 1,9537. Transaction ManagementFree lesson

Handling Network Latency in Distributed Commits

Apply timeout-based strategies to prevent cascading failure in distributed commit protocols.

Handling Network Latency in Distributed Commits

In this lesson, you will learn how to use timeout-based strategies to prevent network latency from causing your entire distributed system to grind to a halt.

The Problem: The "Waiting Forever" Trap

In a distributed commit—like the Two-Phase Commit protocol—the coordinator asks all participating nodes if they are ready to commit. If one participant experiences high network latency, the coordinator doesn't know if that node is slow, crashed, or disconnected. If the coordinator sits there waiting indefinitely, it holds locks on database rows across all participating nodes. This blocks other transactions, leading to a "cascading failure" where the entire system stalls because one network hop is acting up.

The Solution: Implementing Timeouts

To prevent this, we introduce timeout-based strategies. Instead of waiting for a response forever, the coordinator enforces a strict time_limit. If a response isn't received within this window, the coordinator assumes a failure occurred.

Think of it like a group project deadline: if a partner doesn't reply by the agreed-upon time, you move to your "Plan B" (usually an abort) rather than sitting idle waiting for them.

Here is a conceptual example of how a coordinator manages this:

-- Conceptual logic for a coordinator
BEGIN;
  -- Send "Prepare" to participants
  SEND prepare_request TO node_a, node_b;

  -- Set a timeout for the responses
  IF wait_for_responses(timeout_duration = '500ms') == SUCCESS THEN
    SEND commit_command;
  ELSE
    -- If we hit the timeout, abort to release locks
    SEND rollback_command;
    PRINT "Latency exceeded: Transaction aborted to maintain system health.";
  END IF;
COMMIT;

Why This Prevents Cascading Failure

By enforcing a timeout, you ensure that network latency on one node does not translate into a system-wide lockup. If the network is too slow, the coordinator forces a rollback. This keeps the database available for other transactions and prevents the "waiting" state from piling up and consuming all available connection threads. While this makes the specific transaction fail, it protects the overall integrity and availability of your distributed database environment.

Key Takeaway: Use strict timeouts during distributed commits to trade individual transaction completion for system-wide availability, ensuring that a single slow network path cannot block your entire database.