Course contentsShow
← System Design
Lesson 1387 of 1,91934. Data Replication StrategiesFree lesson

Custom Merge Functions

Writing application-specific merge logic to handle complex business rules during conflicts.

Custom Merge Functions

What you'll learn: How to write application-specific merge logic to resolve replication conflicts according to your unique business rules.

The Problem with Generic Merge Strategies

You've learned generic conflict resolution approaches like Last-Write-Wins, version vectors, and even CRDTs. But what happens when your business rules are too nuanced for these standard strategies?

Imagine a hotel booking system where two guests simultaneously try to book the same room. A simple LWW merge would pick one arbitrarily—but your business might require checking if either guest is a VIP member, or preferring the longer stay, or the higher-paying rate. This is where custom merge functions shine.

What Are Custom Merge Functions?

Custom merge functions are application-defined conflict resolution logic that you write yourself to handle conflicts according to domain-specific business rules. Instead of letting the database or replication system blindly choose a winner, your application takes control.

When to Use Them

  • Business priority rules: VIP customers, loyalty tiers, regulatory requirements
  • Domain constraints: Inventory can't go negative, appointments can't overlap
  • Contextual decisions: Merge based on user roles, time zones, or external state
  • Complex aggregation: Combining partial edits from multiple sources intelligently

How They Work

When a conflict is detected (via version vectors or other mechanisms), the system invokes your custom function with both conflicting versions. Your code examines the differences, applies business logic, and returns the resolved state—or even flags it for manual review if needed.

Key Takeaway: Custom merge functions give you fine-grained control over conflict resolution by encoding your unique business rules directly into the replication logic, ensuring conflicts resolve correctly for your domain rather than relying on generic strategies that may violate business constraints.