Choosing the Right Data Fetching Strategy

💡 One of the most common interview questions: “𝗛𝗼𝘄 𝗱𝗼 𝘆𝗼𝘂 𝗿𝗲𝗻𝗱𝗲𝗿 𝗮 𝗹𝗮𝗿𝗴𝗲 𝗹𝗶𝘀𝘁 𝗼𝗳 𝗱𝗮𝘁𝗮?” Most people immediately think of frontend optimizations like: • memoization • lazy loading But there’s a backend angle too 👇 📄 Traditional approach: Page-based pagination • send page + limit • use skip on database • return data Works well for: ✅ small to medium datasets ✅ quick implementation ⚠️ But what happens when data grows? Imagine: • 100,000 records • 100 per page → 1000 pages Now think about the UX 🤯 • endless page numbers • poor navigation • slower queries with large skips 🚀 Better approach: Cursor-based pagination Instead of page numbers, we use a cursor (usually last item id). How it works: • send last fetched item id • query next set using > (greater than)/ <(less than) condition • return next batch ✨ Benefits: ✅ smooth infinite scrolling ✅ better performance on large datasets ✅ no large skip operations ✅ improved user experience 🖥️ On frontend: We can use Intersection Observer to detect when user reaches the bottom → trigger next API call. ⚡ Simple idea, but widely used in: • social media feeds • e-commerce apps • search results Sometimes the right solution is not just optimizing rendering… It’s choosing the right 𝗱𝗮𝘁𝗮 𝗳𝗲𝘁𝗰𝗵𝗶𝗻𝗴 𝘀𝘁𝗿𝗮𝘁𝗲𝗴𝘆. #javascript #webdevelopment #systemdesign #backend #frontend

  • diagram
Like
Reply

To view or add a comment, sign in

Explore content categories