
A Network Call Is Not a Method Call: Assumptions That Break Distributed Systems
- كاتب موثّق
محتويات المقال
payments.Charge(order). Now it's an await on an HTTP call, and the code around it never changed, because it looked like the same call.
The common misconception
await it, and handle exceptions the way you always do.
IPaymentsClient interface looks like any in-process service, and on a laptop the other service runs on localhost and answers in milliseconds, every time. Microsoft's microservices guide describes the trap: in a monolith, components call each other with method calls, and "A direct conversion from in-process method calls into RPC calls to services will cause a chatty and not efficient communication that won't perform well in distributed environments."
What actually happens
A remote call has a third outcome
Latency adds up
Failures multiply along a chain
The eight fallacies
The network is reliable Latency is zero Bandwidth is infinite The network is secure Topology doesn't change There is one administrator Transport cost is zero The network is homogeneous
The fix: design for three outcomes
public sealed class Checkout(HttpClient http){ public async Task<bool> PayAsync(Charge charge) { try { var response = await http.PostAsJsonAsync("/charges", charge); return response.IsSuccessStatusCode; } catch (Exception) { return false; // timed out... but the card may have been charged } }}// The page says "Payment failed, try again",// and the second click charges the card againpublic enum Outcome { Paid, Rejected, Unknown }public sealed class Checkout(HttpClient http, ILogger<Checkout> log){ public async Task<Outcome> PayAsync(Charge charge, CancellationToken ct) { using var cts = CancellationTokenSource.CreateLinkedTokenSource(ct); cts.CancelAfter(TimeSpan.FromSeconds(3)); // a deadline for this call try { var res = await http.PostAsJsonAsync("/charges", charge, cts.Token); if (res.IsSuccessStatusCode) return Outcome.Paid; // 4xx: Payments answered no. 5xx: it may have done part of it return (int)res.StatusCode < 500 ? Outcome.Rejected : Outcome.Unknown; } catch (Exception e) when (e is HttpRequestException or OperationCanceledException) { // No answer isn't a "no": ask Payments about charge.Id first log.LogWarning(e, "Charge {ChargeId} outcome unknown", charge.Id); return Outcome.Unknown; } }}Id, the unknown case can be resolved by asking Payments about it instead of charging again. Retrying is one possible next step, with risks covered later in this path; how to hold the HttpClient itself is covered in the article on IHttpClientFactory.
How to apply it in your project
Find the calls that cross the network Search for HttpClient, gRPC clients and generated API clients, including the ones hidden behind interfaces likeIPaymentsClient. List every call a request makes.Give each one a deadline Set a timeout per call that fits the end-to-end time the caller allows, and pass the request's cancellation token along. Decide what "unknown" means for each write For every call that changes something, write down what happens when there's no answer: check status by ID, show a pending state, or reconcile later. Make chatty calls coarse Replace loops of small remote calls with one call that sends or returns the whole set, and question every synchronous chain longer than one hop.
Key takeaways
A network call can succeed, fail, or leave you not knowing; design every remote call for all three. A timeout doesn't mean the work didn't happen, so don't treat "no answer" as "no". Remote calls cost a round trip each, so APIs must be coarse-grained, not chatty. Synchronous chains add up latency and multiply the chance that something on the path fails. The eight fallacies are a checklist of what the network won't do for you; check your design against them.
📚 المصادر
The Eight Fallacies of Distributed Computing — Peter Deutsch, on James Gosling's site SE Radio 470: L. Peter Deutsch on the Fallacies of Distributed Computing (2021) Microservices and the First Law of Distributed Objects — Martin Fowler (2014) Communication in a microservice architecture — .NET Microservices e-book, Microsoft Learn Transient fault handling — Azure Architecture Center, Microsoft Learn
التعليقات (0)