Slide 1

Slide 1 text

KEEP YOUR DATA IN SYNC IN YOUR ERLANG APPLICATION CodeBeam Munich - 2018

Slide 2

Slide 2 text

benoît chesneau craftsman working on P2P and custom data endpoints solution Created 11 years ago, Enki Multimedia member of the Erlang Industrial User Group about me

Slide 3

Slide 3 text

more and more of our data is in the "Cloud" we let to third parties the control over our data we live in the time where Making us more and more dependant

Slide 4

Slide 4 text

Barrel is not a database. It is a framework / product that synchronizes 
 and replicates data between machines and applications.

Slide 5

Slide 5 text

keep a partial state of your data locally replicate changes in your data route automatically or implicitly a view of your data Built in Erlang Opensource Features

Slide 6

Slide 6 text

Barrel, core concepts

Slide 7

Slide 7 text

core data structure of barrel are documents stored in a barrel can be represented as a an Erlang map or JSON with a revision Document oriented { "id" : "ID", "key" : "value", "_rev": "REVISIONID" }

Slide 8

Slide 8 text

attachments are binary contents updated or linked (http) to a document attach anything { "id" : "ID", "key" : "value", "_attachments": [ { "id" : "AttachmentID" } ] }

Slide 9

Slide 9 text

by document IDs by path automatically indexed { "id" : "ID", "locations" : [ { "country" : "France", "city" : "creil" } ], "headquarter" : "France" } $/id/ID $/locations/0/country/france $/locations/0/city/creil $/headquarter/france

Slide 10

Slide 10 text

listen on any document changes changes are incremental filter changes persist view changes database changes { "id" : "ID", "rev" : "REVID", "seq" : 1, "changes" : ["REVID"] }

Slide 11

Slide 11 text

uses the changes feed handle the conflicts push/pull replication push/pull replication

Slide 12

Slide 12 text

built in Erlang good resilience due to an efficient supervision. (aten from rabbitmq for local nodes monitoring) Each steps of the write workflow is done in different isolated processes local document db using Rocksdb using dirty-nifs:
 https://gitlab.com/barrel-db/erlang-rocksdb new improvements in the TCP/IO stack improves the performance for free.

Slide 13

Slide 13 text

create a custom data platform mixing local and remote storages and different peer-to-peer replications strategies.

Slide 14

Slide 14 text

HTTP(s) & Websocket for any one lib can be embedded in an Erlang Node if you want to remote nodes uses websockets to exchange the data embedded or remote

Slide 15

Slide 15 text

barrels have to be created per nodes Or via the HTTP API create a barrel Name = <<"mybarrel">>, Params = #{ store_provider => <<"MyLocalRocksdb">> }, ok = create_barrel(Name, Params) POST /barrels/name { "name": "mybarrel", "params" : { "store_provider": "MyLocalRocksdb" } }

Slide 16

Slide 16 text

once opened , you can use a barrel to create and retrieve a doc simple put and get a barrel {ok, Barrel} = barrel:open(Name) {ok, DocId, Rev} = barrel:save_doc(Barrel, {}), {ok, Doc = #{ <<"id">> := DocId, <<"_rev">> := Rev }} = barrel:fetch_doc(Barrel, DocId)

Slide 17

Slide 17 text

once opened , you can use a barrel to create and retrieve a doc can be nested "Peer to Peer" Put Params = #{ p2p_write => { host => "HOST:PORT", name => <<"RemoteBarrel">> }} {ok, DocId, Rev} = barrel:save_doc(Barrel, {}, Params) Params = #{p2p_write => { host => "HOST:PORT", name => <<"RemoteBarrel">>, p2p_write => { ... } }}

Slide 18

Slide 18 text

similar syntax to Put Read first locally then remote "Peer to Peer" Get Params = #{p2p_read => { host => "HOST:PORT", name => <<"RemoteBarrel">>} {ok, Doc} = barrel:fetch_doc(Barrel, {}, Params),

Slide 19

Slide 19 text

once opened , you can use a barrel to create and retrieve a doc
 
 Source and Target can be remote or local. Or both. Can work in Push and Pull mode at the same time (synchronisation). REPLICATION Params = #{ <<"source">> => <<"MyLocalBarrel">>, <<"target" => << "ws://HOST:PORT/RemoteBarrel" >> } {ok, RepId} = barrel:replicate(Params),

Slide 20

Slide 20 text

A listener is a simple process on which you can pass a callback. you can subscribe to different pattern of path with a semantic similar to the one in MQTT Listen on change Callback = fun(Change) -> ok end, Params => #{ callback_fun => MyCallback, subscribe_to => [<<"/*">>], since => 0 }, {ok, ListenerPid} = start_listener(Barrel, Params).

Slide 21

Slide 21 text

what's next?

Slide 22

Slide 22 text

[email protected] https://barrel-db.org