In this lesson, you will learn how to resolve semantic conflicts in multi-master database architectures by applying business-logic-based "winner writes" policies.
The Challenge of Multi-Master Semantic Conflicts
In multi-master systems, updates can occur concurrently on the same row across different geographically dispersed nodes. While traditional techniques like Wound-Wait handle transaction deadlocks, they do not resolve semantic conflicts—cases where two concurrent writes are technically valid but logically incompatible.
A "winner writes" strategy relies on application-defined rules to determine the "truth" when divergent states collide, rather than relying solely on timestamps (which are prone to clock skew) or simple last-writer-wins (LWW) policies that might inadvertently discard critical data.
Implementing Logic-Based Resolution
To resolve these conflicts, your database schema must often include metadata that allows your application layer to evaluate which update takes precedence based on business rules.
Consider an inventory system where two nodes process orders. If Node A updates the quantity to 5 and Node B updates it to 8, a simple LWW might lead to stock inaccuracies. Instead, you can implement a "merge function" or a "precedence flag."
-- Schema with conflict metadata
CREATE TABLE inventory (
item_id INT PRIMARY KEY,
quantity INT,
last_modified TIMESTAMP,
origin_node_priority INT -- Lower number = higher priority
);
When the replication engine detects a conflict, your application logic evaluates the state:
def resolve_conflict(local_row, incoming_row):
# Business Rule: Higher priority node always wins,
# regardless of timestamp.
if incoming_row.origin_node_priority < local_row.origin_node_priority:
return incoming_row
# Fallback: If priorities are equal, retain the higher quantity
# to avoid overselling.
return max(local_row.quantity, incoming_row.quantity)
By decoupling the resolution from the physical database state and pushing it into the application layer, you ensure that the system adheres to business invariants (like never having negative stock) rather than simple technical ordering. This approach ensures that even in eventual consistency models, the system eventually converges on a state that satisfies your domain requirements.
Key Takeaway: In multi-master systems, semantic conflicts must be resolved using business-defined priority rules rather than relying on technical primitives like wall-clock timestamps.