Search code examples
erlangdistributedmnesia

How does mnesia sync disc_copies with other nodes / PCs?


I want to back up my app using Mnesia so if the main node fails it could be restarted on another PC.
So far I succeeded in connecting the main node to other nodes and their Mnesia, but I can’t make the remote nodes save the tables as disc_copies (except for the schema). Even after mnesia:change_table_copy_type, [data, Node, disc_copies] on all tables and nodes they are still only saved “remotely” on the main node.

How does Mnesia sync and save to those remote tables? What frequency? If the backup nodes don’t have enough RAM will they write data to disc? If the main node falls, will the data be salvageable?
According to this answer Mnesia dumps to disc only when RAM gets too full, this means that a sudden shutdown of the main PC will lose data not yet written to disc, which is not good at all...


Solution

  • After asking on the Erlang forums and reading more about the documentation I have a few answers:

    How does Mnesia sync and save to those remote tables?

    Mnesia tables and connections works according to "Atomicity", meaning all writes either succeed on ALL tables (i.e. nodes in cluster) or fail. This ensures that tables are synced as much as possible. This also raises another issue of making sure nodes are really up and configured correctly when trying to connect them tho.

    If the backup nodes don’t have enough RAM will they write data to disc?

    Yes, Mnesia makes sure of this.

    If the main node falls, will the data be salvageable?

    Yes, because of this Atomicity, the data will be on all nodes, so even if the main node fails and has data in RAM, this data is also on the other remote tables :)

    EDIT: Just make sure to use mnesia:sync_transaction() instead of a normal transaction!!