erleans/erleans
{ "createdAt": "2017-03-04T18:24:41Z", "defaultBranch": "main", "description": "Erlang Orleans", "fullName": "erleans/erleans", "homepage": null, "language": "Erlang", "name": "erleans", "pushedAt": "2025-02-02T12:39:53Z", "stargazersCount": 300, "topics": [ "distributed-actors", "distributed-systems", "erlang", "orleans" ], "updatedAt": "2025-11-24T08:04:38Z", "url": "https://github.com/erleans/erleans"}Erleans
Section titled “Erleans”Erleans is a framework for building distributed applications in Erlang and Elixir based on Microsoft Orleans.
Requirements
Section titled “Requirements”Rebar3 3.24.0 or above or Elixir 1.18+.
Components
Section titled “Components”Grains
Section titled “Grains”Stateful grains are backed by persistent storage and referenced by a primary key set by the grain. An activation of a grain is a single Erlang process in on an Erlang node (silo) in an Erlang cluster. Activation placement is handled by Erleans and communication is over standard Erlang distribution. If a grain is sent a message and does not have a current activation one is spawned.
Grain state is persisted through a database provider with an always increasing change id or etag. If the change id or etag has been by another activation the activation attempting to save state will stop.
Activations are registered through global by default.
Stateless Grains
Section titled “Stateless Grains”Stateless grains have no restriction on the number of activations and do not persist state to a database.
Stateless grain activations are pooled through gproc.
Reminders (TODO)
Section titled “Reminders (TODO)”Timers that are associated with a grain, meaning if a grain is not active but a reminder for that grain ticks the grain is activated at that time and the reminder is delivered.
Observers (TODO)
Section titled “Observers (TODO)”Processes can subscribe to grains to receive notifications for grain specific events. If a grain supports observers a group is created through pg.
Providers
Section titled “Providers”Interface that must be implemented for any persistent store to be used for grains.
Streams have a provider type as well for providing a pluggable stream layer.
Differences from gen_server
Section titled “Differences from gen_server”No starting or linking, a grain is activated when it is sent a request if an activation is not currently running.
Grain Placement
Section titled “Grain Placement”prefer_local: If an activation does not exist this causes the new activation to be on the same node making the request.random: Picks a random node to create any new activation of a grain.stateless: Stateless grains are always local. If no local activation to the request exists one is created up to a default maximum value.{stateless, Max :: integer()}: Allows for up toMaxnumber of activations for a grain to exist per node. A new activation, up untilMaxexist on the node, will be created for a request if an existing activation is not currently busy.
Erlang Example
Section titled “Erlang Example”The grain implementation test_grain is found in test/:
-module(test_grain).
-behaviour(erleans_grain).
...
placement() -> prefer_local.
provider() -> in_memory.
deactivated_counter(Ref) -> erleans_grain:call(Ref, deactivated_counter).
activated_counter(Ref) -> erleans_grain:call(Ref, activated_counter).
node(Ref) -> erleans_grain:call(Ref, node).
state(_) -> #{activated_counter => 0, deactivated_counter => 0}.
activate(_, State=#{activated_counter := Counter}) -> {ok, State#{activated_counter => Counter+1}, #{}}.$ rebar3 as test shell...> Grain1 = erleans:get_grain(test_grain, <<"grain1">>).> test_grain:activated_counter(Grain1).{ok, 1}Elixir Example
Section titled “Elixir Example”defmodule ErleansElixirExample do use Erleans.Grain, placement: :prefer_local, provider: :postgres, state: %{:counter => 0}
def get(ref) do :erleans_grain.call(ref, :get) end
def increment(ref) do :erleans_grain.cast(ref, :increment) end
def handle_call(:get, from, state = %{:counter => counter}) do {:ok, state, [{:reply, from, counter}]} end
def handle_cast(:increment, state = %{:counter => counter}) do new_state = %{state | :counter => counter + 1} {:ok, new_state, [:save_state]} endend$ mix deps.get$ mix compile$ iex --sname a@localhost -S mix
iex(a@localhost)1> ref = Erleans.get_grain(ErleansElixirExample, "somename")...iex(a@localhost)2> ErleansElixirExample.get(ref)0iex(a@localhost)3> ErleansElixirExample.increment(ref):okiex(a@localhost)4> ErleansElixirExample.get(ref)1Contributing
Section titled “Contributing”Running Tests
Section titled “Running Tests”$ epmd -daemon$ rebar3 ct