Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Convergent Replicated Data Types in Riak 2.0

Convergent Replicated Data Types in Riak 2.0

Talk by Gordon Guthrie, Senior Software Engineer at Basho.

Summary

A review of the CAP Theorem and the difficulties of resolving conflicts in highly distributed systems. Covering the issues and various theories on how to resolve including the use CRDTs in Riak

Details

CRDTs are used to replicate data across multiple computers in a network, executing updates without the need for remote synchronisation. This leads to merge conflicts in systems using conventional eventual consistency technology, but CRDTs are designed such that conflicts are mathematically impossible. Under the constraints of the CAP theorem they provide the strongest consistency guarantees for available/partition-tolerant (AP) settings.

The CRDT concept was first formally defined in 2007 by Marc Shapiro and Nuno Preguiça in terms of operation commutativity, and development was initially motivated by collaborative text editing. The concept of semilattice evolution of replicated states was first defined by Baquero and Moura in 1997, and development was initially motivated by mobile computing. The two concepts were later unified in 2011.

Basho has worked with the EU and Marc Shapiro's team to push CRDTs into distributed systems. Riak v2.x is the first commercial product to include this functionality

Big Data Spain

May 29, 2015
Tweet

More Decks by Big Data Spain

Other Decks in Technology

Transcript

  1. OLAP OLTP Financial s 99.9999% 99.999% 99.99% 99.9% 99% Availability

    Pressure Consistency Pressure Year End Quarter End Month End Week End End Of Day End Of Hour End Of Minute Popping a Cap in the Enterprise’s Ass Customers Company
  2. ‘Data Platform’ Riak Riak Available Replication Consistent At A Point

    In Time CRDT’s – The Magic Customers Company
  3. What is it for? • Riak is a highly-available, low-latency

    KV store • it is an open-source implementation of the Dynamo paper from Amazon • Dynamo underpins the Amazon shopping cart
  4. Why does it matter? • availability matters • latency matters

    • Amazon found every 100ms of latency cost them 1% in sales • Google found an extra 0.5 seconds in search page generation time dropped traffic by 20%
  5. …it’s a hash function… If I have: • a ring

    of 16 slots • a hashed keyspace of 2 8 Then the slots are: • Slot 1: 0 – 15 • Slot 2: 16 – 31 • Slot 3: 32 – 47 • etc, etc {key: ‘Alice’, value: ‘hello’} hash(‘Alice’, 2 8) ­> 17 Store it in Slot 2
  6. Mapped To Machines Physical Machine 1 Physical Machine 2 Physical

    Machine 3 Physical Machine 4 Physical Machine 5
  7. Machines Go Down Physical Machine 1 Physical Machine 2 Physical

    Machine 3 Physical Machine 3 Physical Machine 4 Physical Machine 5
  8. And Come Back Physical Machine 1 Physical Machine 2 Physical

    Machine 3 Physical Machine 4 Physical Machine 5
  9. • In a distributed computer system you trade:  Consistency

     Availability  Partition tolerance • Pick two  CA (Relational)  CP (Mongo, Hbase, Redis)  AP (Riak, Cassandra, Couchbase, Dynamo DB) C C A A P P CAP Theorem Relaxed consistency X
  10. • Think of C, A and P as dials on

    a dashboard • In a good implementation (like Riak) they can be tuned to achieve the most appropriate balance of consistency, availability and partition tolerance for specific workloads 0 1 2 3 4 A 5 6 7 8 9 10 11 0 1 2 3 4 P 5 6 7 8 9 10 11 0 1 2 3 4 C 5 6 7 8 9 10 11 Tuning the CAP
  11. Partitions Happen • because Riak is a highly-available data store

    it will continue to store your data • but your data will not be consistent
  12. AP Physical Machine 1 Physical Machine 2 Physical Machine 3

    Physical Machine 4 Physical Machine 5 Partition Val 1 Val 2
  13. And Come Back Physical Machine 1 Physical Machine 2 Physical

    Machine 3 Physical Machine 4 Physical Machine 5
  14. Now you have siblings Physical Machine 1 Physical Machine 2

    Physical Machine 3 Physical Machine 4 Physical Machine 5 {Val 1, Val 2}
  15. Sibling Resolution • Developers hate siblings • Developers hate resolving

    siblings • Developers play Timestamp Roulette and go for Last Write Wins • but with intercontinental lag, NTP failure, clock skew and latency sometimes Timestamps kill your data
  16. Google’s View • “Designing applications to cope with concurrency anomalies

    in their data is very error-prone, time-consuming, and ultimately not worth the performance gains.” • “We have a lot of experience with eventual consistency systems at Google.” • “We find developers spend a significant fraction of their time building extremely complex and error-prone mechanisms to cope with eventual consistency”
  17. The Partition Cycle Consistent Available Available Partitioned Not Consistent Available

    (Eventually) Consistent Available partitioned unpartitioned Developer Hell Developer Heaven CRDTs
  18. Understanding CRDTs • Not going to go into a lot

    of detail • Based on the idea of Lamport Vector Clocks • A Lamport Vector clock allows an ‘actor’ in a distributed data system to say 2 things: • this is when I changed this data • and when I changed it I knew all the things every other actor had done upto these times • Its a vector clock because it has a separate clock for every actor
  19. Where Are The Actors? • Actors can be clients-side or

    server-side or both • Riak is implementing an architecture that allows users to use server-side CRDTs • to avoid the risk of ‘actor’ explosion
  20. Client Side Actors • Alice’s Laptop • Alice’s phone •

    Alice’s iPad • Alice’s daughter’s iPad (her battery is flat) • Bob’s new laptop • Bob’s old laptop • etc, etc
  21. How They Work • Lets take a simple example with

    two actions: • add an item to a shopping cart • empty the shopping cart • these actions don’t commute: • add item then clear gives an empty cart • empty cart then add item gives a cart with one item in it
  22. How They Work Consistent Available Available Partitioned Not Consistent Available

    (Eventually) Consistent Available partitioned unpartitioned CRDTs Clear Add Item [{a, }] [{b, }] {[{a, }], [{b, }]} The ‘actor’ clearing the shopping cart didn’t know there was anything in the cart Therefore there should be something in the cart after merging the two updates
  23. How They Work Consistent Available Available Partitioned Not Consistent Available

    (Eventually) Consistent Available partitioned unpartitioned CRDTs Clear Add Item [{a, }] [{a, },{b, }] {[{a, }], [{a, },{b, }]} The ‘actor’ clearing the shopping cart knew there was something in the cart Therefore there should be nothing in the cart after merging the two updates
  24. Scary Maths Stuf • Join Semi­Lattices that have a Bottom

    and a Least Upper Bound • that have defined properties involving Associativity, Commutativity, Idempotency • with a Merge Function
  25. Native Eventually Consistent Data Types • Flags • Registers •

    Counters • Sets • Maps • can contain Flags, Registers, Counter and Maps • the basis of composable data types
  26. Flags • Can have one of two values: • enabled

    • disabled • Operations: • enable • disable • Examples - use like Booleans • has a tweet been sent? • has the customer signed up to a pricing plan?
  27. Registers • Named Binaries that have contents which have a

    value • Operations: • store new value • Examples • store “My New Post” in the field “Blog Post Title”
  28. Counters • Contain a number which can be incremented or

    decremented • Operations • increment • decrement • Examples • the number of “likes” in Facebook • the number of Twitter followers
  29. Sets • Collections of unique binaries • Operations • add

    an element • remove an element • add a list of elements • remove a list of elements • Examples • shopping cart
  30. Maps • Maps are like hash maps • Operations •

    add a named Flag, Register, Counter, Set or Map • remove a named Flag, Register, Counter, Set or Map • pass through an op for an element in the Map • Example • the Map is used to compose the data model