R
RailwayAshoniA

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:
@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}
)
@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}
)
GitHub
GitHub - AlexShonia/e-commerce
Contribute to AlexShonia/e-commerce development by creating an account on GitHub.
P
Percy14d ago
Project ID: 59308ce1-f27b-4a5b-aa0b-10ac909d0bba
A
AshoniA14d ago
59308ce1-f27b-4a5b-aa0b-10ac909d0bba
B
Brody14d ago
how are you serving your frontend?
A
AshoniA14d ago
npm run build and python manage.py collectstatic
B
Brody14d ago
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 -
backend
manage.py
...
frontend
package.json
...
backend
manage.py
...
frontend
package.json
...
A
AshoniA14d ago
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?
B
Brody14d ago
there is many things to change, serving the frontend from a django backend is far from ideal
A
AshoniA14d ago
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?
B
Brody14d ago
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
A
AshoniA14d ago
Alright I'll start seperating the two
B
Brody14d ago
awesome, that means also removing anything from the backend code that is meant to serve the frontend
A
AshoniA14d ago
Alright, I'll try to do that I'm guessing staticfiles directory and the code connected to it will be removed on the backend
B
Brody14d ago
yeah all the frontend assets will be served by the frontend service
A
AshoniA14d ago
what about images that users can upload? can backend store them in the public folder on the frontend?
B
Brody14d ago
yeah that's backend stuff
A
AshoniA14d ago
I see idk what happened, but my github is not available anymore Account was suspended
B
Brody14d ago
well that's not ideal
A
AshoniA14d ago
so weird wow yeah not really lmao it almost feels like you hacked me but that's just the coincidence :D
B
Brody14d ago
haha
A
AshoniA14d ago
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
B
Brody14d ago
sounds good, feel free to ping me when you have that sorted!
A
AshoniA14d ago
Alright, thanks for the help so far!
B
Brody14d ago
no prob
A
AshoniA14d ago
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/
B
Brody14d ago
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
A
AshoniA14d ago
did it
B
Brody14d ago
make sure you remove the frontend's dist folder from github and add it to your .gitignore
A
AshoniA14d ago
done
A
AshoniA14d ago
No description
A
AshoniA14d ago
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
B
Brody14d ago
well hold on there can i ask you to switch to postgres instead of sqlite?
A
AshoniA14d ago
sure that would be better
B
Brody14d ago
yes it really would
A
AshoniA14d ago
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"),
}
}
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"),
}
}
B
Brody14d ago
looks good to me, do you have all those variables set in your service variables for the backend service?
A
AshoniA14d ago
not on backend, they are set on postgres
No description
A
AshoniA14d ago
should I set them there too? alright I set them on backend service as well
B
Brody14d ago
click the eye icon on the backend's service variables to make sure they show correctly
A
AshoniA14d ago
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
No description
A
AshoniA14d ago
so, I'm changing these variables and still they show up as empty strings
No description
A
AshoniA14d ago
when I click edit they appear again maybe just a visual bug
B
Brody14d ago
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
PGDATABASE=${{Postgres.PGDATABASE}}
PGUSER=${{Postgres.PGUSER}}
PGPASSWORD=${{Postgres.PGPASSWORD}}
PGHOST=${{Postgres.PGHOST}}
PGPORT=${{Postgres.PGPORT}}
PGDATABASE=${{Postgres.PGDATABASE}}
PGUSER=${{Postgres.PGUSER}}
PGPASSWORD=${{Postgres.PGPASSWORD}}
PGHOST=${{Postgres.PGHOST}}
PGPORT=${{Postgres.PGPORT}}
A
AshoniA14d ago
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
A
AshoniA14d ago
No description
A
AshoniA14d ago
oh nvm they are differnt
B
Brody14d ago
please use the variables i provided
A
AshoniA14d ago
yep doing it now
A
AshoniA14d ago
No description
A
AshoniA14d ago
alright should be it
B
Brody14d ago
is this the raw editor of the postgres service?
A
AshoniA14d ago
it's of the backend service
B
Brody14d ago
why do you have every variable in there that the postgres service has?
A
AshoniA14d ago
I copied everything from there😅 so should I just use only the ones you provided?
B
Brody14d ago
yes
A
AshoniA14d ago
No description
B
Brody14d ago
okay now make sure they display correctly
A
AshoniA14d ago
pghost and pgport are empty strings still
No description
A
AshoniA14d ago
others appear fine
B
Brody14d ago
you would need to save the changes with alt + shift + enter
A
AshoniA14d ago
alright, eveyrhting appears now
B
Brody14d ago
okay can i see a screenshot of your railway project now please
A
AshoniA14d ago
No description
A
AshoniA14d ago
if this is what you meant, here it is
B
Brody14d ago
yeah thats good okay code changes time
A
AshoniA14d ago
im ready :)
A
AshoniA14d ago
No description
A
AshoniA14d ago
haha got my answer on that one
B
Brody14d ago
lmao 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
}
}
{
"$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
}
}
and the root directory should be set to /backend
A
AshoniA14d ago
did it
B
Brody14d ago
okay now let me know if the backend fails
A
AshoniA14d ago
No description
A
AshoniA14d ago
it's successful
A
AshoniA14d ago
No description
A
AshoniA14d ago
it crashed
SECRET_KEY = os.environ.get("SECRET_KEY")
SECRET_KEY = os.environ.get("SECRET_KEY")
I get secret key from environment variables and I dont have it set anymore guess I'll just set it again
B
Brody14d ago
yeah just set it as a service variable
A
AshoniA14d ago
alright it's up
A
AshoniA14d ago
No description
A
AshoniA14d ago
these is the log output though
B
Brody14d ago
ive updated this
A
AshoniA14d ago
No description
A
AshoniA14d ago
alright it's active now
B
Brody14d ago
awsome
A
AshoniA14d ago
nice
B
Brody14d ago
on to the frontend
A
AshoniA14d ago
let's go
B
Brody14d ago
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
A
AshoniA14d ago
alright did it
B
Brody14d ago
and let me know if something with the frontend fails
A
AshoniA14d ago
No description
A
AshoniA14d ago
service is active these are the logs
B
Brody14d ago
okay now you will have to update your frontend code to call the backend domain
A
AshoniA14d ago
alright I think I know what to do
B
Brody14d ago
awsome
A
AshoniA14d ago
alright the server is working! you're awesome man, can't thank you enough
B
Brody14d ago
nothing is too slow?
A
AshoniA14d ago
have to test it out yet after I add stuff to db I'll let you know !
B
Brody14d ago
sounds good
A
AshoniA14d ago
hmm the problem remains still, must be something with my code then. regardless it was great to improve the architecture and conect to postgres
B
Brody14d ago
do you have some middleware that's slowing things down?
A
AshoniA14d ago
not that I know of. only the pagination is the problem it seems. registration and login is pretty fast as well as other operations
B
Brody14d ago
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
A
AshoniA14d ago
definitely worth a while, and thank you again!
B
Brody14d ago
happy to help where I can, and definitely let me know when you find out what code is responsible for the slow down
A
AshoniA13d ago
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.
B
Brody13d ago
how long is too slow again? you said 60 seconds right?
A
AshoniA13d ago
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
B
Brody13d 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
A
AshoniA13d ago
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
B
Brody13d ago
just loading the content for that page took ~17 seconds, distance is not the main issue you are facing
A
AshoniA13d ago
damn, okay. in one way it's good news and means I can fix this
B
Brody13d ago
indeed
A
AshoniA13d ago
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")
if(query):
products = Product.objects.filter(name__icontains=query).order_by("_id")
else:
products = Product.objects.all().order_by("_id")
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
B
Brody13d ago
interesting
A
AshoniA12d ago
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
B
Brody12d ago
I was on chrome
A
AshoniA11d ago
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
B
Brody11d ago
load times are good for me now too
A
AshoniA11d ago
nice
B
Brody11d ago
job done
Want results from more Discord servers?
Add your server
More Posts
Down only in DubaiHi Railway, Starting around 7.30 PM UTC +4, we can’t access our production instances. The URL is zeApplying chance is persistingI have this popup constantly appearing, even though nothing is being updated / changed. Anyway to geMySQL Memory UsageHey there, we just migrated to Railway and in the last 24h I saw that the memory usage is consistentRailway crashed after errors on trycatch?Is there any parameter to change in Railway so that it doesn't crash? because I'm using a trycatch tRailway having trouble connecting to local db upon deploymentHi. I recently deployed my node.js app on Railway and found out that during startup of my node.js apRedeploying service doesnt use updated Source Repo URLHi im new to railway so maybe this is a misunderstanding. I have started a project using a template rust deploy uses 10x more ram than local devhey guys, i have a rust api that is using 4-8gb of ram suddenly, now for rust that's unheard of conUnique variables for replicasHow can I assign a different token environment variable to each replica? Or have the replicas chooseDelete Strapi build and .cache foldersHello, how can I get a list of files in my railway project? I tried the command `railway run ls` buUnable to add repository to service that I'm a collaborator onI'm a collaborator on a github repo, and I'm trying to deploy a service on railway using that repo. Deploy angular project on RailwayI have angular project which I can build and run on my local machine perfectly. here is the link of Issue when deploying Typebot templateLooks like something went wrong and the train derailed... Hello, I have tried deploying both TypeboBun + Next JS 14 + Dockerfile run build failed timed outHi! The run build crashes due to timeout, please look, what's wrong with this? Service ID 9783c8b7-cExtremely expensive by previous standardsI've been using Railway for more than a year and a half, and for the first time I've seen such nonseI am charged twice for the same BDI have double database since the migration, and I am being billed twice as much, I have several dataRailway projects suddenly failing with new "Cannot find type definition file for 'mime'" ErrorSeems that something changed in the Railway infra because even 'redeploying' previously working projWebdriver Exception:Process unexpectedly closed with status: 1I am using selenium 4.19.0, and Firefox as the webdriver with headless configuration. My code works I need help restoring the postgres dataI accidentally used prisma migrate and overwrote the data in my production database. Can someone fPayload CMS App Doesn't Appear to work correctlyI've got an app that works correctly localhost, but when I deploy to Railway, the only thing I get iTeam project, but cost x2What the different between free plan and pro plan? bcs my company have upgrade pro plan, but it cos