💻 Programming

Phoenix

Phoenix Framework

A high-performance web framework based on Elixir. Implements real-time UIs with LiveView. Enables scalable web application development using the concurrency and fault tolerance of the Erlang VM.

📖 Detailed Explanation

Phoenix is a high-performance web framework written in the Elixir language that runs on the Erlang VM (BEAM). Inspired by Ruby on Rails, it offers a developer-friendly structure while providing the scalability to handle millions of concurrent connections through Erlang's powerful concurrency model. It is based on the MVC pattern and natively supports real-time communication through Channels.

Phoenix, whose development was started by Chris McCord in 2014, was born with the goal of combining the productivity of Rails with the reliability of Erlang. After experiencing the limits of handling large-scale traffic as a Rails developer, McCord took notice of the potential of Elixir and the Erlang VM. As a result, he established a distinctive positioning: "the convenience of Rails + the performance of Erlang."

Phoenix LiveView is an innovative feature introduced in 2019 that implements real-time interactive UIs with server-side rendering alone. It synchronizes server and client state over WebSocket and provides a user experience similar to that of a SPA (Single Page Application) without JavaScript. As a result, the frontend and backend can be developed in a single language, which greatly improves development productivity.

In practice, the greatest strengths of Phoenix are concurrency and fault tolerance. The lightweight processes of the Erlang VM are far lighter than OS threads, and thanks to isolation between processes, an error in one does not affect the entire system. Following the "Let it crash" philosophy, a Supervisor automatically restarts failed processes, making it possible to achieve 99.9999% (Six Nines) availability. Companies such as Discord, Pinterest, and Bleacher Report use Phoenix to operate large-scale real-time services.

💻 Code Example

Controller - REST API endpoint
# lib/my_app_web/controllers/user_controller.ex
defmodule MyAppWeb.UserController do
  use MyAppWeb, :controller

  alias MyApp.Accounts
  alias MyApp.Accounts.User

  # GET /api/users
  def index(conn, _params) do
    users = Accounts.list_users()
    render(conn, :index, users: users)
  end

  # GET /api/users/:id
  def show(conn, %{"id" => id}) do
    user = Accounts.get_user!(id)
    render(conn, :show, user: user)
  end

  # POST /api/users
  def create(conn, %{"user" => user_params}) do
    case Accounts.create_user(user_params) do
      {:ok, %User{} = user} ->
        conn
        |> put_status(:created)
        |> put_resp_header("location", ~p"/api/users/#{user}")
        |> render(:show, user: user)

      {:error, %Ecto.Changeset{} = changeset} ->
        conn
        |> put_status(:unprocessable_entity)
        |> render(:error, changeset: changeset)
    end
  end
end
LiveView - Real-time counter (without JavaScript!)
# lib/my_app_web/live/counter_live.ex
defmodule MyAppWeb.CounterLive do
  use MyAppWeb, :live_view

  # 초기 상태 설정
  def mount(_params, _session, socket) do
    {:ok, assign(socket, count: 0)}
  end

  # 실시간 UI 렌더링 (HEEx 템플릿)
  def render(assigns) do
    ~H"""
    <div class="counter-container">
      <h1>실시간 카운터</h1>
      <p class="count"><%= @count %></p>

      <div class="buttons">
        <button phx-click="decrement">-</button>
        <button phx-click="increment">+</button>
        <button phx-click="reset">Reset</button>
      </div>
    </div>
    """
  end

  # 이벤트 핸들러 - 서버에서 처리, 자동으로 UI 업데이트
  def handle_event("increment", _params, socket) do
    {:noreply, update(socket, :count, &(&1 + 1))}
  end

  def handle_event("decrement", _params, socket) do
    {:noreply, update(socket, :count, &(&1 - 1))}
  end

  def handle_event("reset", _params, socket) do
    {:noreply, assign(socket, count: 0)}
  end
end

# router.ex에 추가
# live "/counter", CounterLive
PubSub - Real-time chat broadcast
# lib/my_app_web/live/chat_live.ex
defmodule MyAppWeb.ChatLive do
  use MyAppWeb, :live_view

  def mount(%{"room" => room}, _session, socket) do
    # 채팅방 토픽 구독
    if connected?(socket) do
      Phoenix.PubSub.subscribe(MyApp.PubSub, "chat:#{room}")
    end

    {:ok, assign(socket, room: room, messages: [], message: "")}
  end

  # 메시지 전송 - 모든 구독자에게 브로드캐스트
  def handle_event("send", %{"message" => msg}, socket) do
    Phoenix.PubSub.broadcast(
      MyApp.PubSub,
      "chat:#{socket.assigns.room}",
      {:new_message, %{text: msg, time: DateTime.utc_now()}}
    )
    {:noreply, assign(socket, message: "")}
  end

  # 브로드캐스트 메시지 수신
  def handle_info({:new_message, message}, socket) do
    messages = socket.assigns.messages ++ [message]
    {:noreply, assign(socket, messages: messages)}
  end
end

🗣️ Examples of Practical Conversations

In a technical review meeting on implementing real-time features

"We need to implement real-time notifications and chat, but if we attach Socket.IO with Node.js, frontend state management becomes complicated. If we use Phoenix LiveView, state is managed on the server and WebSocket is handled automatically, so the amount of code drops to less than half. Discord also handles millions of concurrent connections with Phoenix."

Technology stack decision discussion: Phoenix vs Rails

"Rails has a broader ecosystem, but real-time dashboards are the core of our service. There's also a benchmark in which Phoenix handled 2 million WebSocket connections on a single server. On top of that, Elixir syntax is similar to Ruby, so Rails developers adapt in 2-3 weeks. Fault tolerance is also built in, so no separate infrastructure is needed for failure recovery."

In a technical interview

"The reason Phoenix is high-performance is the lightweight process model of the Erlang VM. One request is handled in one process, and each process is very light at about 2KB. That's why even hundreds of thousands of concurrent connections can be handled on a single server. In contrast, Java and Ruby are thread-based, so the memory overhead is large."

In a code review - LiveView optimization

"This LiveView is updating all assigns on every handle_event. Since Phoenix LiveView sends only the changed parts to the client, it's better to update only the assigns you need. Instead of assign(socket, items: new_items), switch to update(socket, :items, fn _ -> new_items end), and handle heavy lists with stream/3 to reduce memory usage as well. And if PubSub broadcasts are frequent, consider debouncing."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

⚠️ Cautions

1
Elixir learning curve

To use Phoenix properly, an understanding of Elixir and functional programming is essential. Time is needed to adapt to a paradigm different from OOP, including pattern matching, immutability, recursion, and the pipe operator. Reflect the training cost and time for the entire team in the project plan.

2
Ecosystem size

The Elixir/Phoenix ecosystem has fewer libraries than Node.js or Python. Certain third-party API SDKs or specialized domain libraries may not exist, so you may need to implement them yourself or integrate Erlang libraries. Check the availability of required libraries before starting the project.

3
Hiring market

The Elixir/Phoenix developer pool is relatively small. In the Korean market in particular, hiring experienced developers can be difficult. You should consider alternatives such as training existing team members, remote hiring, or recruiting people with Ruby/Erlang experience and then providing transition training.

🔗 Related Terms

📚 Learn More