0Pricing
Erlang OTP: Distributed & Fault-Tolerant Systems Programming · 강의

ETS 및 Mnesia를 활용한 분산 데이터

메모리 내 분산 테이블을 위한 ETS와 경량 분산 데이터베이스 기능을 위한 Mnesia를 소개합니다.

ETS 및 Mnesia를 활용한 분산 데이터은(는) CoddyKit의 무료 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Erlang OTP: Distributed & Fault-Tolerant Systems Programming 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

Why Distributed Data?

In large-scale Erlang systems, data often needs to be shared and synchronized across multiple nodes. This is crucial for building fault-tolerant and scalable applications.

Imagine a chat application where user sessions or message queues need to be accessible even if one server fails. Distributed data stores make this possible by allowing data to live beyond a single process or node.

Erlang Term Storage (ETS)

ETS stands for Erlang Term Storage. It's a powerful, built-in in-memory key-value store. Think of it as a super-fast hash table directly integrated into the Erlang runtime.

  • Extremely fast for read/write operations.
  • Can store any Erlang term (integers, atoms, lists, tuples, PIDs, etc.).
  • Process-safe: multiple processes can access the same table concurrently.

Exploring ETS Table Types

ETS offers different table types, each with unique properties:

  • set: The default. Each key can only have one value. Like a dictionary.
  • ordered_set: Similar to set, but keys are kept sorted. Slower writes, faster range lookups.
  • bag: Allows multiple objects with the same key.
  • duplicate_bag: Allows multiple objects with the same key AND value.

Choosing the right type depends on your data's structure and access patterns.

Basic ETS Operations

Let's see how to create an ETS table and perform basic operations like inserting and looking up data. We'll use a set table for simplicity.

-module(ets_example).
-export([run/0, add_user/2, get_user/1, cleanup/0]).

% Function to run the example sequence
run() ->
    io:format("--- ETS Example Start ---~n"),
    % Create a named public set table
    ets:new(users_table, [set, public, named_table]),
    io:format("Table 'users_table' created.~n"),

    add_user(1, "Alice"),
    add_user(2, "Bob"),
    add_user(1, "Alicia"), % This will update Alice to Alicia

    get_user(1),
    get_user(2),
    get_user(3), % User 3 doesn't exist

    cleanup(),
    io:format("--- ETS Example End ---~n").

add_user(Id, Name) ->
    ets:insert(users_table, {Id, Name}),
    io:format("Inserted/updated user {~p, ~p}~n", [Id, Name]).

get_user(Id) ->
    case ets:lookup(users_table, Id) of
        [{Id, Name}] ->
            io:format("Lookup result for ~p: {~p, ~p}~n", [Id, Name]);
        [] ->
            io:format("User ~p not found in table.~n", [Id])
    end.

cleanup() ->
    ets:delete(users_table),
    io:format("Table 'users_table' deleted.~n").

ETS: Local Storage Only

While ETS is incredibly fast and concurrent, it's important to remember a key limitation: ETS tables are local to the Erlang node where they are created.

  • Data in an ETS table on node_A is not automatically available on node_B.
  • To share ETS data across nodes, you'd need to implement custom replication or remote access mechanisms, often using message passing or RPC.

This is where Mnesia comes in to simplify distributed data management.

Mnesia: Erlang's Distributed DB

Mnesia is a powerful, distributed, fault-tolerant, real-time database management system built into Erlang/OTP. It leverages ETS and DETS (Disk Erlang Term Storage) for its storage.

Mnesia simplifies the challenges of managing data across multiple Erlang nodes, providing transactional guarantees and automatic replication, making it ideal for robust distributed applications.

Mnesia Setup & Schema

Before using Mnesia, you need to start it and define your data schema. The schema describes your tables and their properties (e.g., what node hosts which copy).

  • mnesia:start(): Initializes the Mnesia system.
  • mnesia:create_schema([node()]): Creates the Mnesia directory and schema on the specified nodes. This is typically a one-time setup per cluster.
  • mnesia:create_table(Name, Props): Defines a new table with its attributes and storage type.

Mnesia Transactions

Mnesia operations are typically performed within transactions to ensure atomicity, consistency, isolation, and durability (ACID properties). This means all operations within a transaction either succeed or fail together.

Here's how to create a table and interact with it using transactions:

-module(mnesia_example).
-export([init_mnesia/0, add_product/2, get_product/1, cleanup/0]).

% Define a record for our product data
-record(product, {id, name}).

init_mnesia() ->
    io:format("--- Mnesia Init Start ---~n"),
    % Start Mnesia
    mnesia:start(),
    io:format("Mnesia started.~n"),

    % Uncomment and run 'mnesia_example:create_schema().' once for a new Mnesia setup:
    % mnesia:create_schema([node()]),
    % io:format("Mnesia schema created.~n"),

    % Create product table with disc_copies on this node
    % Uncomment to delete an existing table before creating a new one:
    % mnesia:delete_table(product),
    TableProps = [{disc_copies, [node()]}, {attributes, record_info(fields, product)}],
    case mnesia:create_table(product, TableProps) of
        {atomic, ok} -> io:format("Table 'product' created.~n");
        {aborted, {already_exists, product}} -> io:format("Table 'product' already exists.~n");
        Other -> io:format("Error creating table: ~p~n", [Other])
    end,
    io:format("--- Mnesia Init End ---~n").

add_product(Id, Name) ->
    Product = #product{id = Id, name = Name},
    F = fun() -> mnesia:write(Product) end,
    case mnesia:transaction(F) of
        {atomic, ok} -> io:format("Added product {~p, ~p}~n", [Id, Name]);
        {aborted, Reason} -> io:format("Failed to add product: ~p~n", [Reason])
    end.

get_product(Id) ->
    F = fun() -> mnesia:read({product, Id}) end,
    case mnesia:transaction(F) of
        {atomic, [ProductRecord]} ->
            io:format("Found product ~p: {~p, ~p}~n", [Id, ProductRecord#product.name]);
        {atomic, []} ->
            io:format("Product ~p not found.~n", [Id]);
        {aborted, Reason} ->
            io:format("Failed to get product: ~p~n", [Reason])
    end.

cleanup() ->
    io:format("--- Mnesia Cleanup Start ---~n"),
    mnesia:stop(),
    io:format("Mnesia stopped.~n"),
    io:format("--- Mnesia Cleanup End ---~n").

Mnesia Data Replication

Mnesia achieves distribution and fault tolerance through data replication. When creating a table, you specify where its copies should reside:

  • disc_copies: Data is stored on disk and in RAM on the specified nodes. Best for persistence and fault tolerance.
  • ram_copies: Data is stored only in RAM on the specified nodes. Faster access but data is lost on node crash unless a disc_copies is also present elsewhere.
  • disc_only_copies: Data is stored only on disk. Slowest access but lowest RAM footprint.

Mnesia automatically handles replication and synchronization across these copies, ensuring data consistency.

Quick Check: ETS vs Mnesia

Both ETS and Mnesia are powerful tools for managing data in Erlang, but they serve different purposes. Select all statements that correctly describe the key differences or characteristics.

Recap: Distributed Data Tools

In this lesson, we explored two essential tools for managing data in Erlang: ETS and Mnesia.

  • ETS is a super-fast, in-memory key-value store, excellent for local caches and lookup tables. Remember, it's local to a node.
  • Mnesia is Erlang's built-in distributed, fault-tolerant database. It offers transactional safety, schema definition, and automatic data replication across nodes, built upon ETS.

Understanding when to use each is key to building resilient and scalable Erlang applications.

자주 묻는 질문

“ETS 및 Mnesia를 활용한 분산 데이터” 강의는 무료인가요?

네 — “ETS 및 Mnesia를 활용한 분산 데이터” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 강의 전체를 잠금 해제할 수 있습니다. Erlang OTP: Distributed & Fault-Tolerant Systems Programming 강의에는 총 4개의 강의가 포함되어 있습니다.

“ETS 및 Mnesia를 활용한 분산 데이터”에서 뭘 배우나요?

메모리 내 분산 테이블을 위한 ETS와 경량 분산 데이터베이스 기능을 위한 Mnesia를 소개합니다. 브라우저에서 직접 실행하는 실습 코드로 Erlang OTP: Distributed & Fault-Tolerant Systems Programming을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Erlang OTP: Distributed & Fault-Tolerant Systems Programming을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Erlang OTP: Distributed & Fault-Tolerant Systems Programming은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.

“ETS 및 Mnesia를 활용한 분산 데이터” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Erlang OTP: Distributed & Fault-Tolerant Systems Programming 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 네트워크 분할 처리
  2. ETS 및 Mnesia를 활용한 분산 데이터
  3. 확장성 및 복원력을 위한 설계
  4. 노드 간 부하 분산과 장애 조치
← Erlang OTP: Distributed & Fault-Tolerant Systems Programming(으)로 돌아가기