Top Banner
Algebraic data type: Semilattices a.k.a eventually consistent data structures Bernhard Huemer IRIAN Solutions @bhuemer
22

Algebraic data types: Semilattices

Dec 14, 2014

Download

Technology

Bernhard Huemer

Introduction to the algebraic data type Semilattice and its application in distributed environments.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Algebraic data types: Semilattices

Algebraic data type: Semilattices

a.k.a eventually consistent data structures

Bernhard Huemer IRIAN Solutions

@bhuemer

Page 2: Algebraic data types: Semilattices

.. because distributed is the new normal

Why are we here?

Shamelessly stolen from: https://skillsmatter.com/skillscasts/4915-how-do-we-reconcile-eventually-consistent-data

Page 3: Algebraic data types: Semilattices

Source: Wikimedia Commons

130 ms

E = MC2

Latency might be one reason why you want distribution

Shamelessly stolen from: https://skillsmatter.com/skillscasts/4915-how-do-we-reconcile-eventually-consistent-data

Page 4: Algebraic data types: Semilattices

Scale-up vs scale-out

Page 5: Algebraic data types: Semilattices
Page 6: Algebraic data types: Semilattices

foo = foo + 1

foo = foo + 2

Race conditions

Page 7: Algebraic data types: Semilattices

Network partitions

Page 8: Algebraic data types: Semilattices

Conflict resolution (1)

Not clinging to some total order will make your life easier

Page 9: Algebraic data types: Semilattices

Conflict resolution (2)

Leave it to the user to resolve conflicts, often there’s something meaningful you can do (e.g. merge shopping carts)

Page 10: Algebraic data types: Semilattices

G-Counters

Conflict resolution (3)

.. or this thing that Riak does for you

Page 11: Algebraic data types: Semilattices

Algebraic data types

Source: http://en.wikipedia.org/wiki/Algebraic_structure

Algebra - the GoF design pattern collection for functional programmers

Rather than solving this problem over and over again, let’s find a more general solution

Page 12: Algebraic data types: Semilattices

Semilattice

trait Semilattice[T] { ! def join(T a, T b): T }

Monoid

trait Monoid[T] { def id: T def op(T a, T b): T }

Idempotency Commutativity Associativity

Identity Associativity

Page 13: Algebraic data types: Semilattices

Idempotency

List(a) ++ List(a) ≠ List(a)

Set(a) ++ Set(a) = Set(a)

1 + 1 ≠ 1

max(1, 1) = 1

!

• Familiar binary operations forming monoids don’t need to be semilattices!

• Immutability isn’t enough / the same

• It doesn’t matter how many times you apply the operation

Page 14: Algebraic data types: Semilattices

Commutativity

• Order in which you apply operations doesn’t matter any more

• If we notice dropped packages, just send them again

1 + 2 = 2 + 1

max(1, 2) = max(2, 1)

List(a) ++ List(b) ≠ List(b) ++ List(a)

Set(a) ++ Set(b) = Set(b) ++ Set(a)

Page 15: Algebraic data types: Semilattices

Associativity (1)

• Allows you to split up and batch computations

• Each node needn’t receive all atomic operands, intermediate results will do as well

1 + (2 + 3) = (1 + 2) + 3

max(1, max(2, 3) = max(max(1, 2), 3)

List(a) ++ (List(b)++ List(c))

= (List(a) ++ List(b))

++ List(c)

Page 16: Algebraic data types: Semilattices

Associativity (2)

• Again, intermediate results are as good as atomic operands

• You never lose any information in the whole computation

red + blue = blue + red =

purple

red + (blue + blue) red + blue

≠ (red + blue) + blue

purple + blue

* Simplistic version that assumes we’re losing information about the volume of the colour, for example (if you’re mixing paint)

avg(1, avg(2, 4)) avg(1, 3)

≠ avg(avg(1, 2), 4)

avg(1.5, 4)

Page 17: Algebraic data types: Semilattices

G-Set

Page 18: Algebraic data types: Semilattices

2P-Set

Page 19: Algebraic data types: Semilattices

OP-Set

Page 20: Algebraic data types: Semilattices

Further reading (1)• “Jonas Bonér - The Road to Akka Cluster, and Beyond”:

https://skillsmatter.com/skillscasts/4543-the-road-to-akka-cluster-and-beyond

• “Noel Welsh - Reconciling eventually consistent data”: https://skillsmatter.com/skillscasts/4915-how-do-we-reconcile-eventually-consistent-data

• “Sean Cribbs - Eventually Consistent Data Structures”: https://vimeo.com/43903960

Page 21: Algebraic data types: Semilattices

Further reading (2)

• “A comprehensive study of Convergent and Commutative Replicated Data Types”: http://hal.upmc.fr/docs/00/55/55/88/PDF/techreport.pdf

Page 22: Algebraic data types: Semilattices

One more thing …