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

CRDTs - The science behind Phoenix Presence

CRDTs - The science behind Phoenix Presence

Maciej Kaszubowski

May 25, 2017
Tweet

More Decks by Maciej Kaszubowski

Other Decks in Programming

Transcript

  1. Node A Node B [User] [User] [User] [] [] [User]

    User connects User disconnects
  2. Node A Node B [User] [User] [User] [] [] [User]

    User connects User disconnects
  3. Node A Node B [User] [User] [User] [] [] [User]

    User connects User disconnects
  4. P.track(self, "users", "U1", %{}) P.list("users") P.track(self, "users", "U2", %{}) P.list("users")

    %{"U1"  %{metas: [%{phx_ref: "…"}]}, "U2"  %{metas: [%{phx_ref: "…"}]}} %{"U1"  %{metas: [%{phx_ref: "…"}]}}
  5. P.track(self, "users", "U1", %{}) P.list("users") P.track(self, "users", "U2", %{}) P.list("users")

    %{"U1"  %{metas: [%{phx_ref: "…"}]}, "U2"  %{metas: [%{phx_ref: "…"}]}} P.track(self, "users", "U1", %{}) P.untrack(self, "users", "U1") %{"U1"  %{metas: [%{phx_ref: "…"}]}}
  6. P.track(self, "users", "U1", %{}) P.list("users") P.track(self, "users", "U2", %{}) P.list("users")

    %{"U1"  %{metas: [%{phx_ref: "…"}]}, "U2"  %{metas: [%{phx_ref: "…"}]}} P.track(self, "users", "U1", %{}) P.untrack(self, "users", "U1") P.list("users") %{"U1"  %{metas: [%{phx_ref: "…"}]}, "U2"  %{metas: [%{phx_ref: "…"}]}} P.list("users") %{"U1"  %{metas: [%{phx_ref: "…"}]}, "U2"  %{metas: [%{phx_ref: "…"}]}} %{"U1"  %{metas: [%{phx_ref: "…"}]}}
  7. Node A Node B +5 5 8 -2 3 +5

    User connects User disconnects 0 0 5 3 8
  8. Node A Node B +5 5 8 -2 8 +5

    User connects User disconnects 0 0 5 3 13 10
  9. Node 2: P=2 N=2 Value=8 Node 1: P=5 N=3 Node

    3: P=6 N=0 2 0 6 +2 +1 Merge
  10. Node A Node B [User] [User] [User] [] [] [User]

    User connects User disconnects
  11. Node A Node B [ ] [1] [1] Add Remove

    [1] [1] [1] [ ] [1] [1] [1] [ ] [1]
  12. Node A Node B [ ] [ ] Add Remove

    [{A,1}] [{A,1}] [ ] [ ] [{A,1}] [{A,1}, ]
  13. Node A Node B [ ] [ 1 ] Add

    Remove [{A,1}] [{A,1}] [ ] [ ] [{A,1}] [{A,1},{A,2}]
  14. Node A Node B [ ] [ 1 ] Add

    Remove [{A,1}] [{A,1}] [ ] [ ] [{A,1}] [{A,1},{A,2}]
  15. Node A Node B [ ] [ 1 ] Add

    Remove [{A,1}] [{A,1}] [ ] [ ] [{A,1}] [{A,1},{A,2}] [ 1 ] [{A,1},{A,2}] [{A,1},{A,2}] [ 1 ]
  16. Node A Node B [ ] [ 1 ] Add

    Remove [{A,1}] [{A,1}] [ ] [ ] [{A,1}] [{A,1},{A,2}] [ 1 ] [{A,1},{A,2}] [{A,1},{A,2}] [ 1 ] [A] [A]
  17. • Load balancing / routing • Mobile clients synchronisation •

    Temporary data on the servers • Avoiding work duplication • Collaborative editing