Server Response too slow

AnonymousTRIAL

a year 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

AnonymousTRIAL

a year ago

59308ce1-f27b-4a5b-aa0b-10ac909d0bba


a year ago

how are you serving your frontend?


AnonymousTRIAL

a year ago

npm run build and python manage.py collectstatic


a year ago

this is never a good idea


a year ago

please separate this repo out into two folders -

backend
  manage.py
  ...
frontend
  package.json
  ...

AnonymousTRIAL

a year 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?


a year ago

there is many things to change, serving the frontend from a django backend is far from ideal


AnonymousTRIAL

a year 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?


a year ago

yes your frontend will be served by another railway service, that is standard practice


a year ago

finding more information about this means just following along while I guide you


AnonymousTRIAL

a year ago

Alright I'll start seperating the two


a year ago

awesome, that means also removing anything from the backend code that is meant to serve the frontend


AnonymousTRIAL

a year ago

Alright, I'll try to do that


AnonymousTRIAL

a year ago

I'm guessing staticfiles directory and the code connected to it will be removed


AnonymousTRIAL

a year ago

on the backend


a year ago

yeah all the frontend assets will be served by the frontend service


AnonymousTRIAL

a year ago

what about images that users can upload? can backend store them in the public folder on the frontend?


a year ago

yeah that's backend stuff


AnonymousTRIAL

a year ago

I see


AnonymousTRIAL

a year ago

idk what happened, but my github is not available anymore


AnonymousTRIAL

a year ago

Account was suspended


a year ago

well that's not ideal


AnonymousTRIAL

a year ago

so weird wow


AnonymousTRIAL

a year ago

yeah not really lmao


AnonymousTRIAL

a year ago

it almost feels like you hacked me but that's just the coincidence


AnonymousTRIAL

a year ago

:D


a year ago

haha


AnonymousTRIAL

a year 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


AnonymousTRIAL

a year ago

oh well will talk to support


a year ago

sounds good, feel free to ping me when you have that sorted!


AnonymousTRIAL

a year ago

Alright, thanks for the help so far!


a year ago

no prob


AnonymousTRIAL

a year 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/


a year ago

please remove all code from the backend that serves the frontend
like this -


AnonymousTRIAL

a year ago

did it


a year ago

make sure you remove the frontend's dist folder from github and add it to your .gitignore


AnonymousTRIAL

a year ago

done


AnonymousTRIAL

a year ago

1224786923264938000


AnonymousTRIAL

a year ago

alright uploaded this repo twice


AnonymousTRIAL

a year ago

in one settings I have backend/ as root


AnonymousTRIAL

a year ago

and in another frontend/


AnonymousTRIAL

a year ago

I'm guessing this is how I'd deploy front and back from same repository seperately


a year ago

well hold on there


a year ago

can i ask you to switch to postgres instead of sqlite?


AnonymousTRIAL

a year ago

sure


AnonymousTRIAL

a year ago

that would be better


a year ago

yes it really would


AnonymousTRIAL

a year 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"),
    }
}

a year ago

looks good to me, do you have all those variables set in your service variables for the backend service?


AnonymousTRIAL

a year ago

not on backend, they are set on postgres

1224792394533441500


AnonymousTRIAL

a year ago

should I set them there too?


AnonymousTRIAL

a year ago

alright I set them on backend service as well


a year ago

click the eye icon on the backend's service variables to make sure they show correctly


AnonymousTRIAL

a year 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

1224798725457842200


AnonymousTRIAL

a year ago

so, I'm changing these variables and still they show up as empty strings

1224799131269337000


AnonymousTRIAL

a year ago

when I click edit they appear again


AnonymousTRIAL

a year ago

maybe just a visual bug


a year ago

please slow down, there are changes we need to make still, you wont be able to just give railway your repo just yet


a year 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}}

AnonymousTRIAL

a year ago

sorry, I'm just trying to fix stuff on my own. I'll wait for you then


AnonymousTRIAL

a year ago

alright these are set now on my backend service


AnonymousTRIAL

a year ago

1224801684459622400


AnonymousTRIAL

a year ago

oh nvm they are differnt


a year ago

please use the variables i provided


AnonymousTRIAL

a year ago

yep doing it now


AnonymousTRIAL

a year ago

1224802176409538600


AnonymousTRIAL

a year ago

alright should be it


a year ago

is this the raw editor of the postgres service?


AnonymousTRIAL

a year ago

it's of the backend service


a year ago

why do you have every variable in there that the postgres service has?


AnonymousTRIAL

a year ago

I copied everything from there😅


AnonymousTRIAL

a year ago

so should I just use only the ones you provided?


a year ago

yes


AnonymousTRIAL

a year ago

1224802769748099300


a year ago

okay now make sure they display correctly


AnonymousTRIAL

a year ago

pghost and pgport are empty strings still

1224803632558637300


AnonymousTRIAL

a year ago

others appear fine


a year ago

you would need to save the changes with alt + shift + enter


AnonymousTRIAL

a year ago

alright, eveyrhting appears now


a year ago

okay can i see a screenshot of your railway project now please


AnonymousTRIAL

a year ago

1224805435635270000


AnonymousTRIAL

a year ago

if this is what you meant, here it is


a year ago

yeah thats good


a year ago

okay code changes time


AnonymousTRIAL

a year ago

im ready :)


AnonymousTRIAL

a year ago

1224806345040068600


AnonymousTRIAL

a year ago

haha


AnonymousTRIAL

a year ago

got my answer on that one


a year ago

lmao


a year 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
    }
}

a year ago

and the root directory should be set to /backend


AnonymousTRIAL

a year ago

did it


a year ago

okay now let me know if the backend fails


AnonymousTRIAL

a year ago

1224807490042990600


AnonymousTRIAL

a year ago

it's successful


AnonymousTRIAL

a year ago

1224808031305339100


AnonymousTRIAL

a year ago

it crashed


AnonymousTRIAL

a year ago

SECRET_KEY = os.environ.get("SECRET_KEY")

I get secret key from environment variables and I dont have it set anymore


AnonymousTRIAL

a year ago

guess I'll just set it again


a year ago

yeah just set it as a service variable


AnonymousTRIAL

a year ago

alright it's up


AnonymousTRIAL

a year ago

1224810532771729400


AnonymousTRIAL

a year ago

these is the log output though


a year ago

ive updated this


AnonymousTRIAL

a year ago

1224811434844618800


AnonymousTRIAL

a year ago

alright it's active now


a year ago

awsome


AnonymousTRIAL

a year ago

nice


a year ago

on to the frontend


AnonymousTRIAL

a year ago

let's go


a year 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


AnonymousTRIAL

a year ago

alright did it


a year ago

and let me know if something with the frontend fails


AnonymousTRIAL

a year ago

1224812966881526000


AnonymousTRIAL

a year ago

service is active


AnonymousTRIAL

a year ago

these are the logs


a year ago

okay now you will have to update your frontend code to call the backend domain


AnonymousTRIAL

a year ago

alright I think I know what to do


a year ago

awsome


AnonymousTRIAL

a year ago

alright the server is working!


AnonymousTRIAL

a year ago

you're awesome man, can't thank you enough


a year ago

nothing is too slow?


AnonymousTRIAL

a year ago

have to test it out yet after I add stuff to db


AnonymousTRIAL

a year ago

I'll let you know !


a year ago

sounds good


AnonymousTRIAL

a year 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


a year ago

do you have some middleware that's slowing things down?


AnonymousTRIAL

a year 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


a year ago

well to be honest, I wasn't fully expecting these changes to fix anything


a year 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


AnonymousTRIAL

a year ago

definitely worth a while, and thank you again!


a year ago

happy to help where I can, and definitely let me know when you find out what code is responsible for the slow down


AnonymousTRIAL

a year ago

I'll let you know!


AnonymousTRIAL

a year ago

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.


a year ago

how long is too slow again? you said 60 seconds right?


AnonymousTRIAL

a year ago

sometime it's quite fast. 1-3 seconds. and sometimes it's like 30 seconds and worker times out.


AnonymousTRIAL

a year ago

around 2 out of 5 times the worker times out, and thats a lot


a year 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


AnonymousTRIAL

a year 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.



AnonymousTRIAL

a year ago

sure, just pagination


AnonymousTRIAL

a year ago

from page to page


AnonymousTRIAL

a year ago

is the problematic part


AnonymousTRIAL

a year ago

other parts too but this is most problematic


a year ago

just loading the content for that page took ~17 seconds, distance is not the main issue you are facing


AnonymousTRIAL

a year ago

damn, okay. in one way it's good news and means I can fix this


a year ago

indeed


AnonymousTRIAL

a year 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")

AnonymousTRIAL

a year ago

so querying data from db is the problem


AnonymousTRIAL

a year ago

I tested this by printing something first and then printing products and products got printed much later


a year ago

interesting


AnonymousTRIAL

a year 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.


AnonymousTRIAL

a year ago

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.


AnonymousTRIAL

a year ago

wait nvm, I thought debug mode meant that I wouldn't even be able to access /admin page


AnonymousTRIAL

a year ago

but I can't login there so I guess debug is indeed false


a year ago

I was on chrome


AnonymousTRIAL

a year 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


a year ago

load times are good for me now too


AnonymousTRIAL

a year ago

nice


a year ago

job done


AnonymousTRIAL

a year ago

I have the same problem, and I am on chrome


a year ago

please open your own help thread


Server Response too slow - Railway Help Station