Using Redix with Genserver

I am reading through using genserver and redix try to understand how both can work together. I havent been able to find a resource that just explains the marriage between the two. Here is my use case:
I have data that is already stored in the database and my goal is to read the data and save it on a redix cache. The redix documentation is really clear on the functions I can use to set stata, start link but I am just not sure where to marry the two. This should be done via the genserver so that I can ensure fault tolerance. Below is what I have so far
A module database that uses genserver and has a set function that has a handle cast that sets a user to a key in redix. Within the handle_cast I am calling the redix

defmodule Database
  use Genserver
      .
      .
      .
  def set({pid, key, value}) do
    GenServer.cast(pid, {:set, key, value})
  end

  def handle_cast({:set, key, value}, _from, state) do
    Redix.command(state, ["SET", key, value])
    {:no_reply, }
  end 
      .
      .
      .
end

I know that redix has a start link function and my genserver Database module also has a start link. When and where do I use either of them?

Why do you need a genserver for fault tolerance? You shouldn’t use a genserver unless you need to keep state (and that itself should be super rare). Just use redix it’s designed to keep state for you. Presumably as an elixir package it’s already fault tolerant.

You might find a similar question in this post :slight_smile:

I don’t really think it would help.

1 Like