Distributed Systems in Node.js
Concept
Distributed systems operate over unreliable networks with partial failure, retries, duplicates, reordering, clock uncertainty, replication, and consistency trade-offs.
Why It Exists
Node's async ergonomics do not remove distributed-system constraints.
Mental Model
Treat every arrow as a boundary with a cost, ownership rule, cancellation behavior, and failure mode. Node.js is effective when those boundaries are explicit instead of hidden behind framework defaults.
How It Works
The JavaScript callback runs on the main JavaScript thread. Native Node.js bindings, libuv, the operating system, worker threads, child processes, or remote services may perform work elsewhere. Completion only becomes useful when control returns to JavaScript. Under load, the important questions are what is queued, what is bounded, what can be cancelled, and which resource saturates first.
Example
type Command = {id: string; orderId: string};
async function consume(command: Command): Promise<void> {
const inserted = await insertInboxRecordIfAbsent(command.id);
if (!inserted) return;
await applyOrderChange(command.orderId);
await markInboxComplete(command.id);
}
The example is intentionally small enough to execute, but the production boundary is the important part: validate inputs, establish a deadline, propagate cancellation, classify failures, and release resources deterministically.
Production Use
Use deadlines, jittered retries, idempotency, inbox/outbox, sagas, explicit consistency, partition-aware messaging, tracing, and recovery workflows.
Security
Authenticate service identities, authorize data access, prevent cross-tenant message routing, and protect distributed locks from becoming security assumptions.
Performance
Every retry and replica consumes capacity. Measure queue age, replication lag, lock contention, hot partitions, tail latency, and recovery time.
Common Mistakes
- Believing exactly-once delivery is a broker checkbox.
- Using wall-clock timestamps as a total order.
- Holding a distributed lock across slow remote work.
Debugging
Trace one operation across services, inspect message IDs and attempts, compare clocks carefully, and reconstruct state transitions.
Testing
Test duplication, reordering, partitions, slow dependencies, leader changes, clock skew, and crash points around commits.
When Not to Use It
Do not add CQRS, event sourcing, or distributed locks without a specific problem and operational ownership.
Interview Questions
- What is partial failure?
- What does CAP actually constrain?
- How do outbox and inbox patterns work?