Quick Answer: When you run an offset query in Google Cloud Firestore, you are billed for every skipped document read up to your offset count, plus the documents returned. To avoid unexpected read spikes, replace offset() with cursor-based pagination using startAfter() or startAt(), which leverages Firestore's internal indexes to fetch records without paying for skipped data.
Serverless databases feel like magic right up until you receive an invoice for reading documents your users never actually saw.
I have a lot of time for Firestore. It is a solid choice when you need a database that handles spiky traffic without forcing you to pay for an expensive, idle database instance running 24/7. But because Firestore bills on granular operations—per read, per write, and per delete—standard relational database habits will catch you off guard.
The biggest trap? Traditional offset pagination.
How does Firestore charge for offset queries?
Firestore bills you for every document evaluated up to the offset, plus the documents returned in your limit. If you execute a query with an offset of 100 and a limit of 10, Firestore charges you for 110 document reads, even though your client application only receives 10.
Think of it like hiring a courier to fetch page 50 of a report. Instead of turning directly to page 50, the courier reads aloud pages 1 through 49, charges you a fee for every page they glance at, and then hands you the single page you requested.
If a user clicks through to page 10 of a 20-item list in your UI, that single request bills you for 200 reads. Let that UI pattern scale across thousands of daily active users, and you will rack up millions of unnecessary document reads on skipped data.
Why does Firestore charge for skipped documents?
Firestore is a multi-tenant, serverless document store that does not maintain a dedicated virtual machine or cache buffer for your open connection. To satisfy an offset query, the underlying storage nodes must still locate, walk through, and process each index entry and matching document before discarding it.
Because Firestore allocates compute and I/O resources to evaluate each document in that sequence, it passes that operational cost directly to you. It is designed around the premise that you pay strictly for the work the system does under the hood.
Offset pagination vs. cursor pagination: How do they compare?
Offset pagination relies on counting past a numeric volume of records, which creates linear read costs (O(N) reads where N is the offset). Cursor-based pagination uses an anchor point—a specific document snapshot or indexed field value—to jump straight to that location in O(1) read cost.
| Feature | Offset Pagination (offset()) |
Cursor Pagination (startAfter()) |
|---|---|---|
| Read Cost | Skipped docs + Returned docs | Only returned docs |
| Cost at Page 50 (Limit 10) | 510 reads | 10 reads |
| Performance | Degrades as offset grows | Constant speed via index |
| Query Mechanism | Evaluates and drops records | Seeks direct index position |
| Recommended Use Case | Small datasets (< 50 items) | Production apps, infinite scroll |
How do you implement cursor-based pagination in Firestore?
To paginate without burning reads, pass the last visible document from your current result set into startAfter(). Firestore requires indexes for every query, so it uses that document reference to jump directly to the correct index position without scanning prior records.
// Fetch the initial page
const firstBatch = await db.collection('orders')
.orderBy('createdAt', 'desc')
.limit(10).get();
const lastVisibleDoc = firstBatch.docs[firstBatch.docs.length - 1];
// Fetch next page without paying for earlier reads
const nextBatch = await db.collection('orders')
.orderBy('createdAt', 'desc')
.startAfter(lastVisibleDoc)
.limit(10).get();
By pointing Firestore to an exact document anchor, you pay only for the 10 documents you actually use.
Frequently Asked Questions
Can you implement traditional page numbers (e.g., page 1, 2, 3) with cursors?
Not easily. Cursor-based pagination is inherently sequential, making it ideal for "Next/Previous" buttons or infinite scrolling. If your UI strictly requires jumping directly to arbitrary page numbers, you must either accept the offset() read billing penalty or pre-aggregate page boundaries in separate index documents.
What happens if the cursor document is deleted before the next fetch?
If you pass a DocumentSnapshot that has since been deleted, Firestore can still extract the field values and order key from that snapshot to navigate the index. Alternatively, you can use startAfter() with literal field values (such as a timestamp or unique ID) instead of the full document snapshot.
Does client-side caching reduce offset read costs?
If you use the Firebase Web or Mobile SDK with offline persistence enabled, subsequent identical reads can sometimes be served from local cache. However, backend server SDKs (such as Node.js or Go) do not maintain client cache and will incur full read costs on every offset invocation.
Top comments (0)