# GenServer fail safe queue

**URL:** <https://forum.elixirforum.com/t/genserver-fail-safe-queue/12321>\
**Category:** Questions\
**Tags:** queues, genserver\
**Created:** [February 7, 2018, 10:46am UTC](https://forum.elixirforum.com/t/genserver-fail-safe-queue/12321 "2018-02-07T10:46:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![quatermain](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/quatermain/32/21781_2.png) [@quatermain](https://forum.elixirforum.com/u/quatermain)\
**Post date:** [February 7, 2018, 10:46am UTC](https://forum.elixirforum.com/t/genserver-fail-safe-queue/12321/1 "2018-02-07T10:46:02Z")

</div>

hello,  
I have phoenix application(not important) but I have a couple of GenServers with:

`use GenServer, restart: :transient, shutdown: 10_000`

and I have questions:

1. How can I prepare code for situation when GenServer crash down and I don’t want to lost current processing data or data waiting in queue
2. How can I do queue with more same workers(GenServers) because now when one GenServer si working with some stuff another request(from phoenix or “cron”) is waiting. (some good practise?)
3. Is it possible to use 3rd party queue/messages(kafka,rabbit, redis,…) without parsing params into string?

I tried using `Agent` with current processing state but it looks like it’s not very good idea with more “workers”

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kokolegorille/32/4784_2.png) [@kokolegorille](https://forum.elixirforum.com/u/kokolegorille)\
**Post date:** [February 7, 2018, 10:49am UTC](https://forum.elixirforum.com/t/genserver-fail-safe-queue/12321/2 "2018-02-07T10:49:56Z")

</div>

You can use [immortal](https://github.com/danielberkompas/immortal). Or add a stash server that will store state on terminate function of dying GenServer,

---

<div class="post-metadata">

**Author:** ![quatermain](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/quatermain/32/21781_2.png) [@quatermain](https://forum.elixirforum.com/u/quatermain)\
**Post date:** [February 7, 2018, 10:55am UTC](https://forum.elixirforum.com/t/genserver-fail-safe-queue/12321/3 "2018-02-07T10:55:10Z")

</div>

so do you think it’s good idea use something like this:

```elixir
defmodule Transactions do
  use GenServer, restart: :transient, shutdown: 10_000
  
  alias Transactions.Current

  def start_link(state \\ %{}) do
    GenServer.start_link( __MODULE__ , state, name: __MODULE__ )
  end

  def init(state) do
    perform_with_backup(&perform_something/1, Current.get_current())
    {:ok, state}
  end

  def handle_call({:something, params}, _from, state) do
    resp =
      perform_with_backup(&perform_something/1, params)
    {:reply, resp, state}
  end

  defp perform_with_backup(procedure, params) do
    Current.put_current(params)
    resp = procedure.(params)
    Current.clean_current()
    resp
  end
  defp perform_something(params) do
         # do something
  end
end

```

and

```elixir
defmodule Transactions.Current do
  use Agent

  def start_link(_) do
    Agent.start_link(fn -> %{} end, name: __MODULE__ )
  end

  def put_current(%{} = new_state) do
    Agent.update( __MODULE__ , fn _state -> new_state end)
  end

  def get_current() do
    Agent.get( __MODULE__ , fn state -> state end)
  end

  def clean_current() do
    Agent.update( __MODULE__ , fn _state -> %{} end)
  end
end

```

---

<div class="post-metadata">

**Author:** ![kokolegorille](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kokolegorille/32/4784_2.png) [@kokolegorille](https://forum.elixirforum.com/u/kokolegorille)\
**Post date:** [February 7, 2018, 11:00am UTC](https://forum.elixirforum.com/t/genserver-fail-safe-queue/12321/4 "2018-02-07T11:00:56Z")

</div>

The terminate function is called for clean up. Be cautious because it seems terminate is not always executed.

```elixir
defmodule Transactions do
  use GenServer, restart: :transient, shutdown: 10_000
  
  alias Transactions.Current

  # Take state from stach, or empty state
  def start_link(state \\ %{}) do
    GenServer.start_link( __MODULE__ , state, name: __MODULE__ )
  end

  ...
  def terminate(reason, state) do
    # Save state to stash
  end
  ...
end

```

There is a stash example in this presentation

[![](https://img.youtube.com/vi/ENolm5T0bkU/maxresdefault.jpg "ElixirConf 2017 - Managing Tables With Elixir and OTP - Robert Beene") ](https://www.youtube.com/watch?v=ENolm5T0bkU)

around 19:30…

---

<div class="post-metadata">

**Author:** ![peerreynders](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/peerreynders/32/5826_2.png) [@peerreynders](https://forum.elixirforum.com/u/peerreynders)\
**Post date:** [February 7, 2018, 2:45pm UTC](https://forum.elixirforum.com/t/genserver-fail-safe-queue/12321/5 "2018-02-07T14:45:32Z")

</div>

[Don’t Lose Your ets Tables](http://steve.vinoski.net/blog/2011/03/23/dont-lose-your-ets-tables/) explains the principles that `Immortal.ETSTableManager` is based on.

---

<div class="post-metadata">

**Author:** ![quatermain](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/quatermain/32/21781_2.png) [@quatermain](https://forum.elixirforum.com/u/quatermain)\
**Post date:** [February 9, 2018, 9:23am UTC](https://forum.elixirforum.com/t/genserver-fail-safe-queue/12321/6 "2018-02-09T09:23:56Z")

</div>

Thanks for answers, I will look on it deeply. I found this articles and it looks very promising. What do you think?  
[https://blog.emerleite.com/using-elixir-genstage-to-track-video-watch-progress-9b114786c604](https://blog.emerleite.com/using-elixir-genstage-to-track-video-watch-progress-9b114786c604)  
[https://blog.emerleite.com/elixir-video-user-profile-service-for-the-olympics-application-teardown-56ac3e103d1a](https://blog.emerleite.com/elixir-video-user-profile-service-for-the-olympics-application-teardown-56ac3e103d1a)

GenServers looks very nice for me. They are just upgrade of GenServers.

---

<div class="post-metadata">

**Author:** ![idi527](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@idi527](https://forum.elixirforum.com/u/idi527)\
**Post date:** [February 9, 2018, 10:05am UTC](https://forum.elixirforum.com/t/genserver-fail-safe-queue/12321/7 "2018-02-09T10:05:33Z")

</div>

> GenServers looks very nice for me.

Do you mean GenStage? Anyway, so far I have only found GenStage to be useful when interfacing with the outside world, when I need to put some back-pressure on the inside. It seems to be exactly what they describe in the first article that you’ve linked. They use it so as not to overload their database.

> They are just upgrade of GenServers.

So GenStage is not an upgrade to GenServers, it solves a more specific problem, I think.

---

<div class="post-metadata">

**Author:** ![quatermain](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/quatermain/32/21781_2.png) [@quatermain](https://forum.elixirforum.com/u/quatermain)\
**Post date:** [February 9, 2018, 11:07am UTC](https://forum.elixirforum.com/t/genserver-fail-safe-queue/12321/8 "2018-02-09T11:07:45Z")

</div>

yes, I mean GenStage.

> They are just upgrade of GenServers.

Yes, it solves more specific problems but technically it’s still GenServer. From their doc:

> GenStage is implemented on top of a GenServer with a few additions.

> **[GenStage — gen\_stage v1.3.2](https://gen-stage.hexdocs.pm/GenStage.html)**

I have problems with a lot of http requests which creates even more events(jobs) and some are asynch and some synch and I can not lost anyone of course. Their solution looks like also solution for me.

---

<div class="post-metadata">

**Author:** ![idi527](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/i/85e7bf/32.png) [@idi527](https://forum.elixirforum.com/u/idi527)\
**Post date:** [February 9, 2018, 12:07pm UTC](https://forum.elixirforum.com/t/genserver-fail-safe-queue/12321/9 "2018-02-09T12:07:52Z")

</div>

Have you considered using pools of workers with something like poolboy? For saving your state I think you might trap exits in your genserver and then `terminate` callback will be called even for abnormal exits.

---

<div class="post-metadata">

**Author:** ![quatermain](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/quatermain/32/21781_2.png) [@quatermain](https://forum.elixirforum.com/u/quatermain)\
**Post date:** [February 9, 2018, 12:49pm UTC](https://forum.elixirforum.com/t/genserver-fail-safe-queue/12321/10 "2018-02-09T12:49:56Z")

</div>

I will try

---

<div class="post-metadata">

**Author:** ![Azolo](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/azolo/32/2189_2.png) [@Azolo](https://forum.elixirforum.com/u/Azolo)\
**Post date:** [February 9, 2018, 4:35pm UTC](https://forum.elixirforum.com/t/genserver-fail-safe-queue/12321/11 "2018-02-09T16:35:08Z")

</div>

> [@idi527](#):
>
> For saving your state I think you might trap exits in your genserver and then `terminate` callback will be called even for abnormal exits.

I tried this at some point, and it worked except in the cases it didn’t. Which in reality were my fault, but it would have taken some major refactoring to make it work. In other words, from my personal experience I found that this method actually caused some weird code coupling issues.

Now I would recommend a separate tracker that requires a completion acknowledgement along with a job timeout. When a job is finished then the worker acknowledges the completion and it’s removed from the tracker; when the job had a timeout, it is purged from the tracker and re-queued. This way you don’t have to keep track of the failure state or its progress through whatever job stages you may have.

If your jobs need to be ordered or aren’t idempotent then this won’t work and I wish the best of luck to you.
