Introduction#
Consistency is not a binary choice in distributed systems. You can often select stronger guarantees for critical operations and weaker guarantees for latency-sensitive paths. This post explains how consistency models affect performance and shows how to make those tradeoffs explicit in Node.js applications.
Consistency Models That Matter#
Strong Consistency#
Reads always reflect the latest committed write. This provides predictable correctness but can increase latency because operations must coordinate across replicas.
Eventual Consistency#
Replicas converge over time. Reads might return stale data, but latency and availability improve. Eventual consistency is common in globally distributed systems.
Causal Consistency#
Operations that are causally related appear in order, while unrelated operations may be observed out of order. This is a middle ground between strong and eventual consistency.
Performance Impact of Consistency Choices#
- Replication round-trips: Strong consistency often waits for quorum acknowledgments.
- Write amplification: Higher consistency can increase write latency and CPU usage.
- Tail latency: Multi-region quorum writes are dominated by the slowest replica.
Practical Tradeoffs in PostgreSQL#
PostgreSQL provides strong consistency but allows tuning of durability and isolation. Lowering durability can reduce latency for non-critical data.
Node.js Example: Tuned Transactions#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import pg from "pg";
const pool = new pg.Pool({
connectionString: process.env.DATABASE_URL,
max: 20,
statement_timeout: 5000
});
export async function recordAuditEvent(event) {
const client = await pool.connect();
try {
await client.query("BEGIN");
await client.query("SET LOCAL synchronous_commit = OFF");
await client.query(
"INSERT INTO audit_log (event_id, payload) VALUES ($1, $2)",
[event.id, event.payload]
);
await client.query("COMMIT");
} finally {
client.release();
}
}
SET LOCAL synchronous_commit = OFF accepts potential data loss in the event of a crash in exchange for lower write latency. This is appropriate for audit trails that can be rebuilt from upstream sources.
Multi-Region Tradeoffs#
If you write to a quorum across regions, latency will resemble the slowest link. Many systems allow local quorum for reads and global quorum for writes to reduce read latency.
Pattern: Consistency Tiering#
Split operations into tiers.
- Tier 1: financial transactions, strong consistency, full durability
- Tier 2: user profile edits, strong consistency but relaxed durability
- Tier 3: analytics counters, eventual consistency
Monitoring Consistency Impact#
Track the following:
- Replication lag per replica
- Write latency p95 and p99
- Read staleness metrics where supported
- Failed quorum writes
Conclusion#
The best systems make consistency explicit rather than implicit. Classify operations by business impact, then apply the weakest acceptable consistency level to preserve performance without losing correctness where it matters.