Any calls to Heroku redis causes flask app to timeout

290 Views Asked by At

I've been developing a web app for the usage of computers and I'm attempting to store some information about active computers on redis. I'm hosting my Flask web app on Heroku and using Heroku redis.

When I'm testing on my local machine using the Heroku Redis URL, there are no problems and everything seems to be fine.

The problem I'm having is when I push and host on heroku. As soon as there is any interaction with the redis database the workers give a H12 web request timeout and none of my pages will load.

at=error code=H12 desc="Request timeout" method=GET path="/" host=my_app ... dyno=web.1 connect=0ms service=30000ms status=503 bytes=0 protocol=https

At first I thought it was a problem with connecting to the redis database, but on launch I can see in the logs that it connects without a problem. I also thought it might be because I'm initializing some lists and hashes, but after removing those and simply doing a "set" on launch caused the same issue. The weird thing is that if I remove any gets, sets, etc. but I leave the connection to the redis database, everything runs fine. However, as soon as I try a simple get or set I get the aforementioned error. I've provided my code for initializing the redis instance below and note that I am using a blueprint model as shown out below. (I've also tried moving the initialization of the redis instance to the init file but it still gives the same results).

- app
- application
  - __init__
  - models 

models.py

import os
from urllib.parse import urlparse
import redis

url = urlparse(os.environ.get("REDIS_URL"))
my_redis = redis.Redis(host=url.hostname, port=url.port, username=url.username,
                       password=url.password, ssl=True, ssl_cert_reqs=None)
1

There are 1 best solutions below

0
On

The issue is that you'll need to change REDIS_URL to REDIS_TLS_URL or set ssl=False. It's better to set REDIS_TLS_URL for security reason though.

If you'll need to work both on the Heroku env and your local env, set a persistent Heroku env var such as

heroku config:set HEROKU=1

Then on you app.py add

if 'HEROKU' in os.environ:
    url = urlparse(os.environ.get("REDIS_TLS_URL"))
else:
    url = urlparse(os.environ.get("REDIS_URL"))