Database / Mnesia intermediate to advanced Interview questions
What is the difference between async_dirty and sync_dirty activity contexts?
Both are non-transactional access contexts you can use with mnesia:activity/2 or by directly
calling dirty functions, trading Mnesia's usual locking/atomicity for speed — the difference between
them is, again, about replication timing rather than locking.
| async_dirty | sync_dirty |
| Returns as soon as the local write succeeds; replicates to other nodes in the background. | Waits for the write to be applied on every replica before returning. |
| Fastest option; least consistency guarantee. | Slightly slower; stronger guarantee that replicas are caught up when the call returns. |
mnesia:activity(sync_dirty, fun() -> mnesia:write(#metric{id = 1, value = 42}) end).
Neither context provides transactional atomicity across multiple operations or locking against concurrent
writers — both remain "dirty" in that sense; sync_dirty just narrows the window where a
reader on another node could see stale data immediately after a write.
More Related questions...