Database / Mnesia intermediate to advanced Interview questions
How does Mnesia's transaction manager serialize concurrent transactions touching the same records?
Mnesia uses pessimistic locking rather than optimistic concurrency control: when a transaction reads or
writes a record, mnesia_tm acquires the appropriate lock (read or write) on that record before
proceeding, and a conflicting lock request from another concurrent transaction simply has to wait until the
first transaction releases it at commit or abort.
sequenceDiagram
participant T1
participant T2
participant TM as mnesia_tm
T1->>TM: request write lock on Key
TM-->>T1: granted
T2->>TM: request write lock on Key
Note over TM: T2 blocks, waiting
T1->>TM: commit, release lock
TM-->>T2: lock granted, T2 proceeds
This guarantees serializability for conflicting operations without needing to detect and retry after the
fact the way optimistic approaches do — the cost is that a transaction can block waiting on a lock held
by another, which is exactly the scenario that can escalate into a deadlock if two transactions each hold a
lock the other is waiting for, something mnesia_tm detects and resolves by aborting one of them.
More Related questions...