Pagination, Filter Expressions, and Projections
Real list screens need pages, filters, and only the attributes they display.
Introduction
Real list screens need pages, filters, and only the attributes they display. DynamoDB pagination is cursor-based: each page returns a LastEvaluatedKey that you pass back as ExclusiveStartKey. Filters and projections shape what is returned, but neither reduces what is read.
Purpose of this lesson
You will implement cursor pagination for an API, understand how Limit interacts with filters, and trim responses with projection expressions.
Understanding the topic
Rules to remember:
Limitcaps the number of items evaluated, before the filter — a page can contain fewer items than the limit, or none, whileLastEvaluatedKeyis still set.- Pages stop at 1 MB of data read even if
Limitis not reached. ProjectionExpressionreturns selected attributes; it does not lower consumed capacity.- There is no offset-based paging; to jump to page N you must read through earlier pages or design keys for direct access.
Select: "COUNT"returns only counts, but still reads (and charges for) the matching items.
Informative example
API-friendly pagination with an opaque cursor:
import { QueryCommand } from "@aws-sdk/lib-dynamodb";import { ddb, TABLE } from "./ddb";const encode = (key?: Record<string, unknown>) =>key ? Buffer.from(JSON.stringify(key)).toString("base64url") : undefined;const decode = (cursor?: string) =>cursor ? JSON.parse(Buffer.from(cursor, "base64url").toString()) : undefined;export async function listOrders(customerId: string, cursor?: string) {const page = await ddb.send(new QueryCommand({TableName: TABLE,KeyConditionExpression: "PK = :pk AND begins_with(SK, :o)",ExpressionAttributeValues: { ":pk": `CUSTOMER#${customerId}`, ":o": "ORDER#" },ProjectionExpression: "SK, #total, #status",ExpressionAttributeNames: { "#total": "total", "#status": "status" },ScanIndexForward: false,Limit: 20,ExclusiveStartKey: decode(cursor),}));return { items: page.Items ?? [], nextCursor: encode(page.LastEvaluatedKey) };}
Validate decoded cursors on the server (for example check that PK matches the requested customer) so clients cannot use a cursor to read another customer's partition.
Useful templates
Fill a page when using a filter
If you often filter on the same attribute, put it in the key (or a sparse GSI) instead — filters still pay for every item read.
export async function listShipped(customerId: string, pageSize = 20) {const items: Record<string, unknown>[] = [];let ExclusiveStartKey: Record<string, unknown> | undefined;do {const page = await ddb.send(new QueryCommand({TableName: TABLE,KeyConditionExpression: "PK = :pk AND begins_with(SK, :o)",FilterExpression: "#status = :shipped",ExpressionAttributeNames: { "#status": "status" },ExpressionAttributeValues: { ":pk": `CUSTOMER#${customerId}`, ":o": "ORDER#", ":shipped": "SHIPPED" },ExclusiveStartKey,}));items.push(...(page.Items ?? []));ExclusiveStartKey = page.LastEvaluatedKey;} while (ExclusiveStartKey && items.length < pageSize);return items.slice(0, pageSize);}
Best practices
- Return opaque cursors, never raw keys, from public APIs.
- Move frequent filters into sort keys or sparse indexes.
- Use projection expressions to keep responses small.
Common mistakes
- Treating an empty page as the end of results while
LastEvaluatedKeyis still set. - Expecting
Limitto mean 'items returned'. - Building total counts by scanning on every request — maintain counters instead.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1IntermediateQuestionWhy might a Query with Limit 10 return 3 items?+
Answer
2AdvancedQuestionHow do you implement 'jump to page 50' in DynamoDB?+
Answer
Hands-on exercise
Hands-on: insert 45 orders for one customer and page through them with listOrders. Assert you get pages of 20, 20, and 5 items, and that the final nextCursor is undefined.
Summary
Pagination is cursor-based through LastEvaluatedKey. Limit counts evaluated items, filters and projections shape output but not cost, and frequent filters belong in keys or sparse indexes.