Prev Next

Database / Mnesia basics Interview questions

1. What is Mnesia? 2. What is a Mnesia table? 3. What are the storage types available for a Mnesia table? 4. What is a Mnesia schema? 5. How do you create a Mnesia schema? 6. How do you create a Mnesia table? 7. What is a Mnesia transaction? 8. How do you write a record to a Mnesia table? 9. How do you read a record from a Mnesia table? 10. What are dirty operations in Mnesia? 11. What is mnesia:dirty_read/1? 12. What is a Mnesia record? 13. How do you delete a record from a Mnesia table? 14. What is the purpose of mnesia:start/0? 15. How do you stop Mnesia? 16. What are the Mnesia table types (set, ordered_set, bag)? 17. What is a Mnesia index? 18. How do you add a secondary index to a Mnesia table? 19. What is mnesia:info/0 used for? 20. How do you check if Mnesia is running? 21. What is the Mnesia directory (Mnesia.dir)? 22. What is the primary key in a Mnesia table? 23. What is mnesia:select/2 used for? 24. What is record_info used for when creating a Mnesia table? 25. What are the attributes of a Mnesia table? 26. What is a Mnesia node? 27. How do you replicate a table across multiple Mnesia nodes? 28. What is mnesia:table_info/2 used for? 29. Describe the difference between a Mnesia table and an ETS table? 30. Why should Mnesia operations generally run inside a transaction? 31. When should you use dirty operations instead of a transaction? 32. What is the difference between ram_copies and disc_copies? 33. What is disc_only_copies and when would you use it? 34. What happens to a disc_copies table when its node restarts? 35. How do you change a table's storage type at runtime? 36. What is a Mnesia table lock and how does it work? 37. How does Mnesia keep replicated tables consistent across nodes? 38. How do you migrate a Mnesia record definition when its fields change? 39. What is mnesia:transform_table/3 used for? 40. When should you choose bag over set for a Mnesia table? 41. How do you back up a Mnesia database? 42. How do you restore a Mnesia database from a backup? 43. Explain the lifecycle of a Mnesia transaction from start to commit? 44. How do you troubleshoot a Mnesia table stuck in the "loading" state? 45. What is mnesia:force_load_table/1 and when should you use it? 46. What is the difference between mnesia:read/1 and mnesia:wread/1? 47. What is a Mnesia checkpoint used for? 48. How does Mnesia resolve conflicts after a network partition heals?

1. What is Mnesia?

Mnesia is Erlang/OTP's built-in distributed database management system , designed to run inside the same BEAM nodes as your application rather than as a separate external service. It stores ordinary Erlang terms directly — tuples and records — so there's no object-relational mapping l...

Read full answer

2. What is a Mnesia table?

A Mnesia table is a named collection of records, conceptually similar to a database table but storing ordinary Erlang tuples rather than rows in a relational sense. Each table is backed by an ETS (or disc-based DETS) table under the hood, with Mnesia layering transactions, replication, and schema...

Read full answer

3. What are the storage types available for a Mnesia table?

Mnesia supports three storage types per table, letting you trade off speed against durability on a per-table basis: ram_copies disc_copies disc_only_copies In memory only; fastest, lost on node restart. In memory AND on disk; fast reads, durable across restarts. On disk only; slower, used when th...

Read full answer

4. What is a Mnesia schema?

The schema is Mnesia's own metadata table — it tracks which tables exist, their storage types, which nodes hold a copy of each, and other structural information about the database itself, separate from the actual row data inside your tables. mnesia:create_schema([node()]). mnesia:start(). m...

Read full answer

5. How do you create a Mnesia schema?

Creating a schema is a one-time setup step, typically run from an Erlang shell before your application starts Mnesia for the first time, specifying which nodes will participate in the database. 1> mnesia:create_schema([node()]). ok 2> mnesia:start(). ok mnesia:create_schema/1 takes a list of node...

Read full answer

6. How do you create a Mnesia table?

mnesia:create_table/2 takes a table name and a list of options describing its attributes, storage type, and table type, registering it in the schema and allocating the underlying storage. - record(perso n , { id , na me , age } ). m nes ia : crea te _ ta ble(perso n , [{ a ttr ibu tes , record_i ...

Read full answer

7. What is a Mnesia transaction?

A Mnesia transaction wraps a set of reads and writes so they either all succeed together or none of them take effect — the same all-or-nothing guarantee a traditional database transaction provides, implemented here across Erlang processes and, if the table is replicated, across nodes. mnesi...

Read full answer

8. How do you write a record to a Mnesia table?

mnesia:write/1 inserts or updates a record inside a transaction — if a record with the same primary key already exists, it's overwritten; otherwise a new one is added. -record(person, {id, name, age}). mnesia:transaction(fun() -> mnesia:write( #person{id = 1, name = "Ada", age = 34}) end). ...

Read full answer

9. How do you read a record from a Mnesia table?

mnesia:read/1 (or the 2-arity form naming the table explicitly) fetches all records matching a given primary key, returning a list — empty if nothing matches, one element for a set table, or possibly several for a bag table where duplicate keys are allowed. mnesia:transaction(fun() -> mnesi...

Read full answer

10. What are dirty operations in Mnesia?

Dirty operations ( mnesia:dirty_read/1 , mnesia:dirty_write/1 , mnesia:dirty_delete/1 , and similar) perform a single read or write directly against the table, bypassing transaction locking and the coordination needed to keep replicated copies perfectly in sync during the operation. mnesia:dirty_...

Read full answer

11. What is mnesia:dirty_read/1?

mnesia:dirty_read({Table, Key}) fetches matching records directly, without opening a transaction or taking any lock — it's the fastest way to read a single record when you don't need the consistency guarantees a transaction provides. mnesia:dirty_read({person, 1}). %% -> [ #person{id = 1, n...

Read full answer

12. What is a Mnesia record?

A Mnesia record is simply an ordinary Erlang -record , whose field names match a table's declared attributes — Mnesia doesn't introduce a separate record concept of its own, it reuses the language's existing record syntax as the shape of each row. -record(person, {id, name, age}). Row = #pe...

Read full answer

13. How do you delete a record from a Mnesia table?

mnesia:delete/1 removes the record matching a given key, run inside a transaction just like write/1 and read/1 , so the deletion participates in the same atomicity and locking guarantees. mnesia:transaction(fun() -> mnesia:delete({person, 1}) end). There's also mnesia:delete_object/1 , which dele...

Read full answer

14. What is the purpose of mnesia:start/0?

mnesia:start/0 boots the Mnesia application on the current node: it loads the schema created earlier by mnesia:create_schema/1 , opens any disk-based tables, and begins participating in replication with any other already-running nodes that share tables with this one. mnesia:start(). %% ok It's as...

Read full answer

15. How do you stop Mnesia?

mnesia:stop/0 shuts down the Mnesia application on the current node cleanly, flushing any pending disk writes and detaching from replication with other nodes, without affecting Mnesia's state on other nodes in the cluster. mnesia:stop(). %% stopped This is a graceful local shutdown, distinct from...

Read full answer

16. What are the Mnesia table types (set, ordered_set, bag)?

The type option on mnesia:create_table/2 controls how a table handles keys and ordering, mirroring the same distinction ETS makes. set ordered_set bag Unique keys; one record per key. Default type. Unique keys, kept in a sorted order for efficient range-style traversal. Keys may repeat; multiple ...

Read full answer

17. What is a Mnesia index?

By default, only a table's primary key can be looked up efficiently; searching by any other field means scanning every record. A Mnesia index adds a secondary lookup structure on a non-key attribute so queries on that field can be answered directly, without a full table scan. mnesia:add_table_ind...

Read full answer

18. How do you add a secondary index to a Mnesia table?

mnesia:add_table_index/2 adds an index on a given attribute of an already-existing table, which can be done at runtime, even while the system is live and serving traffic. mnesia:add_table_index(person, age). Once added, queries using mnesia:index_read(Table, Value, Attribute) can look up records ...

Read full answer

19. What is mnesia:info/0 used for?

mnesia:info/0 prints a human-readable summary of the running Mnesia system directly to the shell: which tables exist, their sizes, storage types, which nodes hold copies, and general system status — a quick way to sanity-check the state of the database without writing any query code. 1> mne...

Read full answer

20. How do you check if Mnesia is running?

The most direct check is mnesia:system_info(is_running) , which returns an atom describing the current state rather than a plain boolean, since Mnesia can be in intermediate states like starting up or stopping rather than strictly on or off. mnesia:system_info(is_running). %% -> yes | no | starti...

Read full answer

21. What is the Mnesia directory (Mnesia.dir)?

The Mnesia directory is the filesystem location where a node stores all of its disk-based Mnesia data — the schema file, disc_copies table files, transaction logs, and backups. Its default name follows the pattern Mnesia.NodeName , but it's fully configurable. %% in sys.config or via comman...

Read full answer

22. What is the primary key in a Mnesia table?

The primary key of a Mnesia table is always its first declared attribute — there's no separate syntax to mark a different field as the key the way some databases let you designate an arbitrary column. Every lookup by mnesia:read/1 or mnesia:dirty_read/1 is keyed on this first field. -record...

Read full answer

23. What is mnesia:select/2 used for?

mnesia:select/2 queries a table using a match specification, letting you filter on conditions beyond a simple primary-key lookup — the same match-spec mechanism ETS uses, run inside a Mnesia transaction for consistency. m nes ia : transa c t io n ( fun () - > m nes ia : selec t (perso n , [...

Read full answer

24. What is record_info used for when creating a Mnesia table?

record_info(fields, RecordName) is a compiler built-in that expands, at compile time, to the literal list of field names declared in a -record definition — it's the standard way to feed a table's attributes option without retyping the field list by hand. - record(perso n , { id , na me , ag...

Read full answer

25. What are the attributes of a Mnesia table?

A table's attributes are simply the ordered field names describing the shape of each record it stores — equivalent to column names in a relational table, though here they correspond directly to an Erlang record's fields. -record(person, {id, name, age}). %% attributes: id, name, age The att...

Read full answer

26. What is a Mnesia node?

In Mnesia's context, a "node" is simply an Erlang node (a running BEAM instance with a name) that participates in a Mnesia database, either holding copies of tables directly or just being schema-aware of the cluster. A single logical Mnesia database can span several such nodes, all coordinating t...

Read full answer

27. How do you replicate a table across multiple Mnesia nodes?

Replication is set up simply by listing more than one node in a table's storage type option when creating (or later reconfiguring) the table — Mnesia then keeps all listed nodes' copies in sync automatically as part of every transaction. m nes ia : crea te _ ta ble(sessio n , [{ disc_copies...

Read full answer

28. What is mnesia:table_info/2 used for?

mnesia:table_info(Table, Item) returns a specific, structured piece of metadata about a table — its size, storage type, attribute list, index list, and more — suitable for use in application code, unlike the human-readable printout from mnesia:info/0 . mnesia:table_info(person, size)....

Read full answer

29. Describe the difference between a Mnesia table and an ETS table?

Mnesia tables are actually built on top of ETS (or DETS for disk-only storage), so they share the same underlying storage engine — the difference is entirely in what Mnesia layers on top. ETS table Mnesia table Node-local only; no built-in replication. Can be replicated across multiple node...

Read full answer

30. Why should Mnesia operations generally run inside a transaction?

Wrapping reads and writes in mnesia:transaction/1 gives you the same guarantee a relational database transaction gives you: either every operation in the block succeeds, or none of them take effect, even if the table is replicated across several nodes. Without that, a partial failure mid-sequence...

Read full answer

31. When should you use dirty operations instead of a transaction?

Dirty operations fit specifically when an operation genuinely stands alone — a single read or write that doesn't depend on any other operation succeeding alongside it, and where occasional inconsistency during a race or a node failure mid-write is acceptable for that particular piece of dat...

Read full answer

32. What is the difference between ram_copies and disc_copies?

Both storage types keep an active, fast in-memory copy of the table data for reads and writes during normal operation — the difference is entirely about what happens to that data when the node restarts. ram_copies disc_copies In memory only. In memory AND mirrored to disk. Data is lost if t...

Read full answer

33. What is disc_only_copies and when would you use it?

disc_only_copies keeps a table's data purely on disk (backed by DETS), with no full in-memory copy the way ram_copies and disc_copies maintain — every read and write goes through disk I/O rather than being served from RAM. m nes ia : crea te _ ta ble(archive_log , [{ disc_o nl y_copies , [ ...

Read full answer

34. What happens to a disc_copies table when its node restarts?

On restart, Mnesia reads the table's data back from the disk files in the Mnesia directory before the node rejoins the cluster and is considered ready — the in-memory copy is rebuilt from what was durably written to disk, plus replaying any transaction log entries recorded since the last fu...

Read full answer

35. How do you change a table's storage type at runtime?

mnesia:change_table_copy_type/3 converts a table's storage type on a given node while the system stays live — for example, promoting a ram_copies table to disc_copies once you decide it needs durability, without dropping and recreating it. mnesia:change_table_copy_type(person, node(), disc_...

Read full answer

36. What is a Mnesia table lock and how does it work?

Inside a transaction, Mnesia automatically acquires locks on the specific records (or, in some cases, the whole table) a transaction touches, so concurrent transactions can't corrupt each other's view of the data. Reads take a read lock ; writes take a write lock , which is exclusive and blocks o...

Read full answer

37. How does Mnesia keep replicated tables consistent across nodes?

Every write inside a transaction on a replicated table is propagated to all nodes holding a copy as part of that same transaction's commit, coordinated through a two-phase commit protocol: a coordinating node asks each replica to prepare the write, and only tells them all to actually commit once ...

Read full answer

38. How do you migrate a Mnesia record definition when its fields change?

Simply changing a -record definition in your code and recompiling doesn't retroactively update records already stored in a Mnesia table — existing rows keep whatever shape they were written with, which will mismatch the new record definition the next time your code tries to read them. %% ol...

Read full answer

39. What is mnesia:transform_table/3 used for?

mnesia:transform_table(Table, Fun, NewAttributes) rewrites every record in a table according to a supplied function, and updates the table's declared attribute list to match the new shape — the primary tool for evolving a table's schema after data already exists in it. mnesia:transform_tabl...

Read full answer

40. When should you choose bag over set for a Mnesia table?

Choose bag when a single key naturally has multiple associated values that all need to coexist as separate records — a classic one-to-many relationship you'd otherwise model with a separate join table in a relational database. m nes ia : crea te _ ta ble(i te m_ ta g , [{ a ttr ibu tes , [ ...

Read full answer

41. How do you back up a Mnesia database?

mnesia:backup/1 writes a point-in-time snapshot of the entire database (schema plus table data) to a file, which can later be restored on the same or a different set of nodes. mnesia:backup("/var/backups/mnesia_20260101.bak"). For finer control — backing up only specific tables, or customiz...

Read full answer

42. How do you restore a Mnesia database from a backup?

mnesia:restore/2 loads data from a backup file back into a running (or freshly initialized) Mnesia instance, with options controlling exactly how the restored data merges with what's already there. m nes ia : res t ore( "/var/backups/mnesia_20260101.bak" , [{ de fault _op , recrea te _ ta bles }]...

Read full answer

43. Explain the lifecycle of a Mnesia transaction from start to commit?

Calling mnesia:transaction(Fun) kicks off a well-defined sequence: Mnesia runs your function, tracking every read and write it performs, acquiring the appropriate locks as it goes, before deciding whether the whole thing can be committed. flowchart TD A[mnesia:transaction Fun called] --> B[Fun ex...

Read full answer

44. How do you troubleshoot a Mnesia table stuck in the "loading" state?

A table stuck loading after a restart usually means Mnesia can't decide it has a safe, up-to-date copy to load from — commonly after an unclean shutdown, a network partition that separated replicas, or a node coming back up while unsure whether another replica holds newer data. mnesia:table...

Read full answer

45. What is mnesia:force_load_table/1 and when should you use it?

mnesia:force_load_table/1 tells Mnesia to load a table from the local node's copy immediately, overriding its normal safety check that waits to confirm it has the most up-to-date, authoritative copy before proceeding. It's a deliberate override for a table that's stuck loading and blocking system...

Read full answer

46. What is the difference between mnesia:read/1 and mnesia:wread/1?

Both fetch a record by key inside a transaction, but they take different lock types up front. mnesia:read/1 takes a read lock, which is fine for a plain lookup, but if you intend to immediately write back an updated version of that same record, a read lock can force an extra lock upgrade step. mn...

Read full answer

47. What is a Mnesia checkpoint used for?

A checkpoint captures a consistent, point-in-time view of one or more tables while the system keeps running — it's the mechanism backups and certain replication operations use internally to get a stable snapshot without pausing the whole database. { ok , Name , Nodes } = m nes ia : ac t iva...

Read full answer

48. How does Mnesia resolve conflicts after a network partition heals?

When a network partition splits a cluster, each side can keep operating independently on its own copy of a replicated table, potentially applying different, conflicting writes to the same records on each side. When the partition heals and the nodes reconnect, Mnesia detects this as an inconsisten...

Read full answer

«
»

Comments & Discussions