DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • From Database Changes to Real-Time Actions at Scale: Oracle GoldenGate and Java
  • How (and Why) to Build Dynamic Data Pipelines
  • Two AI Agents, One Database: Solving Concurrent Updates With Optimistic Locking
  • MySQL Instant DDL: Metadata Locks, Waiting Writers, and Query Pileups

Trending

  • How (and Why) to Build Dynamic Data Pipelines
  • Slopsquatting: Building a Scanner That Catches AI-Hallucinated Packages Before They Reach Production
  • Save To Bookmarks/Favorites
  • Full-Text Search Meets the Graph: Apache Lucene Inside Neo4j

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Databases
  4. Conflict Resolution: Using Last-Write-Wins vs. CRDTs

Conflict Resolution: Using Last-Write-Wins vs. CRDTs

LWW isn't always the wrong approach to use. However, CRDTs can simplify development when it comes to geo-distributed applications.

By 
Cihan B.
·
Jan. 03, 18 · Opinion
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
25.8K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Some say that the cloud is quite reliable and easily forget about the outages from 2017. Let's be more specific: a single region may not be that reliable in the cloud but multiple-region deployments are! Cloud infrastructure makes deploying multi-regional, geo-distributed applications simpler to a certain degree; many public clouds affords a wide choice of regions.

That's where the simplicity ends, however. Developing active-active geo-distributed applications is still not easy. Imagine we are maintaining a shopping cart — it is a private session of a user trying to dress up for Halloween.

  • At time t1, the user adds a costume to the cart.

  • At time t2, before the cart could replicate over to the European datacenter, the U.S. datacenter fails. The user is routed to the European datacenter.

  • At time t3, the user adds a mask that goes with the costume.

  • At time t4, the U.S. datacenter recovers. Perfect! What's in the shopping cart now?

Image title

This is a fairly straightforward case with simple geo-failover. However, even with this case, the failover causes a conflicting write with itself.

Common Techniques for Resolving Conflicts

NoSQL provides a number of techniques to resolve such write conflicts. Many use LWW (last-write-wins). Simply put, last-write-wins identifies the latest write and keeps that. We all know that clocks on machines drift, and it is easy to get skewed results. Even with NTP in place, skews can be in a half-second range, and maybe more.

NoSQL provides this by keeping a timestamp of some sort that help them decide which write came last. Databases like DynamoDB or Cassandra use LWW to process writes. With these folks, strongly consistent writes require write to a quorum of shards, thus will cost you more money. (See Q: What is the consistency model of Amazon DynamoDB? or Cassandra consistency levels.).

In some cases, LWW can be acceptable. However, in this case, the cost of LWW is a lost update: at time t5, you lose either the costume or the mask. That will be one upset user on Halloween.

Image title

We know what the ideal result would be here: to keep both items in the cart and merge them. The good news is that advanced techniques can easily give you this result — conflict-free replicated data types (CRDTs) are designed to do that.

CRDTs design a conflict resolution technique based on data type. For a set like a shopping cart, they can engage things like OR sets (observed remove sets), which provides the ideal result for the shopping cart. The idea with CRDTs is that each "type" (like a shopping cart) acts with intelligence to resolve conflicts automatically. With CRDTs at time t5, you get a shopping cart with both costume and mask.

Image title

However, there is a big problem here. Databases typically don't care to understand whether your table/document is representing a shopping cart or a product inventory. There are databases that understand structures, such as Redis structures and Riak. With CRDTs, a type like a counter, stack, list, or queue engages different techniques to resolve conflicts that are more intelligent than LWW. Instead of blindly picking one of the instances of the data, CRDTs implement sets, counters etc. correctly when concurrent writes come together. For example, Redis Sets are commonly used for bags of items and use OR sets. Each type, combined with its methods, drives conflict resolution based on the sequence of updates.

LWW isn't always the wrong approach to use. However, CRDTs can simplify development when it comes to geo-distributed apps. If you are simply looking for more reliable failover or looking to handle concurrent writes across geographies correctly, CRDTs can give you a leg up over LWW.

To dive deeper into CRDTs, read Eric Brewer's article here. To get started with Redis and CRDTs, visit here.

Database

Opinions expressed by DZone contributors are their own.

Related

  • From Database Changes to Real-Time Actions at Scale: Oracle GoldenGate and Java
  • How (and Why) to Build Dynamic Data Pipelines
  • Two AI Agents, One Database: Solving Concurrent Updates With Optimistic Locking
  • MySQL Instant DDL: Metadata Locks, Waiting Writers, and Query Pileups

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook