Mnesia - Replicate ram_copy table to disc_only_copy table from another node
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Mnesia is a distributed DataBase Management System (DBMS) that comes bundled with the Erlang/OTP platform. It provides the unique capability of creating and managing both in-memory (RAM) and on-disk tables either individually or in a hybrid form. In diverse environments, where both speed and durability are crucial, understanding how to replicate data from a RAM-based table to a disk-based table across different nodes becomes essential.
Why Replicate from ram_copy to disc_only_copy?
Replicating a table from ram_copy to disc_only_copy is useful in scenarios where:
- You want to maintain a fast-read access table on one node and a persistent copy on another for recovery and backup.
- The system is designed to handle high throughput data in RAM with periodic backups to disk for durability.
How Does Mnesia Replication Work?
Mnesia replication is built around the concept of table copies. Each table can be configured to reside on several nodes with different storage types:
- ram_copy: The table is kept in RAM and lost on restart.
- disc_only_copy: The table is kept on disk.
- disc_copy: The table resides both in RAM for faster access and on disk for persistence.
Each node in an Erlang cluster can have different types of copies of the same table. The replication between nodes is handled by Mnesia's built-in transaction system, ensuring that all changes are atomic and consistent across the network.
Implementation Steps: Replicating ram_copy to disc_only_copy
Here's how to replicate data from a ram_copy table located on one node to a disc_only_copy on another node in an Erlang system:
- Setup: Ensure the Erlang nodes are connected and Mnesia is running on both nodes. Let's consider two nodes:
node1@hostandnode2@host. - Create a RAM Table on Node 1:
- Insert Data into the RAM Table:
- Create the disc_only_copy on Node 2:
- Copying Data from Node 1 to Node 2: The actual replication logic would be to write a distributed transaction that reads from
node1@hostand writes tonode2@host. Here’s an example function:
- Activate the Replication: Call the
replicate_data()anywhere in your application where you need to sync the nodes.
Summary Table
Here is a table summarizing the key points about configuring and executing table replication in Mnesia from ram_copy to disc_only_copy:
| Parameter | Description | Example |
| Source Node Setup | Create and populate ram_copy table. | mnesia:create_table with ram copies |
| Destination Node Setup | Define disc_only_copy table. | mnesia:create_table with disc_only copies |
| Replication Method | Use transaction to ensure consistency. | Distributed transaction copying entries |
| Trigger Replication | Based on application needs. | On specific intervals or system events |
Additional Considerations
- Consistency and Concurrency: Mnesia transactions ensure that the operations are atomic. However, handling race conditions and ensuring data integrity across nodes require careful design of the transaction functions.
- Error Handling: Implement robust error handling within the distributed transactions to manage possible network failures or conflicts during replication.
- Performance Impacts: Consider the network and memory load caused by frequent data replication, especially in large-scale systems.
By setting up appropriate nodes, table types, and data replication logic as described, Mnesia provides a powerful way to harness the benefits of both in-memory and on-disk data management in distributed Erlang applications. This capability is particularly valuable in systems requiring fast data access coupled with the need for data persistence and recovery.

