shahryarjb

shahryarjb

Redix the connection to Redis is closed

I made mistake to use redis to store my user api JWT token, after deploying on server .
unfortunately, I have no time to replace redis with other thing and am forced to fix this on redis.

it is my redis benchmark info:

# Clients
connected_clients:28223
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0

I just store my Token on redis ad after user request on 1 day my redis has this error: Redix the connection to Redis is closed, then system is down .

How can I fix this?

this is my code to use Redis:

defmodule BankError.Extera.Redis do

  def connect_to_redis do
    case Redix.start_link() do
    {:ok, conn} ->
      Redix.pipeline(conn, [["AUTH","PASSWORD"]]) # will be changed
      {:ok, conn, :connect_to_redis}
      _ -> {:error, :connect_to_redis}
    end
	end

  def insert_or_update_into_redis(table_name, record_id, params, expire_time) do
    with {:ok, conn, :connect_to_redis} <- connect_to_redis() do
      conn |>
      Redix.pipeline([
        List.flatten(
          [
            "HMSET",
            # String.to_charlist(table_name <> record_id)
            "#{table_name}#{record_id}"
          ], BankError.map_to_single_list_with_string_key(params)
        ),
        [
          "EXPIRE",
          table_name <> record_id,
          expire_time
        ]
      ])
      {:ok, :insert_or_update_into_redis}
    end
  end

  @doc """
    show all fields of redis record
  """
  def get_all_fields_of_record_redis(table_name, record_id) do
		with {:ok, conn, :connect_to_redis} <- connect_to_redis() do
      conn
      |> Redix.pipeline!([["HGETALL",table_name <> record_id]])
    else
      n ->
        {:error, :get_all_fields_of_record_redis, n}
    end
	end

  @doc """
    show singel fields of redis record
  """
  def get_singel_field_record_of_redis(table_name, record_id, field_name) do
    with {:ok, conn, :connect_to_redis} <- connect_to_redis() do
      conn
      |> Redix.pipeline!([["HGET","#{table_name}#{record_id}", field_name]])
    else
      n ->
        {:error, :get_singel_field_record_of_redis, n}
    end
  end

  @doc """
    delete redis record
  """
  def delete_record_of_redis(table_name, record_id) do
    with {:ok, conn, :connect_to_redis} <- connect_to_redis(),
         {:ok, :get_all_fields_of_record_redis, record} <- convert_output_of_get_all_fields_of_record_redis(get_all_fields_of_record_redis(table_name, record_id)) do

          conn
          |> Redix.pipeline([["HDEL", table_name <> record_id] ++ record])
          {:ok, :delete_record_of_redis, "The record is deleted"}
    else
      n ->  n
    end
  end

  def convert_output_of_get_all_fields_of_record_redis(params) do
    case params do
      [[]] -> {:error, :get_all_fields_of_record_redis, "The data concerned doesn't exist"}
      [] -> {:error, :get_all_fields_of_record_redis, "The data concerned doesn't exist"}
      [nil] -> {:error, :get_all_fields_of_record_redis, "The data concerned doesn't exist"}
      n ->
        [record | _] = n
        {:ok, :get_all_fields_of_record_redis, record}
    end
  end

  @doc """
    delete singel field of redis record
  """
  def delete_field_of_record_redis(table_name, record_id, field_name) do

    with {:ok, conn, :connect_to_redis} <- connect_to_redis(),
         {:ok, :get_all_fields_of_record_redis, _record} <- convert_output_of_get_all_fields_of_record_redis(get_all_fields_of_record_redis(table_name, record_id)),
         {:ok, [1]} <- Redix.pipeline(conn, [["HDEL", table_name <> record_id, field_name]]) do

          {:ok, :delete_field_of_record_redis}
    else
      {:ok, [0]} -> {:error, :delete_field_of_record_redis, "The field you need doesn't exist"}
      n -> n
    end
  end

  @doc """
    get expire time of singel record
  """
  def get_expire_time_of_redis(table_name, record_id) do
    with {:ok, conn, :connect_to_redis} <- connect_to_redis(),
        {:ok, :get_expire_time_error_handler, expire_time} <- get_expire_time_error_handler(conn, table_name, record_id) do

        {:ok, :get_expire_time, expire_time}
    else
      n -> n
    end
  end

  defp get_expire_time_error_handler(conn, table_name, record_id) do
    case Redix.pipeline(conn, [["TTL",table_name <> record_id]]) do
      {:ok, [-2]} ->
        {:error, :get_expire_time_error_handler, "The data concerned doesn't exist"}

      {:ok, [expire_time]} ->
        {:ok, :get_expire_time_error_handler, expire_time}

      _ ->
      {:error, :get_expire_time_error_handler, "The data concerned doesn't exist"}
    end
  end

  @doc """
    get expire time of singel record
  """
  def update_expire_time_of_redis(table_name, record_id, expire_time) do
    with {:ok, conn, :connect_to_redis} <- connect_to_redis(),
        {:ok, :update_expire_time_of_error_handler, msg} <- update_expire_time_of_error_handler(conn, table_name, record_id, expire_time) do

        {:ok, :update_expire_time_of_redis, msg}
    else
      n -> n
    end
  end

  defp update_expire_time_of_error_handler(conn, table_name, record_id, expire_time) do
    case Redix.pipeline(conn, [["EXPIRE",table_name <> record_id, expire_time]]) do
      {:ok, [0]} ->
        {:error, :update_expire_time_of_error_handler, "The data concerned doesn't exist"}

      {:ok, [1]} ->
        {:ok, :update_expire_time_of_error_handler, "The data concerned was updated"}

      _ ->
      {:error, :update_expire_time_of_error_handler, "The data concerned doesn't exist"}
    end
  end

end

and my Supervisor on redis

defmodule BankError.Extera.RedisSup do
   @pool_size 1000

  def child_spec(_args) do
    # Specs for the Redix connections.
    children =
      for i <- 0..(@pool_size - 1) do
        Supervisor.child_spec({Redix, name: :"redix_#{i}"}, id: {Redix, i})
      end

    # Spec for the supervisor that will supervise the Redix connections.
    %{
      id: RedixSupervisor,
      type: :supervisor,
      start: {Supervisor, :start_link, [children, [strategy: :one_for_one]]}
    }
  end

for example I use Redis like this:

in my controller:

  def brands(conn, %{"category_id" => category_id, "token" => token}) do
    with {:ok, :is_token_validated?, id} <- UserQuery.is_token_validated?(token),
     {:ok, :api_user_check_mobile_and_subscribe, _user_info, _subscriber_info} <- UserQuery.api_user_check_mobile_and_subscribe(id) do

      conn
      |> put_status(200)
      |> json(%{brands: Error.show_error_brands(category_id)})
    end
  end

and Redix code:

  def is_token_validated?(token) do
    with {:ok, claims} <- verify_token(token),
         {:ok, %{id: id}} <- get_id_from_jwt_climes(claims),
         {:ok, :get_all_fields_of_record_redis, record_of_user_token} <- BankError.Extera.Redis.convert_output_of_get_all_fields_of_record_redis(BankError.Extera.Redis.get_all_fields_of_record_redis("user_token", id)),
         %{"token" => redis_token} <- BankError.list_to_map(record_of_user_token),
         {:ok, :change_password_token_on_check} <- change_password_token_on_check(token, redis_token) do

        {:ok, :is_token_validated?, id}

      else
        _ ->
          {:error, :is_token_validated?}
    end
  end

Thanks

First Post!

aseigo

aseigo

So many connected clients:

My guess is that Redix.stop/2 is never being called and the Redix processes are being linked (in connect_to_redis/0 when it calls Redix.start_link/1) to a long-lived process in your application, so all those processes just stay around and never drop their connections to the Redis server. At some point, the Redis server can not take any more client connections and connections start failing.

You could confirm this by connecting to your production server with a remote shell or observer and looking at how many Redix processes there are :slight_smile:

p.s. Any reason you aren’t using a connection pool for your Redix processes?

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement