DynamoDB Scan Pagination Returns Empty Pages Despite Matching Items Existing
0 reputation · 19 Jul 2025, 14:40 UTC
0 reputation · 19 Jul 2025, 14:40 UTC
A DynamoDB Scan with a highly selective FilterExpression on a table containing millions of items returns pages where Count is zero while ScannedCount and ConsumedCapacity indicate read capacity was consumed. The LastEvaluatedKey advances correctly, allowing the scan to continue, but the application cannot predict how many pages must be fetched to accumulate a target number of matching items (e.g., 100 results for a UI page). This behavior is documented: the 1 MB read limit applies to raw items before FilterExpression evaluation.
The workload uses provisioned throughput with consistent reads (RCU cost doubled per 4 KB chunk). Parallel Scan segments each enforce the 1 MB limit independently, so total scanned bytes can exceed the logical dataset size when filters are selective. KeyConditionExpression on a GSI is not an option because the filter attributes are not part of any index key schema. The application must remain on Scan + FilterExpression for now.
Design a client-side pagination loop that reliably accumulates a requested number of matching items without over-fetching or under-fetching, while respecting provisioned throughput limits and avoiding excessive empty-page rounds.
36775 reputation · 19 Jul 2025, 19:24 UTC
Keep scanning with LastEvaluatedKey until you accumulate the target number of matching items. Do not stop on an empty Count page. Tune Limit per page using the observed match rate (Count / ScannedCount), and project remaining cost from ConsumedCapacity per match before issuing the next request. If several consecutive pages scan near the 1 MB boundary while returning almost no matches, treat that as a low-density signal and consider stopping rather than scanning the full table.
Confirmed DynamoDB behavior: the 1 MB Scan response limit applies to items read before FilterExpression evaluation. A page can therefore have Count = 0 while ScannedCount > 0, ConsumedCapacity > 0, and LastEvaluatedKey present. Matching items exist beyond that key. Limit bounds items evaluated, not items returned, and operates independently of the byte cap. Consistent reads double RCU cost per 4 KB chunk.
| Item | Status | Notes |
|---|---|---|
Count zero while ScannedCount advances | Confirmed | Filter discards items after the 1 MB read window fills. |
LastEvaluatedKey advances on empty pages | Confirmed | Resume from this key; the page is not terminal. |
| Parallel Scan segments each have independent 1 MB limits | Confirmed | Aggregate scanned bytes can exceed table size; all segments can return empty pages at once. |
| Export to S3 is a partial key-range fallback | Not accurate | Export exports the whole table, not a key range. |
accumulatedMatches, lastKey = null, and a short sliding match-rate window.Scan with FilterExpression, ExpressionAttributeValues, a modest starting Limit, ExclusiveStartKey = lastKey, ConsistentRead if required, and ReturnConsumedCapacity = "TOTAL".Items; DynamoDB already applied the filter server-side.ScannedCount, Count, ConsumedCapacity.CapacityUnits, and LastEvaluatedKey.accumulatedMatches.length >= target, stop and return the slice.LastEvaluatedKey is null, stop; the table is exhausted.lastKey = LastEvaluatedKey and adjust Limit using the adaptive rule below.Start with a conservative Limit so one unproductive page does not burn excessive capacity. After each page, compute matchRate = Count / max(ScannedCount, 1) over the last 3–5 pages.
Limit moderately, for example 1.5x, to reduce round trips.Limit to avoid over-scanning empty regions.Limit so projected bytes stay under 1 MB; average item size determines how many items fit, not item count alone.Pace requests and back off on throttling to avoid bursting provisioned RCUs.
ConsumedCapacity and ScannedCount to estimate remaining costPer page, compute:
rcuPerMatch = ConsumedCapacity.CapacityUnits / max(Count, 1)
Then project the cost to finish:
projectedRCU = (target - accumulatedMatches.length) * rcuPerMatch
Compare projectedRCU against remaining capacity or a cost budget before the next request. Include the consistent-read multiplier in the projection. Treat the estimate as local and uncertain; recent match rate does not guarantee the rest of the table has the same density.
A practical signal is: ScannedCount per page remains near the page ceiling while Count stays near zero across several consecutive pages, and projectedRCU keeps rising. Stop the scan and choose a fallback at that point.
Export to S3 is only sensible if matches are spread across the table and you need a bulk dataset; it exports the entire table, not a key range, so it may not help when matches are clustered in a small range. If matches are clustered, continuing from LastEvaluatedKey is usually cheaper.
ConsumedCapacity, ScannedCount, Count, and LastEvaluatedKey for every response; confirm Count can be zero while ScannedCount exceeds zero and LastEvaluatedKey is present.ConsumedCapacity after a controlled backfill run.What is the average item size? It determines how many items fit in the 1 MB page and materially changes how aggressively Limit can be increased.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.