Prev Next

Database / Mnesia intermediate to advanced Interview questions

What is a sticky lock in Mnesia and why does it improve performance?

Acquiring a write lock on a replicated table normally requires coordinating with every node holding a copy, even if, in practice, all the writes to a particular record consistently originate from the same node. A sticky lock lets a node "claim" ownership of a table's locking for itself after its first access, so subsequent lock requests from that same node can be granted locally without a fresh network round-trip to every other replica each time.

sequenceDiagram
    participant NodeA
    participant NodeB
    NodeA->>NodeB: First write: acquire lock (full coordination)
    NodeB-->>NodeA: Lock granted, marked sticky to NodeA
    NodeA->>NodeA: Subsequent writes: lock granted locally, no round-trip

This is a significant win specifically for workloads where one node is the dominant writer for a given table over a stretch of time — it turns a network round-trip per lock acquisition into a purely local operation. Mnesia manages sticky locks automatically as an internal optimization; there's no special API call needed to opt in.

What does a sticky lock let a node skip on subsequent lock requests?
What workload pattern benefits most from sticky locks?

More Related questions...

What is the difference between mnesia:transaction/1 and mnesia:sync_transaction/1? What is the difference between async_dirty and sync_dirty activity contexts? How does mnesia:activity/4 let you customize the access context of an operation? What is table fragmentation in Mnesia and why would you use it? How do you configure a fragmented Mnesia table? Why does fragmentation help Mnesia scale beyond what one table replica set can handle? What is the difference between mnesia:match_object/1 and mnesia:select/2? What is mnesia:all_keys/1 used for? How do you integrate Mnesia with QLC for complex queries? Why would you use qlc:q with mnesia:table/1 instead of mnesia:select/2? What is a sticky lock in Mnesia and why does it improve performance? When can sticky locks cause a problem in a distributed Mnesia cluster? How do you dynamically add a new node to a running Mnesia cluster? What is mnesia:change_config(extra_db_nodes, Nodes) used for? How do you remove a table replica from a node without dropping the whole table? What is mnesia:del_table_copy/2 used for? How do you move a table copy from one node to another while the system stays live? What is mnesia:move_table_copy/3 used for? What is the {majority, true} table option and what tradeoff does it introduce? Why would you enable the majority option on a table prone to network partitions? What is a local_content table in Mnesia? When would you use a local_content table instead of a normal replicated table? What is mnesia:set_master_nodes/2 used for? How does setting master nodes influence conflict resolution after a network partition? What is mnesia_tm and what role does it play internally? How does Mnesia's transaction manager serialize concurrent transactions touching the same records? Why can a "hot" record or table become a bottleneck under heavy Mnesia write load? How can you redesign a schema to reduce lock contention on a frequently-updated record? What is mnesia:subscribe/1 used for? How do you react to table events using Mnesia's subscription mechanism? What is a nested Mnesia transaction and how does it behave differently from a top-level one? Why should nested transactions generally be avoided or used carefully in Mnesia? What is the dc_dump_limit configuration parameter used for? Why does tuning the transaction log dump frequency matter for write-heavy Mnesia workloads? How do you merge two previously-separate Mnesia clusters into one? What complications arise when merging schemas from two independently-created Mnesia databases? How does Mnesia decide which node's data wins during schema merge conflicts? What is the difference between a global lock and a per-record lock in Mnesia transactions? When would you use mnesia:read_lock_table/1 instead of relying on per-record locks? How do you profile and optimize Mnesia transaction throughput in a write-heavy system? Why is batching multiple related writes into a single transaction usually better than many small transactions? How does Mnesia's schema interact with OTP release upgrades (appup/relup)? What is the difference between mnesia:dirty_all_keys/1 and mnesia:all_keys/1? Explain the internal working of how Mnesia chooses which replica to read from in a load-balanced cluster? What is the where_to_read table_info item used for? What is mnesia:dump_log/0 used for? How do you handle a transaction that needs to retry after being aborted due to a deadlock? What is the difference between mnesia:transaction/1's built-in behavior and manual retry logic you might add?
Show more question and Answers...


Comments & Discussions