Redis connection error when retreiving data using flask caching and valkey as backend in a dash plotly python web app

I have a dash plotly app where I have multiple pages and I am pulling data for each page from big query. As the data is huge and there are many interactive filters, I am caching the data and refreshing the data every fifteen minutes. I am using flask caching and redis and backend. The setup looks like below;

from flask_caching import Cache

Set up Flask-Caching

cache = Cache(
app.server,
config={
“CACHE_TYPE”: “redis”,
“CACHE_REDIS_URL”: redis_url,
“CACHE_DEFAULT_TIMEOUT”: 900, # Cache timeout (in seconds)
},
)

callback to run query and then cache the updated LTV By App data after every 15 minutes

@cache.cached(
timeout=900, key_prefix=constants.LTV_BY_APP_DATA_KEY_PREFIX
) # Cache for 15 minutes
def get_ltv_by_app_data():
logging.info(“Running BigQuery query to fetch LTV by App data.”)
query = f"“”
SELECT * FROM {os.getenv(constants.PROJECT_ID_ENV_VAR)}.dataset.table ORDER BY Installs DESC
“”"
try:
client = bigquery.Client()
df = execute_query(client, query)
logging.info(“Data fetched and cached successfully.”)
return df
except Exception as e:
logging.error(f"Error fetching LTV by App data: {e}")
return None

when I call this get_ltv_by_app_data(), it works and there are no issues nut when I call

callback to run query and then cache the updated Paywall Optimizer data after every 15 minutes

@cache.cached(
timeout=900, key_prefix=constants.PAYWALL_OPTIMIZER_DATA_KET_PREFIX
) # Cache for 15 minutes
def get_paywall_optimizer_data():
logging.info(“Running BigQuery query to fetch paywall optimizer data.”)
query = f"“”
SELECT *
FROM {os.getenv(constants.PROJECT_ID_ENV_VAR)}.dataset.table
“”"
try:
client = bigquery.Client()
df = execute_query(client, query)
logging.info(“Data fetched and cached successfully.”)
return df
except Exception as e:
logging.error(f"Error fetching paywall optimizer data: {e}")
return None

i get the below error trace

ERROR:flask_caching:Exception possibly due to cache backend.

May 27 03:23:00 PM

9xdnm

Traceback (most recent call last):

May 27 03:23:00 PM

9xdnm

File “/opt/render/project/src/.venv/lib/python3.11/site-packages/redis/connection.py”, line 586, in send_packed_command

May 27 03:23:00 PM

9xdnm

self._sock.sendall(item)

May 27 03:23:00 PM

9xdnm

ConnectionResetError: [Errno 104] Connection reset by peer

May 27 03:23:00 PM

9xdnm

May 27 03:23:00 PM

9xdnm

During handling of the above exception, another exception occurred:

May 27 03:23:00 PM

9xdnm

May 27 03:23:00 PM

9xdnm

Traceback (most recent call last):

May 27 03:23:00 PM

9xdnm

File “/opt/render/project/src/.venv/lib/python3.11/site-packages/flask_caching/init.py”, line 435, in decorated_function

May 27 03:23:00 PM

9xdnm

self.cache.set(

May 27 03:23:00 PM

9xdnm

File “/opt/render/project/src/.venv/lib/python3.11/site-packages/cachelib/redis.py”, line 96, in set

May 27 03:23:00 PM

9xdnm

result = self._write_client.setex(

May 27 03:23:00 PM

9xdnm

^^^^^^^^^^^^^^^^^^^^^^^^^

May 27 03:23:00 PM

9xdnm

File “/opt/render/project/src/.venv/lib/python3.11/site-packages/redis/commands/core.py”, line 2330, in setex

May 27 03:23:00 PM

9xdnm

return self.execute_command(“SETEX”, name, time, value)

May 27 03:23:00 PM

9xdnm

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

May 27 03:23:00 PM

9xdnm

File “/opt/render/project/src/.venv/lib/python3.11/site-packages/redis/client.py”, line 622, in execute_command

May 27 03:23:00 PM

9xdnm

return self._execute_command(*args, **options)

May 27 03:23:00 PM

9xdnm

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

May 27 03:23:00 PM

9xdnm

File “/opt/render/project/src/.venv/lib/python3.11/site-packages/redis/client.py”, line 633, in _execute_command

May 27 03:23:00 PM

9xdnm

return conn.retry.call_with_retry(

May 27 03:23:00 PM

9xdnm

^^^^^^^^^^^^^^^^^^^^^^^^^^^

May 27 03:23:00 PM

9xdnm

File “/opt/render/project/src/.venv/lib/python3.11/site-packages/redis/retry.py”, line 92, in call_with_retry

May 27 03:23:00 PM

9xdnm

raise error

May 27 03:23:00 PM

9xdnm

File “/opt/render/project/src/.venv/lib/python3.11/site-packages/redis/retry.py”, line 87, in call_with_retry

May 27 03:23:00 PM

9xdnm

return do()

May 27 03:23:00 PM

9xdnm

^^^^

May 27 03:23:00 PM

9xdnm

File “/opt/render/project/src/.venv/lib/python3.11/site-packages/redis/client.py”, line 634, in

May 27 03:23:00 PM

9xdnm

lambda: self._send_command_parse_response(

May 27 03:23:00 PM

9xdnm

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

May 27 03:23:00 PM

9xdnm

File “/opt/render/project/src/.venv/lib/python3.11/site-packages/redis/client.py”, line 604, in _send_command_parse_response

May 27 03:23:00 PM

9xdnm

conn.send_command(*args, **options)

May 27 03:23:00 PM

9xdnm

File “/opt/render/project/src/.venv/lib/python3.11/site-packages/redis/connection.py”, line 608, in send_command

May 27 03:23:00 PM

9xdnm

self.send_packed_command(

May 27 03:23:00 PM

9xdnm

File “/opt/render/project/src/.venv/lib/python3.11/site-packages/redis/connection.py”, line 597, in send_packed_command

May 27 03:23:00 PM

9xdnm

raise ConnectionError(f"Error {errno} while writing to socket. {errmsg}.")

May 27 03:23:00 PM

9xdnm

redis.exceptions.ConnectionError: Error 104 while writing to socket. Connection reset by peer.

NOTE: There is not anything wrong with the syntax or queries. I have changed the dataset name and table name myself. All the other pages callbacks are the same. The data is also small on paywall optimizer tab than other tabs? Why I am getting this? And how to solve this. Any help regarding solving this issue will be highly appreciated.

Thanks