shahryarjb
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
Trending in Questions
Hello!
Suppose you are building workflow (order / task / payment) processing system with the following requirements:
Each workflow con...
New
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Kia ora,
We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










Showing Posts 1 to 1- Show Best Posts
- Show All Posts (oldest first)
- Show All Posts (newest first)
aseigo
So many connected clients:
My guess is that
Redix.stop/2is never being called and the Redix processes are being linked (inconnect_to_redis/0when it callsRedix.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
p.s. Any reason you aren’t using a connection pool for your Redix processes?