class: center, middle # Building distributed applications: riak_core vs partisan Mariano Guerra [@warianoguerra](https://twitter.com/warianoguerra) [instadeq.com](https://instadeq.com) [@instadeq](https://twitter.com/instadeq) Erlang Meetup London 2018 --- # Questions * Who are we? * How do we share knowledge? * How do I communicate with someone? * Who is responsible for this? --- ``` Riak Core Partisan +--------------+ +------------+ +-----------------------------+ | | | | | | | RC Metadata | | RC Ring | | Bring Your Own | | | | | | | +--------------+ +------------+ +-----------------------------+ +--------------+ +------------+ +-----------------------------+ | | | | | | | Plumtree | | RC Gossip | | Plumtree | | | | | | | +--------------+ +------------+ +-----------------------------+ +-----------------------------+ +-----------------------------+ | | | | | Distributed Erlang | | Dist Erl/Static/HyParView | | | | | +-----------------------------+ +-----------------------------+ ``` --- ``` +--------------+ +------------+ | | | | | Metadata | | Routing | | | | | +--------------+ +------------+ +-----------------------------+ | | | Gossip | // Provides broadcast | | // communication +-----------------------------+ +-----------------------------+ | | | Membership | // Provides point to point | | // communication +-----------------------------+ ``` --- ``` Riak Core Partisan +--------------+ +------------+ +------------+ +--------------+ | | | | | | | | | RC Metadata | | RC Ring | | Metadata | | RC Ring | | | | | | | | | +--------------+ +------------+ +------------+ +--------------+ +-----------------------------+ +-----------------------------+ | | | | | Plumtree | | Plumtree | | | | | +-----------------------------+ +-----------------------------+ +-----------------------------+ +-----------------------------+ | | | | | Distributed Erlang | | DistErl/HyParView | | | | | +-----------------------------+ +-----------------------------+ ``` --- # Who are we? (Membership) ``` +-----------------------------+ | | | Membership | | | +-----------------------------+ ``` --- # Membership: Riak Core Distributed Erlang * Nodes loosely connected * Connect on first usage + spawn(Node,M,F,A) + net_adm:ping(Node) * Transitive connections + can be disabled with `-connect_all false` --- # Membership: Riak Core (cont.) * Disconnection removes a node + Node down + erlang:disconnect_node(Node) * Nodes connect all to all (mesh) --- # Membership: Riak Core (cont.) * Erlang Port Mapper (EPMD) started on each node + Maps symbolic node names to host/port * List visible nodes() + nodes(hidden) `-hidden` flag + nodes(connected) --- # Membership: Partisan Configurable * Default (Distributed Erlang) * HyParView * Static * Client/Server --- class: center, middle  --- # Membership: Partisan (HyParView) * Active View: size = log(n) + c * Passive View: k(log(n) + c) * Assumes reliable transport protocol like TCP For c = 1 and k = 6 * Network of 10.000 nodes * Active views size is 5 * Passive views size is 30 --- # Membership: Partisan (HyParView) * Active connection to every node on active view * On msg receive, forwards to nodes in active view * Tests connection on each broadcast * Removes from active view if connection fails * Promotes node from passive view * Periodic shuffle with random nodes + Shares some nodes from active view, all from passive view --- # How do I communicate with someone? --- # Riak Core ! or OTP behaviors --- # Partisan ```erlang partisan_peer_service:forward_message(NodeName, RemotePidOrRegisteredName, Message).``` --- # Who is responsible for this? ``` +--------------+ +------------+ | | | | | Metadata | | Routing | | | | | +--------------+ +------------+ ``` --- class: center, middle # Riak Core "Dynamo"  --- class: center, middle # Riak Core "Dynamo"  --- # Riak Core ```erlang DocIdx = riak_core_util:chash_key(Key), PrefList = riak_core_apl:get_primary_apl(DocIdx, 1, sdb), [{IndexNode, _Type}] = PrefList, riak_core_vnode_master:sync_spawn_command(IndexNode, Cmd, sdb_vnode_master). ``` --- # How do we share information? ``` +-----------------------------+ | | | Gossip | | | +-----------------------------+ ``` --- * Riak Core + Plumtree (Riak Core Metadata) * Partisan: + Plumtree --- # Riak Core Metadata * Namespaced Key-Value Store * Replicated to all nodes + Eventually + Asynchronously * In memory (ETS) * On disk (DETS) * Local Read/Writes --- class: center, middle  --- # Plumtree Node State * Eager Set * Lazy Set * Seen Msgs --- class: center # Eager Push  --- class: center # Eager Push  --- class: center # Eager Push  --- class: center # Eager Push  --- class: center # Eager Push  --- class: center # Eager Push  --- class: center # Eager Push  --- class: center # Eager Push  --- class: center # Lazy Push  --- class: center # Lazy Push  --- class: center # Lazy Push  --- class: center # Lazy Push  --- class: center # Lazy Push  --- class: center # Lazy Push  --- class: center # Lazy Push  --- class: center # Lazy Push  --- # Active Anti Entropy (AAE) How does node 3 gets the values broadcasted while it was down? --- # Merkle Tree Tree in which every non-leaf node is labelled with the hash of the labels or values (in case of leaves) of its child nodes --- # Merkle Tree * Git * IPFS * ZFS, Btrfs * BitTorrent * Bitcoin * Cassandra, Riak --- class: center # Hash Tree  --- class: center # Segments  --- class: center # Segment Hashes  --- class: center # Upper Hashes  --- # Hash Tree Operations * Insert * Update * Compare --- class: center # Insert  --- class: center # Insert  --- class: center # Insert  --- class: center # Insert  --- class: center # Insert  --- class: center # Update  --- class: center # Update  --- class: center # Update  --- class: center # Update  --- class: center # Update  --- class: center # Update  --- class: center # Compare  --- class: center # Compare  --- class: center # Compare  --- class: center # Compare  --- class: center # Compare  --- class: center # Demo Time --- # Thanks [github.com/marianoguerra-atik/2018-london-erlang-meetup](https://github.com/marianoguerra-atik/2018-london-erlang-meetup) [github.com/marianoguerra-atik/riak_core_ring_on_partisans_plumtree](https://github.com/marianoguerra-atik/riak_core_ring_on_partisans_plumtree) [marianoguerra.github.io/presentations/2018-london-erlang-meetup](https://marianoguerra.github.io/presentations/2018-london-erlang-meetup) Mariano Guerra [@warianoguerra](https://twitter.com/warianoguerra) [instadeq.com](https://instadeq.com) [@instadeq](https://twitter.com/instadeq)