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?

1 Like

I have tried the example from here:
https://learnyousomeerlang.com/mnesia

I have used 2 nodes from different, but connected systems. I have used mafiapp:install(Nodes).

(mafia@192.168.0.2)9> mnesia:info().
---> Processes holding locks <--- 
---> Processes waiting for locks <--- 
---> Participant transactions <--- 
---> Coordinator transactions <---
---> Uncertain transactions <--- 
---> Active tables <--- 
mafiapp_enemies: with 0        records occupying 312      words of mem
schema         : with 4        records occupying 808      words of mem
mafiapp_services: with 0        records occupying 312      words of mem
mafiapp_friends: with 4        records occupying 384      words of mem
===> System info in version "4.15.5", debug level = none <===
opt_disc. Directory "/home/phoenix/mafia/Mnesia.mafia@192.168.0.2" is used.
use fallback at restart = false
running db nodes   = ['mafia@192.168.0.60','mafia@192.168.0.2']
stopped db nodes   = [] 
master node tables = []
remote             = []
ram_copies         = []
disc_copies        = [mafiapp_enemies,mafiapp_friends,mafiapp_services,schema]
disc_only_copies   = []
[{'mafia@192.168.0.2',disc_copies}] = [mafiapp_enemies]
[{'mafia@192.168.0.2',disc_copies},{'mafia@192.168.0.60',disc_copies}] = [mafiapp_friends,
                                                                          mafiapp_services,
                                                                          schema]
14 transactions committed, 0 aborted, 0 restarted, 5 logged to disc
0 held locks, 0 in queue; 0 local transactions, 0 remote
0 transactions waits for other nodes: []
ok

You can see, that all tables have disk copies on both nodes. Changes are distributed after the cluster is restored.

I hope this asnwered some of your questions.

1 Like

You can use mnesia:add_table_copy to add a table to a remote node. As far as how RAM / Disk is managed. You state explicitly where you want it to be saved when you create the table. If you want to change it later you can use the function you already mentioned: mnesia:change_table_copy_type.

Where in memory a table is saved is a separate issue from what nodes the tables are saved on. Pretty neat to have all that management and redundancy done for you and still be able to customize it to that degree.

1 Like

Hi guys, thanks for the help!
My problem was that I tried to control my backup node thru my master node, so I had to do spawn(BNode, mnesia, ... ) to create those disc_copies.

I still don’t understand how and when the disc_copies are synchronized tho. What’s most important is that the backup node will be able to reopen the fsm according to the most up to date data. If the data is not synchronized immediately between Mnesia nodes and the master nodes closes, then the data in RAM is lost, no?

1 Like

Mnesia does a two-phase commit so changes are propagated to all nodes at the same time.

So it either all succeeds or all fails.

Check out 4.1 Atomicity

https://erlang.org/documentation/doc-5.2/lib/mnesia-4.1/doc/html/Mnesia_chap4.html

1 Like

Interesting, I didn’t know that’s how Mnesia worked.
Thanks, this will help me plan my app better :slight_smile:

1 Like