Arsh Singhal’s Post

My client's exact words: "Arsh why is it taking so long to load users?" Me internally: It's fine, server is just... thinking. It was not fine. The server was not just thinking. I was a junior dev. Fresh out of college. Simple requirement — "Show all users on the dashboard." I thought — easy. Let me just fetch the users. @app.get("/users") async def get_users (db: AsyncSession = Depends(get_db)): result =await db.execute(select(User)) users = result.scalars().all() return users Clean. Simple. Confident. Deployed it. Sent the link to the client. Client opens the dashboard. Loading... Loading... Loading... "Why so long?" What I didn't know: The database had 84,000 users. My API was fetching all 84,000 rows. Every single time. On every single page load. Sending it all to the frontend. Which was then trying to render 84,000 table rows in the browser. The browser didn't crash. I wish it had. At least that would have been obvious. Instead it just loaded. Slowly. Painfully. Forever. Then a senior dev looked at my code. He didn't say anything for a few seconds. Then — "where is your pagination?" Pagination... Pagination... Don't fetch everything. Fetch only what the user actually sees. @app.get("/users") async def get_users ( page:int=1, page_size:int=20, db: AsyncSession = Depends(get_db) ): offset =(page -1)* page_size Instead of 84,000 rows — fetch 20. Response time: 14 seconds → 180ms. The client called me the next day. "Arsh ab toh bahut fast hai!" Same server. Same database. Same code basically. Just stopped asking for everything at once. #FastAPI #PostgreSQL #Python #BackendDevelopment #SoftwareEngineering #WebDevelopment #Database #LessonsLearned

To view or add a comment, sign in

Explore content categories