website logo

← All articles

The Actor Model on Ruby: Ractor::Port Changes Everything - A Side-by-Side with Elixir/OTP (Part 2)

The Ractor implementation got better in Ruby 4.0, let's refresh our exposure.

A few years ago, I wrote the article “explaining the introduction of ractors into Ruby”, and time has passed so I decided to write a follow up from that original Part 1




Ruby 4.0 shipped with Ractor::Port, and it fundamentally changes how we write concurrent Ruby. Gone are the awkward days of Ractor.yield and Ractor#take. Gone is the global Ractor.receive mailbox that swallowed messages from anyone. Ports give us first-class, addressable mailboxes, the missing piece that makes Ractors feel like actual actors.

So let’s do what we do best: roll up our sleeves again, write some code, and compare the two approaches side by side.

As like the Part 1, it’s a technical dive for engineers who want to understand the tradeoffs at a fundamental level, and why Ruby 4.0 is a bigger deal than the release notes suggest.

The Core Primitives: Ports vs. Process Mailboxes

In Elixir, every process has a mailbox. You send a message to a PID, and that specific process receives it. No ambiguity.

Before Ruby 4.0, Ractors had a default mailbox, but if you wanted to wait for a specific reply, you were stuck using Ractor.yield and Ractor#take, a rendezvous-style dance that was hard to compose and even harder to reason about under load.

Ractor::Port fixes this. A Port is a lightweight, addressable endpoint. You create one, hand it to whoever needs to send you messages, and only you can receive from it. This is the actor model, done right.

Here’s the simplest possible actor in both worlds:

Elixir:

pid = spawn(fn ->
  receive do
    {:hello, name} -> IO.puts("Hello, #{name}!")
  end
end)

send(pid, {:hello, "CTO"})

Ruby 4.0 with Port:

port = Ractor::Port.new

Ractor.new(port) do |p|
  msg = p.receive
  puts "Hello, #{msg}!"
end

port.send("CTO")

The symmetry is striking. But the semantics underneath still differ in important ways.

Building a Server: GenServer vs. A Port-Driven Loop

Elixir’s GenServer is the workhorse abstraction. It wraps a process with a receive loop, handles synchronous calls, asynchronous casts, and state management cleanly.

Let’s build a simple counter server in both. Watch how Ports make the Ruby version dramatically cleaner than the old yield/take approach.

Elixir GenServer:

defmodule Counter do
  use GenServer

  def start_link(initial) do
    GenServer.start_link(__MODULE__, initial, name: __MODULE__)
  end

  def increment, do: GenServer.cast(__MODULE__, :increment)
  def get, do: GenServer.call(__MODULE__, :get)

  @impl true
  def init(initial), do: {:ok, initial}

  @impl true
  def handle_cast(:increment, state) do
    {:noreply, state + 1}
  end

  @impl true
  def handle_call(:get, _from, state) do
    {:reply, state, state}
  end
end

Ruby 4.0 with Ractor::Port:

class Counter
  def initialize(initial)
    @reply_port = Ractor::Port.new

    @ractor = Ractor.new(initial, @reply_port) do |state, reply_port|
      loop do
        msg = Ractor.receive
        case msg
        in [:cast, :increment]
          state += 1
        in [:call, :get]
          reply_port.send(state)
        end
      end
    end
  end

  def increment
    @ractor.send([:cast, :increment])
  end

  def get
    @ractor.send([:call, :get])
    @reply_port.receive
  end
end

Look at that. The reply_port is created once in the parent, passed to the Ractor, and used for all synchronous replies. No more creating a temporary reply channel per call. No more Ractor.yield blocking the worker. No more tag collisions from Ractor.receive accidentally picking up someone else’s message.

This is the difference between “Ruby can do actors” and “Ruby has actors.”

Why Ports Are a Bigger Deal Than They Look

The Ruby core team didn’t just add a convenience class. They deprecated Ractor.yield and Ractor#take in Ruby 4.0 because Ports make them obsolete. That’s a strong signal: this is the future direction of concurrency in Ruby.

Here’s what Ports give you that the old primitives couldn’t:

1. Addressed delivery, not broadcast. Ractor.receive (the old way) pulls from the Ractor’s default mailbox. If three different callers send you messages, you can’t easily tell who sent what. A Port is a dedicated channel. Messages sent to your reply port go only to you.

2. Non-blocking sends. port.send(obj) returns immediately. It doesn’t wait for the receiver to be ready. This is how actor mailboxes should work.

3. Lifecycle awareness. If port.send succeeds, the destination Ractor is still alive. With the old channel pattern, you had no such guarantee since the receiver might have already terminated.

4. Closing semantics. You can port.close, and subsequent sends raise Ractor::ClosedError. This gives you clean shutdown protocols without polling or timeouts.

For a Rails engineer building background workers or parallel data pipelines, these aren’t academic improvements. They’re the difference between code you trust in production and code you pray doesn’t deadlock at 3 AM.

Supervision: The Elephant That Hasn’t Moved

Let’s be honest. Elixir’s “let it crash” philosophy works because of supervisors. When a GenServer crashes, a supervisor restarts it. The system self-heals.

Ractors still don’t have built-in supervision. If a Ractor raises, it propagates to whoever calls Ractor#value or Ractor#join. You can rescue it, but there’s no automatic restart strategy.

But Ports make building supervision easier. The Ruby feature proposal explicitly discusses using Ports for monitoring Ractor termination, essentially a lightweight monitor pattern like Erlang’s. You can imagine a supervisor Ractor that holds Ports for each worker, receives termination notices, and spawns replacements.

It’s not OTP. But it’s a foundation you can build on, rather than a hole you have to work around.

Where Ractors Shine: Memory Efficiency and Parallelism

The traditional way to get CPU parallelism in Rails is to fork Puma workers. Each worker gets its own GVL and its own memory footprint. Eight workers can consume gigabytes.

Ractors offer parallelism within a single process. Because they share the process space for shareable objects but have isolated heaps for their own work, a pool of Ractors can use far less memory than forked workers.

With Ractor::Port making inter-Ractor communication cleaner, the barrier to experimenting with this architecture just dropped significantly. You can now build a worker pool where tasks are dispatched via Ports, results flow back via reply Ports, and the whole thing lives in one process.

When to Reach for Which, a reminder

Reach for Ractors + Ports when:

  • You have CPU-bound work in a Rails app that you want to parallelize without forking.
  • You’re memory-constrained and want to reduce worker count.
  • You’re building a new service and want actor-style message passing with clean, addressed mailboxes.

Reach for Elixir/OTP when:

  • Concurrency and fault tolerance are the core architectural requirements.
  • You need millions of lightweight processes.
  • You want supervision, hot code reloading, and distribution for free.

The Ruby community’s adoption of the actor model isn’t about beating Elixir at its own game. It’s about making Ruby better at the problems Ruby engineers already face. Ractor::Port is the most significant step in that direction since Ractors first landed.

Ractors are not Erlang processes. They never will be. But with Ports, they’re finally composable. And for an engineer building Rails systems at scale, that’s something worth getting excited about.

The actor model is here to stay on Ruby, properly this time.