u/Unusual_Shame_3839

Revenant - What if GenServer flushed its state into PostgreSQL?
▲ 12 r/erlang+1 crossposts

Revenant - What if GenServer flushed its state into PostgreSQL?

I've been using and actively fascinated by Oban over the last 4 years, and recently I wondered, why can't we make GenServers behave similarly to Oban jobs and:

  • Recover their state after a crash or termination
  • Terminate themselves when not used
  • Store a trace of their execution
  • And do all that without any additional dependencies, using the database we're already used to

I can think of a ton of cases where this might be useful in... Durable GenServers? LiveViews? Distributed GenServers that share state? Hot/Cold executions? Slow/Fast pools?

So I created a PoC library that does just that:

https://github.com/dimamik/revenant

defmodule Account do
  use Revenant, repo: MyApp.Repo

  def initial_state(_id), do: %{balance: 0}

  def handle_call({:deposit, amount}, _from, state) do
    {:reply, :ok, %{state | balance: state.balance + amount}}
  end

  def handle_call(:balance, _from, state) do
    {:reply, state.balance, state}
  end
end

Revenant.call({Account, "acct_42"}, {:deposit, 100})
#=> :ok  - the new state is committed to Postgres before you see this

Please let me know what you think of the concept!

u/Unusual_Shame_3839 — 8 hours ago