Connecting python to redis database
I am unable to connect my redis database from my python application (that's running a fastapi server, using redis-py)
I'm using
import redis
r = redis.Redis(
host=HOST,
port=PORT,
password=PASSWORD,
ssl=True,
charset="utf-8",
decode_responses=True
)
and I tried all combinations of host=, with and without port= (of course password from the variables tab)
o) monorail.proxy.rlwy.net
o) REDIS_PRIVATE_URL, i.e. 'redis://default:[email protected]:6379'
o) REDIS_URL, i.e. 'redis://default:[email protected]:31442'
o) 'redis.railway.internal'
o) 'redis' (since the settings says 'You can also simply call me redis')
but nothing's working.
The deployment logs print:
redis://default:[email protected]:6379:6379. Name or service not known.
redis://default:[email protected]:6379. Name or service not known.
redis://default:[email protected]:6379:31442. Name or service not known.
redis://default:[email protected]:6379:31442. Name or service not known.
redis://default:[email protected]:31442:6379. Name or service not known.
redis://default:[email protected]:31442. Name or service not known.
What's the correct settings here for host and port to establish the connection?50 Replies
Project ID:
20f06db8-c92f-453a-8720-e5ca0547115b
20f06db8-c92f-453a-8720-e5ca0547115b
what redis client are you using?
redis-py
on your railway service, do you use reference variables?
No, I just copy pasted the variable values into my python file
oh please don't do that, you never wanna hard code these kinds of things
do you have any redis service variables setup?
I understand that, I'm just testing the service with dummy data to see if it's useable for my business
.. trying to avoid any confounding factors with env variables. Once I got it working with the pasted stings I'll move to env variables
Only env I set is NIXPACKS_PYTHON_VERSION = 3.11.4
fair enough, I'll try to put a redis-py test together that uses python 3.11. I'll get back to you!
Thank you! Very much appreciate it
hey @oktonoid - i should have asked sooner, but are you using this package? https://pypi.org/project/py-redis/
its from 2021 though
hi @Brody this one:
https://github.com/redis/redis-py
It's the one that's officially endorsed by redis:
https://redis.io/docs/connect/clients/python/
Yes that's it
Sorry I might have been unclear on that
pip install redis
redis==5.0.0
thanks, will definitely get back to you on this
Merci, I'll keep trying too on my end
I can connect to it from redis-cli and RedisInsight using the proxy
redis is in the same project as your app right?
Just not from my python application, neither public nor private
yes it is
are you deploying with a dockerfile?
No, from github
gotcha no dockerfile
And the python application does launch, it just crashes whenever redis tries to connect. With the private urls I get the error messages above, with the public one it just gets stuck or times out without error message, I just get "Application failed to respond" if I try it through the browser
.. I also tried with an 8 second sleep before connecting because I read somewhere on discord that might help .. but it didn't either
Solution
i have this service variable
and this code
and i get a
True
print, meaning the connection worked
python 3.11 and redis==5.0.0Can you print what exactly os.environ['REDIS_PRIVATE_URL'] is pls?
redis://default:[email protected]:6379
.. I mean, without compromizing privacy
is it with or without the :port?
i spun the db up for this test and will be tearing it down later, so no worries there
with, just as it comes when you deploy a redis database, i only used the auto complete dropdown on the service to add it
You don't set the password?
.. maybe that's why
the password is in the url, i use
from_url
Ok, this worked
awsome!
This one also works:
r = redis.Redis(
host = 'redis.railway.internal',
port=6379,
password='todo',
charset="utf-8",
decode_responses=True
)
which is nice because it allows decode_responses and all the other options
I get the redis ping and get/set, I just can't reach the app through the public networking link, getting "Application failed to respond" but that's not a redis issuei'd still stick with the url, you can do this
REDIS_PRIVATE_URL=${{Redis.REDIS_PRIVATE_URL}}?decode_responses=True
Do you happen to know if there are service outages? I see the message "Railway-hosted applications may be inaccessible for users connecting from Europe. We have identified the incident.". I'm in europe but going through a US vpn
then you should be fine, Application failed to respond is a very common error users make
fastapi right?
It's on an off, I've gotten responses a minute ago from the same build, now it's Failed to respond
link?
Yes fastapi, nothing in the logs
.. other than the startup prints which are all good so the service is running it seems
lets see the prints please
Build:
...
Pushing [==================================================>] 173.8MB 90558e9c8238
Pushed 90558e9c8238
a06bf1d5-b9fd-4876-8281-ac96a7b9196c: digest: sha256:5ef7342a76408453a54eb0b9e64f5a30f96d239ac582455063cf351e983787dd size: 2418
Publish time: 11.32 seconds
Deploy:
sleep done
after redis
.. I guess it failed at r.set('foo', 'bar')
Let me try againOk working 🙌 (had to disable ssl)
Thank YOU
Really appreciate it, I know you spent time on this
(Maybe you can add your python-redis connection code sample somewhere to the docs, should be a common use-case. Was mostly misunderstandings on my end though)
good idea! ill see to that
Last question: do you recommend leaving the 3 sec sleep in there? (The log-downloader is super useful too)
yes you need the 3 second sleep for now, the internal dns resolver needs about that much time for it to be ready to answer lookups
Ok makes sense
Super. Thank you again!
no problem!!
but that uses hard coded values, id prefer if you didnt show that in an example
thanks for understanding