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

감독자 소개

감독자가 실패한 프로세스를 자동으로 다시 시작하여 Erlang 애플리케이션의 내결함성과 고가용성을 보장하는 방식을 알아봅니다.

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

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

Meet Erlang Supervisors

In Erlang, processes are designed to crash! But who handles the mess? That's where Supervisors come in.

A supervisor is a special Erlang process whose job is to start, stop, and monitor other processes, called its children.

If a child process crashes, the supervisor automatically restarts it. This makes your applications incredibly resilient and fault-tolerant!

Why Fault Tolerance Matters

Imagine a web server process handling user requests. What happens if it crashes due to an error?

  • Without a supervisor, the server stops, and users lose service.
  • With a supervisor, the crashed process is detected and restarted instantly, often without users even noticing!

This "let it crash" philosophy, combined with supervisors, is key to Erlang's legendary reliability.

How Supervisors Work

Supervisors are part of Erlang's Open Telecom Platform (OTP) framework. They follow a simple hierarchy:

  • A supervisor has a list of child processes it's responsible for.
  • Each child is defined by a child specification.
  • If a child terminates unexpectedly, the supervisor steps in to restart it according to a defined strategy.

They form "supervision trees" where supervisors can supervise other supervisors.

Defining Child Processes

Before a supervisor can manage a process, it needs to know how to start it. This is done via a child specification.

A child spec is a record (or map) containing details like:

  • id: A unique name for the child.
  • start: The module, function, and arguments to call to start the process.
  • restart: When and how to restart (e.g., permanent, temporary).
  • type: Whether it's a worker or another supervisor.

Restart Strategy: One For One

Supervisors use restart strategies to decide what to do when a child crashes. The most common is one_for_one.

With one_for_one:

  • If a child process terminates, only that specific child process is restarted.
  • Other sibling processes managed by the same supervisor are unaffected.

This strategy is ideal when children are independent and a failure in one doesn't impact the others.

Our First Supervised Worker

Let's create a simple Erlang module that will act as a worker process. It will just start, print a message, and then we'll make it crash.

-module(my_worker).
-behaviour(gen_server).

-export([start_link/0, init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
-export([crash_me/0]).

start_link() ->
    gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

init([]) ->
    io:format("Worker started!~n", []),
    {ok, #{}}.

handle_call(crash, _From, State) ->
    io:format("Worker told to crash!~n", []),
    exit(reason_for_crash),
    {reply, ok, State};
handle_call(_Request, _From, State) ->
    {noreply, State}.

handle_cast(_Msg, State) ->
    {noreply, State}.

handle_info(_Info, State) ->
    {noreply, State}.

terminate(_Reason, _State) ->
    io:format("Worker terminating!~n", []).

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.

crash_me() ->
    gen_server:call(?MODULE, crash).

Setting up Our Supervisor

Now, let's create a supervisor module that will manage our my_worker. We'll specify the one_for_one restart strategy.

-module(my_supervisor).
-behaviour(supervisor).

-export([start_link/0, init/1]).

start_link() ->
    supervisor:start_link({local, ?MODULE}, ?MODULE, []).

init([]) ->
    WorkerSpec = #{
        id => my_worker,
        start => {my_worker, start_link, []},
        restart => permanent,
        type => worker,
        shutdown => 5000,
        via => [{local, my_worker}]
    },
    Children = [WorkerSpec],
    Strategy = #{
        strategy => one_for_one,
        intensity => 10,
        period => 1
    },
    {ok, {Strategy, Children}}.

Launching the Application

To see our supervisor in action, we need to start it. We can do this directly from the Erlang shell or a main application module.

Here's how to start it and check its children:

-module(app_starter).
-export([start/0, stop/0]).

start() ->
    my_supervisor:start_link(),
    io:format("Supervisor started. Worker should be running.~n", []).

stop() ->
    supervisor:stop(my_supervisor),
    io:format("Supervisor stopped.~n", []).

% To run this in the shell:
% 1. Compile: c(my_worker), c(my_supervisor), c(app_starter).
% 2. Start: app_starter:start().
% 3. Crash: my_worker:crash_me().
% 4. Observe restarts!

Witnessing Fault Tolerance

After compiling and running app_starter:start()., you should see "Worker started!". Now, call my_worker:crash_me(). in the shell.

What happens?

  • The worker process will terminate ("Worker terminating!").
  • The supervisor detects the crash and restarts the worker.
  • You'll see "Worker started!" again, demonstrating automatic recovery!

This shows the power of supervisors in keeping your system running even when individual components fail.

Supervisor Check-up

Which of the following statements correctly describe the purpose or behavior of an Erlang supervisor with a one_for_one restart strategy?

Supervisors: Your Reliability Hero

Great job! You've learned the fundamentals of Erlang supervisors:

  • They are special processes that monitor and restart child processes.
  • They ensure fault tolerance and high availability by automatically recovering from crashes.
  • Child specifications define how supervisors manage their children.
  • The one_for_one strategy restarts only the failed child.

Supervisors are a cornerstone of robust Erlang/OTP applications. Next, we'll explore more advanced restart strategies!

자주 묻는 질문

“감독자 소개” 강의는 무료인가요?

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

“감독자 소개”에서 뭘 배우나요?

감독자가 실패한 프로세스를 자동으로 다시 시작하여 Erlang 애플리케이션의 내결함성과 고가용성을 보장하는 방식을 알아봅니다. 브라우저에서 직접 실행하는 실습 코드로 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개 중 3번째 강의입니다.

“감독자 소개” 강의는 얼마나 걸리나요?

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

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

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

이 강의의 모든 강의

  1. OTP 및 동작 이해하기
  2. GenServer 동작 구현
  3. 감독자 소개
  4. OTP 애플리케이션과 릴리스 구축
← Erlang OTP: Distributed & Fault-Tolerant Systems Programming(으)로 돌아가기