2 years ago
I have Django, React application live on railway. I'm using Hobby plan.
whenever I click through pagination buttons server is taking up to a minute to respond. I can see in the network panel that request has been sent and it takes 10 - 60 second for the response to come in.
on the localhost everything is instant. I don't know what's causing it.
Full project code : https://github.com/AlexShonia/e-commerce
pagination code on django:
@api_view(["GET"])
def getProducts(request):
query = request.query_params.get("keyword")
if query == "null" or query == None:
query = ""
products = Product.objects.filter(name__icontains=query)
page = request.query_params.get("page")
paginator = Paginator(products, 4)
try:
products = paginator.page(page)
except PageNotAnInteger:
products = paginator.page(1)
except EmptyPage:
products = paginator.page(paginator.num_pages)
serializer = ProductSerializer(products, many=True)
return Response(
{"products": serializer.data, "page": page, "pages": paginator.num_pages}
)
0 Replies
2 years ago
how are you serving your frontend?
npm run build and python manage.py collectstatic
2 years ago
this is never a good idea
2 years ago
please separate this repo out into two folders -
backend
manage.py
...
frontend
package.json
...
if I did this I'm guessing I'd have to change how I'm specifying staticfiles path here:
https://github.com/AlexShonia/e-commerce/blob/174204655011dc89bf06e2f3f34858ea2f5576e7/backend/backend/settings.py#L169
would there be anything else to change?
2 years ago
there is many things to change, serving the frontend from a django backend is far from ideal
if I didn't serve frontend from django backend would that mean I'd have to host frontend separately from backend? I'm quite new to this so I don't know the best practices. where would I find more information about this?
2 years ago
yes your frontend will be served by another railway service, that is standard practice
2 years ago
finding more information about this means just following along while I guide you
2 years ago
awesome, that means also removing anything from the backend code that is meant to serve the frontend
I'm guessing staticfiles directory and the code connected to it will be removed
2 years ago
yeah all the frontend assets will be served by the frontend service
what about images that users can upload? can backend store them in the public folder on the frontend?
2 years ago
yeah that's backend stuff
2 years ago
well that's not ideal
2 years ago
haha
this is basically my portfolio website and maybe they thought it was actualy e-comemrce website and I didnt meet rules or something is probably what happened
2 years ago
sounds good, feel free to ping me when you have that sorted!
2 years ago
no prob
Okay I'm back. idk what that was but after some time I just gained access again. weird.
anyway I spearated the repositories and deleted static files and some code connected to it
https://github.com/AlexShonia/e-commerce/commits/main/
2 years ago
please remove all code from the backend that serves the frontend
like this -
2 years ago
make sure you remove the frontend's dist folder from github and add it to your .gitignore
I'm guessing this is how I'd deploy front and back from same repository seperately
2 years ago
well hold on there
2 years ago
can i ask you to switch to postgres instead of sqlite?
2 years ago
yes it really would
okay that should be it, I made a postgres database on railway and connected it with settings.py:
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": os.environ.get("PGDATABASE"),
"USER" : os.environ.get("PGUSER"),
"PASSWORD" : os.environ.get("PGPASSWORD"),
"HOST" : os.environ.get("PGHOST"),
"PORT" : os.environ.get("PGPORT"),
}
}
2 years ago
looks good to me, do you have all those variables set in your service variables for the backend service?
2 years ago
click the eye icon on the backend's service variables to make sure they show correctly
oh yeah some are empty for some reason even though I copied raw text I'll fix that.
also Im getting this errors on frontend i the logs. I'll look into that
2 years ago
please slow down, there are changes we need to make still, you wont be able to just give railway your repo just yet
2 years ago
these are what you want to use for your app's database variables, not what you currently have
PGDATABASE=${{Postgres.PGDATABASE}}
PGUSER=${{Postgres.PGUSER}}
PGPASSWORD=${{Postgres.PGPASSWORD}}
PGHOST=${{Postgres.PGHOST}}
PGPORT=${{Postgres.PGPORT}}
2 years ago
please use the variables i provided
2 years ago
is this the raw editor of the postgres service?
2 years ago
why do you have every variable in there that the postgres service has?
2 years ago
yes
2 years ago
okay now make sure they display correctly
2 years ago
you would need to save the changes with alt + shift + enter
2 years ago
okay can i see a screenshot of your railway project now please
2 years ago
yeah thats good
2 years ago
okay code changes time
2 years ago
lmao
2 years ago
in your backend, delete the Procfile and replace it with this railway.json file
{
"$schema": "https://railway.app/railway.schema.json",
"build": {
"builder": "NIXPACKS"
},
"deploy": {
"startCommand": "python manage.py migrate && gunicorn backend.wsgi --log-file -",
"restartPolicyType": "ON_FAILURE",
"restartPolicyMaxRetries": 10
}
}
2 years ago
and the root directory should be set to /backend
2 years ago
okay now let me know if the backend fails
SECRET_KEY = os.environ.get("SECRET_KEY")
I get secret key from environment variables and I dont have it set anymore
2 years ago
yeah just set it as a service variable
2 years ago
ive updated this
2 years ago
awsome
2 years ago
on to the frontend
2 years ago
copy the nixpacks.toml and Caddyfile from this repo into your frontend folder.
you will want the root directory on the frontend service set to /frontend
2 years ago
and let me know if something with the frontend fails
2 years ago
okay now you will have to update your frontend code to call the backend domain
2 years ago
awsome
2 years ago
nothing is too slow?
2 years ago
sounds good
hmm the problem remains still, must be something with my code then. regardless it was great to improve the architecture and conect to postgres
2 years ago
do you have some middleware that's slowing things down?
not that I know of. only the pagination is the problem it seems. registration and login is pretty fast as well as other operations
2 years ago
well to be honest, I wasn't fully expecting these changes to fix anything
2 years ago
but it at least does mean it's an issue with the code and not the structure, so it was definitely worth while to do this
2 years ago
happy to help where I can, and definitely let me know when you find out what code is responsible for the slow down
I think I found out what the problem is lol. I just found out the Server is deplyed in US West Oregon and I'm in Georgia (country) so the ping should be very high. I wonder how the site is doing in US.
2 years ago
how long is too slow again? you said 60 seconds right?
sometime it's quite fast. 1-3 seconds. and sometimes it's like 30 seconds and worker times out.
2 years ago
is there a page link you could send me so i could test? im not us-west but i am far closer than you are
when I'm in localhost and I make connection to local postgres db the responses are very fast (under a second) so thats why I'm thinking it's not the code but where the site is hosted.
2 years ago
just loading the content for that page took ~17 seconds, distance is not the main issue you are facing
2 years ago
indeed
from what I've tested the problematic line is this in the django:
if(query):
products = Product.objects.filter(name__icontains=query).order_by("_id")
else:
products = Product.objects.all().order_by("_id")
I tested this by printing something first and then printing products and products got printed much later
2 years ago
interesting
After countless hours of researching I found out that the problem appears only on firefox. I tested edge and chrome and they are all fine. yes the response sometimes takes 1-3 seconds but it's only expected as server is so far away. and nothing compared to 30+ seconds. and btw it's not actually 30+ seconds it's just that gunicorn times out after 30 seconds and when I set timeout to 5 minutes the problematic responses took all 5minutes.
now this problem doesn't bother me as much now because most people use chrome anyway and this is just a portfolio website. but another problem I found is that backend django is in debug mode still even though I specified Debug= False.
wait nvm, I thought debug mode meant that I wouldn't even be able to access /admin page
2 years ago
I was on chrome
that was a different problem. I think I fixed that by usign collectstatic in start configurations? idk or maybe because I changed debug to false. but the 30+ sec response doesn't happen on chrome. and the load times are good for me
2 years ago
load times are good for me now too
2 years ago
job done
a year ago
please open your own help thread