Have you experienced AppSync returning an empty array when you know there is data in DynamoDB? Perhaps you’ve noticed that it also returns a token allowing you to fetch more results?
I see this problem a few times each week. To understand what’s happening and how you might be able to resolve it you need to understand what AppSync and DynamoDB are doing.
What’s happening?
AppSync maps requests and responses between your GraphQL operation and a DynamoDB operation. The underlying DynamoDB operations that we’re interested in here are scan
and query
operation.
If you’re coming from a SQL background then you probably have two expectations that don’t apply to DynamoDB.
- Without a limit the
scan
/query
operation will return all results that match. - That limits are applied after the condition (filter)
DynamoDB has a hard limit of 1MB of data searched per operations. Once that limit is reached DynamoDB will return any results found with a token allowing you to continue searching. There are two key points here
- If your table or index has more than 1MB of data then you will need to perform multiple operations to fetch all of the data
- The filter is applied after fetching the data from the table or index. If no rows match the filter condition you will receive an empty array for the result and a token allowing you to fetch more results.
DynamoDB operations can also have limits applied to them but unlike SQL this limits the number of rows search instead of the number of rows returned.
Let’s take a simple example of scanning the following table for all characters starting with Wonder with a limit of 2.
Row | Name |
---|---|
1 | Batman |
2 | Superman |
3 | Wonder Woman |
4 | Cyborg |
5 | Acquaman |
The first scan
operation only looks at rows 1 & 2. Neither of those rows have a name starting with Wonder so DynamoDB returns an empty array as the result with a token allowing us to continue searching.
Passing the next token into the same query now searches rows 3 & 4 resulting in an array with a single entry for Wonder Woman but you will also receive a next token allowing you to continue searching.
Repeating the process one last time I will get an array with no result and no next token because the table scan
has now been completed.
How can I fix this?
Ultimately you need to write your client so that it can cope with their behaviour. You may also
Want to learn more about serverless applications and devops with AWS?
Sign up for our newsletter.