R
Railway•9mo ago
oktonoid

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?
Solution:
i have this service variable
REDIS_PRIVATE_URL=${{Redis.REDIS_PRIVATE_URL}}
REDIS_PRIVATE_URL=${{Redis.REDIS_PRIVATE_URL}}
and this code...
Jump to solution
50 Replies
Percy
Percy•9mo ago
Project ID: 20f06db8-c92f-453a-8720-e5ca0547115b
oktonoid
oktonoid•9mo ago
20f06db8-c92f-453a-8720-e5ca0547115b
Brody
Brody•9mo ago
what redis client are you using?
oktonoid
oktonoid•9mo ago
redis-py
Brody
Brody•9mo ago
on your railway service, do you use reference variables?
oktonoid
oktonoid•9mo ago
No, I just copy pasted the variable values into my python file
Brody
Brody•9mo ago
oh please don't do that, you never wanna hard code these kinds of things do you have any redis service variables setup?
oktonoid
oktonoid•9mo ago
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
Brody
Brody•9mo ago
fair enough, I'll try to put a redis-py test together that uses python 3.11. I'll get back to you!
oktonoid
oktonoid•9mo ago
Thank you! Very much appreciate it
Brody
Brody•9mo ago
hey @oktonoid - i should have asked sooner, but are you using this package? https://pypi.org/project/py-redis/ its from 2021 though
oktonoid
oktonoid•9mo ago
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/
oktonoid
oktonoid•9mo ago
Yes that's it Sorry I might have been unclear on that pip install redis redis==5.0.0
Brody
Brody•9mo ago
thanks, will definitely get back to you on this
oktonoid
oktonoid•9mo ago
Merci, I'll keep trying too on my end I can connect to it from redis-cli and RedisInsight using the proxy
Brody
Brody•9mo ago
redis is in the same project as your app right?
oktonoid
oktonoid•9mo ago
Just not from my python application, neither public nor private yes it is
Brody
Brody•9mo ago
are you deploying with a dockerfile?
oktonoid
oktonoid•9mo ago
No, from github
Brody
Brody•9mo ago
gotcha no dockerfile
oktonoid
oktonoid•9mo ago
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
oktonoid
oktonoid•9mo ago
Solution
Brody
Brody•9mo ago
i have this service variable
REDIS_PRIVATE_URL=${{Redis.REDIS_PRIVATE_URL}}
REDIS_PRIVATE_URL=${{Redis.REDIS_PRIVATE_URL}}
and this code
import redis
import os
import time

time.sleep(3)

r = redis.from_url(os.environ['REDIS_PRIVATE_URL'])

print(r.ping())
import redis
import os
import time

time.sleep(3)

r = redis.from_url(os.environ['REDIS_PRIVATE_URL'])

print(r.ping())
and i get a True print, meaning the connection worked python 3.11 and redis==5.0.0
oktonoid
oktonoid•9mo ago
Can you print what exactly os.environ['REDIS_PRIVATE_URL'] is pls?
Brody
Brody•9mo ago
redis://default:[email protected]:6379
oktonoid
oktonoid•9mo ago
.. I mean, without compromizing privacy is it with or without the :port?
Brody
Brody•9mo ago
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
Brody
Brody•9mo ago
No description
oktonoid
oktonoid•9mo ago
You don't set the password? .. maybe that's why
Brody
Brody•9mo ago
the password is in the url, i use from_url
oktonoid
oktonoid•9mo ago
Ok, this worked
Brody
Brody•9mo ago
awsome!
oktonoid
oktonoid•9mo ago
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 issue
Brody
Brody•9mo ago
i'd still stick with the url, you can do this REDIS_PRIVATE_URL=${{Redis.REDIS_PRIVATE_URL}}?decode_responses=True
oktonoid
oktonoid•9mo ago
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
Brody
Brody•9mo ago
then you should be fine, Application failed to respond is a very common error users make fastapi right?
oktonoid
oktonoid•9mo ago
It's on an off, I've gotten responses a minute ago from the same build, now it's Failed to respond
Brody
Brody•9mo ago
link?
oktonoid
oktonoid•9mo ago
Yes fastapi, nothing in the logs .. other than the startup prints which are all good so the service is running it seems
Brody
Brody•9mo ago
lets see the prints please
oktonoid
oktonoid•9mo ago
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 again
oktonoid
oktonoid•9mo ago
Ok 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)
Brody
Brody•9mo ago
good idea! ill see to that
oktonoid
oktonoid•9mo ago
Last question: do you recommend leaving the 3 sec sleep in there? (The log-downloader is super useful too)
Brody
Brody•9mo ago
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
oktonoid
oktonoid•9mo ago
Ok makes sense Super. Thank you again!
Brody
Brody•9mo ago
no problem!!
Brody
Brody•9mo ago
but that uses hard coded values, id prefer if you didnt show that in an example thanks for understanding
Want results from more Discord servers?
Add your server