Erlang -- Mnesia

Mnesia

User's Guide

Version 4.15.3

- User's Guide
- Reference Manual
- Release Notes
- PDF
- Top

- Expand All
- Contract All

### Chapters

- Introduction
  - Top of chapter
  - Scope
  - Prerequisites
- Mnesia
  - Top of chapter
  - Mnesia Database Management System (DBMS)
- Getting Started
  - Top of chapter
  - Starting Mnesia for the First Time
  - Example
- Build a Mnesia Database
  - Top of chapter
  - Define a Schema
  - Data Model
  - Start Mnesia
  - Create Tables
- Transactions and Other Access Contexts
  - Top of chapter
  - Transaction Properties
  - Locking
  - Dirty Operations
  - Record Names versus Table Names
  - Activity Concept and Various Access Contexts
  - Nested Transactions
  - Pattern Matching
  - Iteration
- Miscellaneous Mnesia Features
  - Top of chapter
  - Indexing
  - Distribution and Fault Tolerance
  - Table Fragmentation
  - Local Content Tables
  - Disc-Less Nodes
  - More about Schema Management
  - Mnesia Event Handling
  - Debugging Mnesia Applications
  - Concurrent Processes in Mnesia
  - Prototyping
  - Object-Based Programming with Mnesia
- Mnesia System Information
  - Top of chapter
  - Database Configuration Data
  - Core Dumps
  - Dumping Tables
  - Checkpoints
  - Startup Files, Log File, and Data Files
  - Loading Tables at Startup
  - Recovery from Communication Failure
  - Recovery of Transactions
  - Backup, Restore, Fallback, and Disaster Recovery
- Combine Mnesia with SNMP
  - Top of chapter
  - Combine Mnesia and SNMP
- Appendix A: Backup Callback Interface
  - Top of chapter
  - mnesia_backup Callback Behavior
- Appendix B: Activity Access Callback Interface
  - Top of chapter
  - mnesia_access Callback Behavior
- Appendix C: Fragmented Table Hashing Callback Interface
  - Top of chapter
  - mnesia_frag_hash Callback Behavior

# 2 Mnesia

The management of data in telecommunications system has many aspects, thereof some, but not all, are addressed by traditional commercial Database Management Systems (DBMSs). In particular the high level of fault tolerance that is required in many nonstop systems, combined with requirements on the DBMS to run in the same address space as the application, have led us to implement a new DBMS, called Mnesia.

Mnesia is implemented in, and tightly connected to Erlang. It provides the functionality that is necessary for the implementation of fault tolerant telecommunications systems.

Mnesia is a multiuser distributed DBMS specially made for industrial telecommunications applications written in Erlang, which is also the intended target language. Mnesia tries to address all the data management issues required for typical telecommunications systems. It has a number of features that are not normally found in traditional databases.

In telecommunications applications, there are different needs from the features provided by traditional DBMSs. The applications now implemented in Erlang need a mixture of a broad range of features, which generally are not satisfied by traditional DBMSs. Mnesia is designed with requirements like the following in mind:

- Fast real-time key/value lookup
- Complicated non-real-time queries mainly for operation and maintenance
- Distributed data because of distributed applications
- High fault tolerance
- Dynamic reconfiguration
- Complex objects

Mnesia is designed with the typical data management problems of telecommunications applications in mind. This sets Mnesia apart from most other DBMS. Hence Mnesia combines many concepts found in traditional databases such as transactions and queries with concepts found in data management systems for telecommunications applications, for example:

- Fast real-time operations
- Configurable degree of fault tolerance (by replication)
- The ability to reconfigure the system without stopping or suspending it.

Mnesia is also interesting because of its tight coupling to Erlang, thus almost turning Erlang into a database programming language. This has many benefits, the foremost is that the impedance mismatch between the data format used by the DBMS and the data format used by the programming language, which is used to manipulate the data, completely disappears.

### 2.1 Mnesia Database Management System (DBMS)

#### Features

Mnesia contains the following features that combine to produce a fault-tolerant, distributed DBMS written in Erlang:

- Database schema can be dynamically reconfigured at runtime.
- Tables can be declared to have properties such as location, replication, and persistence.
- Tables can be moved or replicated to several nodes to improve fault tolerance. The rest of the system can still access the tables to read, write, and delete records.
- Table locations are transparent to the programmer. Programs address table names and the system itself keeps track of table locations.
- Database transactions can be distributed, and many functions can be called within one transaction.
- Several transactions can run concurrently, and their execution is fully synchronized by the DBMS. Mnesia ensures that no two processes manipulate data simultaneously.
- Transactions can be assigned the property of being executed on all nodes in the system, or on none. Transactions can also be bypassed in favor of running "dirty operations", which reduce overheads and run fast.

Details of these features are described in the following sections.

#### Add-On Application

Query List Comprehension (QLC) can be used with Mnesia to produce specialized functions that enhance the operational ability of Mnesia. QLC has its own documentation as part of the OTP documentation set. The main features of QLC when used with Mnesia are as follows:

- QLC can optimize the query compiler for the Mnesia DBMS, essentially making the DBMS more efficient.
- QLC can be used as a database programming language for Mnesia. It includes a notation called "list comprehensions" and can be used to make complex database queries over a set of tables.

For information about QLC, see the qlc manual page in STDLIB.

#### When to Use Mnesia

Use Mnesia with the following types of applications:

- Applications that need to replicate data.
- Applications that perform complicated searches on data.
- Applications that need to use atomic transactions to update several records simultaneously.
- Applications that use soft real-time characteristics.

Mnesia is not as appropriate with the following types of applications:

- Programs that process plain text or binary data files.
- Applications that merely need a look-up dictionary that can be stored to disc. Those applications use the standard library module dets, which is a disc-based version of the module ets. For information about dets, see the dets manual page in STDLIB.
- Applications that need disc logging facilities. Those applications can use the module disk_log by preference. For information about disk_log, see the disk_log manual page in Kernel.
- Hard real-time systems.