Urgent.News

What's breaking now, across thousands of outlets.

Tech

Handling Partial Failures in Distributed Systems

Quick Answer: To handle partial failures in distributed systems, avoid complex distributed transactions. Instead, design for the "unhappy path" using an orchestration pattern (like the Saga pattern). This ensures that if a downstream service fails after a payment is processed, compensating actions automatically roll back the transaction and refund the user. Let's be honest: a lot of the code…

To prevent problems in systems made up of many separate services, it's best to avoid complex transactions that run across multiple services at once. Instead, plan for the unlikely event that something goes wrong in the middle of a process. This involves using a pattern called the Saga, which helps to undo or compensate for any steps that fail.

Many developers write code that assumes everything will go smoothly, only focusing on the happy scenario where all steps succeed. However, in reality, networks can drop packets, APIs can crash, and validations can fail. If a developer doesn't write code to handle these failures, the system can end up in an inconsistent state, like charging a customer's card but not delivering the product they paid for. This leads to losing money and disappointing customers.

The common mistake is to think that every part of a process will work perfectly, leading to a situation where a developer might charge a customer's card, wait for a downstream service to prepare the product, and then realize the downstream service failed. At this point, the customer has been charged but gets nothing in return.

The traditional approach of using something called Two-Phase Commit (2PC) involves locking databases until all services agree on a transaction. This tightly couples services, making the system slower, less scalable, and more prone to failures. Instead of using 2PC, developers should use an orchestration-based pattern like the Saga pattern. This pattern allows an orchestrator to manage the steps of a distributed workflow and automatically trigger actions to roll back the entire process if something goes wrong.

In a Saga, if a downstream service fails, the orchestrator detects the failure and automatically executes compensating actions, such as issuing a refund to the customer. This ensures that even if parts of the system fail, the system can recover to a consistent state without leaving customers without what they paid for.

To test how the system handles unexpected issues, developers can use tools that simulate problems in the environment, like testing how the system reacts when downstream services are slow to respond or fail. By designing systems that expect failures and have clear steps for handling them, developers can create more reliable and resilient distributed systems.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Tuesday 1 September →