Skip to content

Pagination Done With Cursors

I remember sitting in my tiny apartment at 11 PM, staring at a browser tab that had basically frozen because I tried to load 5,000 database entries all at once. My fan was whirring like a jet engine, and I felt that specific, sinking heat in my chest—the kind where you realize your “working” code is actually a total disaster waiting to happen. Most tutorials make learning how to build pagination sound like this elegant, mathematical dance of offsets and limits, but they skip the part where you’re actually sweating over why your UI just jittered into oblivion.

I’m not here to give you a dry lecture on API architecture or some theoretical white paper. I want to show you how to actually handle the data without crashing your user’s browser or losing your mind in the process. I’m going to break down the real-world logic of slicing up your data, from the backend queries to the frontend buttons, based on the mistakes I actually made when I was still figuring things out. No fluff, just the stuff that actually works when you’re building something real.

Table of Contents

Choosing Your Poison Offset vs Cursor Pagination

Choosing Your Poison Offset vs Cursor Pagination

So, you’ve realized that dumping 5,000 rows of data into a single frontend component is a recipe for a crashed browser, and now you’re stuck at the first real fork in the road. When I first hit this wall, I thought there was just one way to do it. Turns out, the debate between offset vs cursor pagination is basically the “fast fashion vs. sustainable basics” of the dev world. You have to pick your poison based on how much your app is actually going to grow.

Offset pagination is what most of us reach for first because it feels intuitive—it’s just like telling your database, “Hey, skip the first 20 rows and give me the next 10.” It works fine for small projects, but I learned the hard way that it’s a nightmare for database query optimization once your tables get massive. Plus, if a new item gets added to the top of the list while a user is scrolling, they’re going to see the same duplicate item again on the next page. It’s super frustrating.

If you’re aiming for something smoother, like a seamless infinite scroll implementation, you’ll probably want to look at cursors. Instead of counting rows, you’re essentially using a pointer to mark where you left off. It’s a bit more logic to write upfront, but it keeps things consistent and way faster as your data scales.

Why Your First Database Query Might Fail You

Why Your First Database Query Might Fail You

When I first tried to roll my own pagination, I thought I was being clever by just slapping a `LIMIT` and an `OFFSET` onto my SQL queries and calling it a day. It worked fine for my tiny test dataset of maybe twenty rows. But then I tried to scale it up, and suddenly, my app felt like it was running through waist-deep mud. I spent an entire Saturday morning staring at my terminal, wondering why a simple fetch was taking seconds instead of milliseconds. It turns out, as your table grows, the database still has to scan through every single one of those “skipped” rows just to find where your actual page starts. It’s a massive hit to database query optimization that most tutorials just gloss over.

The real headache hit when I realized that offset-based logic is actually kind of fragile. If a new user signs up or a post gets deleted while someone is scrolling, the whole list shifts. Suddenly, a user sees the same item twice, or worse, skips an entry entirely. It’s that weird, glitchy feeling you get in an infinite scroll implementation when the content jumps around unexpectedly. I realized then that I wasn’t just fighting syntax; I was fighting the way data actually moves.

Small Wins (and How Not to Break Your App)

  • Don’t try to build a custom solution for every single component; if you’re just displaying a list of blog posts, a standard offset approach is fine. Save the complex cursor logic for when you’re actually dealing with massive, real-time data streams.
  • Always hardcode a maximum limit for your page size. I learned this the hard way when I left the “limit” parameter out of a query and accidentally tried to fetch 5,000 rows at once, which basically turned my browser into a space heater.
  • Stop making your users click “Next” a hundred times. If you’re building something for humans, implement a way for them to jump to a specific page or at least give them a clear sense of how much data is actually left in the pile.
  • Test your pagination with empty states and tiny datasets. It’s easy to write code that works when you have 100 items, but it’s a total headache when you realize your UI breaks because there are only two items or, even worse, zero.
  • Keep your API responses clean by only sending the metadata you actually need. You don’t need to dump the entire user object back to the client just to tell them they’re on page three; just send the current page, the total count, and the next link.

The "Don't Mess This Up" Checklist

Stop trying to fetch everything at once; if your app starts lagging because you’re loading 500 items instead of 20, you’ve already lost the battle.

Pick your pagination method based on your data, not what looks coolest in a tutorial—use offset if you need to jump to page 10, but switch to cursors if your data is constantly changing and shifting under your feet.

Always test with “ugly” data, like really long strings or weird characters, because nothing breaks a layout faster than a paginated list that can’t handle real-world chaos.

The Reality Check

The Reality Check: Scaling data pagination.

Look, at the end of the day, there isn’t one “perfect” way to handle your data. If you’re building a small side project where the data won’t grow beyond a few hundred rows, offset pagination is totally fine—don’t over-engineer it just because a senior dev said so. But if you’re actually planning on scaling, or if your users are going to be scrolling through endless feeds like they do on TikTok, you’re going to want to invest the extra time into cursor-based logic. I learned the hard way that trying to force a simple offset query onto a massive dataset is a great way to watch your application performance tank in real-time. Just pick the tool that actually fits the scale of what you’re building.

If you’re currently staring at a broken loop or a database error that makes absolutely no sense, please know that I’ve been there. I spent three days of my bootcamp life fighting a single bug that turned out to be a typo in my limit parameter. It feels heavy right now, but it’s just logic puzzles disguised as frustration. You aren’t “bad at this”; you’re just in the middle of the messy part where the actual learning happens. Keep your head down, keep your notes handy, and just keep shipping code. You’ll get it.

Frequently Asked Questions

If I go with cursor pagination, how do I actually handle the logic for when a user wants to jump straight to page 10?

The short answer? You can’t. Not really. And that’s the part that pissed me off when I first learned this. Cursor pagination is built for a continuous stream, like an infinite scroll, not a textbook. If you try to force a “jump to page 10” with cursors, you’re basically fighting the architecture. If your users absolutely need that jumpy, old-school navigation, just stick with offset pagination. Don’t over-engineer a solution for a problem you don’t actually have.

Is there a point where adding more pages actually starts killing my app's performance, or is that just a myth?

It’s definitely not a myth. When I first started, I thought more pages just meant more rows, but I learned the hard way that `OFFSET` is a total performance killer. As your database grows, the engine still has to scan through every single skipped row just to find where your page starts. It’s like reading a book but having to flip through every page from the beginning every time you want to start a new chapter.

How do I stop the UI from looking janky when the data changes while someone is halfway through scrolling?

The “jumpy scroll” is the absolute worst. I remember my first project where, every time a new item loaded, the whole page would snap upward and I’d lose my place. It’s infuriating. To stop the jank, you need to stop letting the browser guess where things go. Use skeleton screens to reserve that vertical space before the data hits, or look into “scroll anchoring.” Basically, tell the UI to hold its breath until the data arrives.

Do I really need to build this logic into my backend, or can I just hack together a solution on the frontend for now?

Look, I get the temptation. When you’re tired after a shift and just want to see your data on the screen, “hacking it” on the frontend feels like a win. But please, don’t do it. If you just fetch everything and slice it in the browser, your app will eventually choke once that database grows. It’s like trying to carry a whole grocery haul in one trip instead of using a cart. Build it into the backend now; your future, sleep-deprived self will thank you.

About Marisol Ebanks

I am close enough to the start to remember which bit was actually confusing.