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

LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
hpopp
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
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

We're in Beta

About us Mission Statement