Server Response too slow
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:
GitHub
GitHub - AlexShonia/e-commerce
Contribute to AlexShonia/e-commerce development by creating an account on GitHub.
121 Replies
Project ID:
59308ce1-f27b-4a5b-aa0b-10ac909d0bba
59308ce1-f27b-4a5b-aa0b-10ac909d0bba
how are you serving your frontend?
npm run build and python manage.py collectstatic
https://github.com/AlexShonia/e-commerce/blob/main/backend/backend/settings.py#L107C9-L107C59
this is never a good idea
please separate this repo out into two folders -
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?
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?
yes your frontend will be served by another railway service, that is standard practice
finding more information about this means just following along while I guide you
Alright I'll start seperating the two
awesome, that means also removing anything from the backend code that is meant to serve the frontend
Alright, I'll try to do that
I'm guessing staticfiles directory and the code connected to it will be removed
on the backend
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?
yeah that's backend stuff
I see
idk what happened, but my github is not available anymore
Account was suspended
well that's not ideal
so weird wow
yeah not really lmao
it almost feels like you hacked me but that's just the coincidence
:D
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
oh well will talk to support
sounds good, feel free to ping me when you have that sorted!
Alright, thanks for the help so far!
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/
please remove all code from the backend that serves the frontend
like this - https://github.com/AlexShonia/e-commerce/blob/main/backend/backend/settings.py#L107C9-L107C59
did it
make sure you remove the frontend's dist folder from github and add it to your .gitignore
done
alright uploaded this repo twice
in one settings I have backend/ as root
and in another frontend/
I'm guessing this is how I'd deploy front and back from same repository seperately
well hold on there
can i ask you to switch to postgres instead of sqlite?
sure
that would be better
yes it really would
okay that should be it, I made a postgres database on railway and connected it with settings.py:
looks good to me, do you have all those variables set in your service variables for the backend service?
not on backend, they are set on postgres
should I set them there too?
alright I set them on backend service as well
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
so, I'm changing these variables and still they show up as empty strings
when I click edit they appear again
maybe just a visual bug
please slow down, there are changes we need to make still, you wont be able to just give railway your repo just yet
these are what you want to use for your app's database variables, not what you currently have
sorry, I'm just trying to fix stuff on my own. I'll wait for you then
alright these are set now on my backend service
oh nvm they are differnt
please use the variables i provided
yep doing it now
alright should be it
is this the raw editor of the postgres service?
it's of the backend service
why do you have every variable in there that the postgres service has?
I copied everything from there😅
so should I just use only the ones you provided?
yes
okay now make sure they display correctly
pghost and pgport are empty strings still
others appear fine
you would need to save the changes with
alt + shift + enter
alright, eveyrhting appears now
okay can i see a screenshot of your railway project now please
if this is what you meant, here it is
yeah thats good
okay code changes time
im ready :)
haha
got my answer on that one
lmao
in your backend, delete the Procfile and replace it with this railway.json file
and the root directory should be set to
/backend
did it
okay now let me know if the backend fails
it's successful
it crashed
I get secret key from environment variables and I dont have it set anymore
guess I'll just set it again
yeah just set it as a service variable
alright it's up
these is the log output though
ive updated this
alright it's active now
awsome
nice
on to the frontend
let's go
https://github.com/brody192/vite-react-template
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
alright did it
and let me know if something with the frontend fails
service is active
these are the logs
okay now you will have to update your frontend code to call the backend domain
alright I think I know what to do
awsome
alright the server is working!
you're awesome man, can't thank you enough
nothing is too slow?
have to test it out yet after I add stuff to db
I'll let you know !
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
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
well to be honest, I wasn't fully expecting these changes to fix anything
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
definitely worth a while, and thank you again!
happy to help where I can, and definitely let me know when you find out what code is responsible for the slow down
I'll let you know!
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.
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.
around 2 out of 5 times the worker times out, and thats a lot
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.
https://e-commerce-sportshop.up.railway.app/
sure, just pagination
from page to page
is the problematic part
other parts too but this is most problematic
just loading the content for that page took ~17 seconds, distance is not the main issue you are facing
damn, okay. in one way it's good news and means I can fix this
indeed
from what I've tested the problematic line is this in the django:
so querying data from db is the problem
I tested this by printing something first and then printing products and products got printed much later
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
but I can't login there so I guess debug is indeed false
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
load times are good for me now too
nice
job done
I have the same problem, and I am on chrome
please open your own help thread