Database / Mnesia intermediate to advanced Interview questions
What is the difference between a global lock and a per-record lock in Mnesia transactions?
A per-record lock (the default behavior of read/1/write/1 inside a transaction)
only blocks other transactions from touching that specific record, letting unrelated concurrent transactions
on other records in the same table proceed freely. A global (table-level) lock, taken via
mnesia:read_lock_table/1 or mnesia:write_lock_table/1, blocks access to the
entire table for the duration of the transaction.
| Per-record lock | Table-level lock |
| Only affects the specific record being read/written. | Affects every record in the table. |
| Maximizes concurrency for unrelated operations. | Serializes all access to the table, sacrificing concurrency. |
| Default behavior for normal read/write. | Requires explicitly calling a table-lock function. |
Table-level locks trade away most of the concurrency benefit of fine-grained locking, so they're reserved for operations that genuinely need a consistent view of, or exclusive access to, the whole table at once.
More Related questions...