Qixxit Development

Qixxit is a digital travel planner that combines long-distance bus, train and flight options into one route and creates a custom itinerary: www.qixxit.com

split_brain.ex

iacobson
6 min readMar 26, 2019

--

The Context

Assumptions

Building the app

Persistency

The Entity Worker

The Worker Supervisor

The public functions

Time to play

iex(1)> MyApp.get_entity("entity_1")
{:ok, %MyApp.Entity{id: "entity_1", state: :initialized}}
iex(2)> MyApp.set_entity_state("entity_1", :working)
:ok
iex(4)> MyApp.set_entity_state("entity_2", :finalized)
:ok
iex(5)> MyApp.get_entity("entity_1")
{:ok, %MyApp.Entity{id: "entity_1", state: :working}}

Time to grow

Testing the app on two servers

server_1: MyApp.get_entity("entity_1")
{:ok, %MyApp.Entity{id: "entity_1", state: :initialized}}
server_2: MyApp.get_entity("entity_1")
{:ok, %MyApp.Entity{id: "entity_1", state: :initialized}}
server_1: MyApp.set_entity_state("entity_1", :working)
:ok
server_2: MyApp.set_entity_state("entity_1", :finalized)
:ok
server_2: MyApp.get_entity("entity_1")
{:ok, %MyApp.Entity{id: "entity_1", state: :finalized}}
server_1: MyApp.get_entity("entity_1")
{:ok, %MyApp.Entity{id: "entity_1", state: :working}}

What can we do?

Connecting the nodes

Elixir ships with facilities to connect nodes and exchange information between them. In fact, we use the same concepts of processes, message passing and receiving messages when working in a distributed environment because Elixir processes are location transparent.

iex --name foo@127.0.0.1 -S mix
iex(foo@127.0.0.1)1>
iex --name bar@127.0.0.1 -S mix
iex(bar@127.0.0.1)1>
iex(foo@127.0.0.1)1> Node.connect(:"bar@127.0.0.1")
true

Enter :global

iex(bar@127.0.0.1)2> MyApp.get_entity("entity_1")
{:ok, %MyApp.Entity{id: "entity_1", state: :initialized}}
iex(foo@127.0.0.1)4> MyApp.set_entity_state("entity_1", :working)
:ok
iex(bar@127.0.0.1)3> MyApp.get_entity("entity_1")
{:ok, %MyApp.Entity{id: "entity_1", state: :working}}

Not quite there!

global: Name conflict terminating {:entity_1, #PID<15181.143.0>}
use Mix.Configconfig :my_app, nodes: [:"foo@127.0.0.1", :"bar@127.0.0.1"]

Conclusion

--

--

Qixxit Development
Qixxit Development

Published in Qixxit Development

Qixxit is a digital travel planner that combines long-distance bus, train and flight options into one route and creates a custom itinerary: www.qixxit.com

No responses yet