[erlang-questions] Mnesia Transactions

Rudolph van Graan rvg@REDACTED
Mon Nov 22 18:47:05 CET 2010


Hi,


> Let us say I have a distributed table that is active on several nodes, and wish to apply a transaction to insert multiple records in that table. Is each operation inside that transaction applied individually on each node, or is the whole transaction applied on each node in turn?


The answer to your question is that the entire transaction is applied in its entirety to all nodes at the same time and it is ACID compliant.

> 1) Take the record in turn and insert it into node1, node2...node X , take the second record and insert into node1, node2...node X until complete.
> 
> Or
> 
> 2) Apply the whole transaction to node1 (insert all 1000 records locally) and then apply the fun (again locally) to node 2 and so on?


Neither. 

The fact that the 1000 writes are all encapsulated in one mnesia:transaction(...) means the following:

1. Your process starts a transaction, because it called mnesia:transaction(...) - this is written to the LOG(*) on all nodes 
2. Your process will acquire 1000 write locks and write 1000 records on the entire database (i.e. all the nodes where the table that you write to reside) - this implies network communication between your process's node and the lock managers on all the nodes holding a copy of the same table. A write lock will be required before you can write the record. 
3. If your process reaches the commit decision point (i.e. it was granted all 1000 write logs and when all nodes received the entries in their LOG), this decision is recorded in the LOG (on all nodes) and each node makes the changes to the local data. If a node fails at this stage, the other nodes will continue to apply the LOG. Once the commit operation is done, this is recorded in the LOG. The failed node will recover from this when restarting.

* with LOG I mean the write-ahead log (WAL) 

Rudolph van Graan


On Nov 22, 2010, at 3:47 PM, Evans, Matthew wrote:

> Hi Team,
> 
> I have a quick question WRT mnesia and transactions, and can't seem to find the answer anywhere.
> 
> Let us say I have a distributed table that is active on several nodes, and wish to apply a transaction to insert multiple records in that table. Is each operation inside that transaction applied individually on each node, or is the whole transaction applied on each node in turn?
> 
> For example
> 
> ListOfOneThousandRecords = create_records(),
> mnesia:transaction(fun() ->
>            [mnesia:write(Rec)||Rec<-ListOfOneThousandRecords]
> end).
> 
> So this transaction will insert 1000 records into the table.
> 
> Will mnesia:
> 
> 1) Take the record in turn and insert it into node1, node2...node X , take the second record and insert into node1, node2...node X until complete.
> 
> Or
> 
> 2) Apply the whole transaction to node1 (insert all 1000 records locally) and then apply the fun (again locally) to node 2 and so on?
> 
> Thanks
> 
> Matt

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3822 bytes
Desc: not available
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20101122/a0944eb2/attachment.bin>


More information about the erlang-questions mailing list