Skip to content

DynamoDB Query behaves differently on local Dynamo VS cloud service

0

Hello, I noticed an inconsistent behavior between local DynamoDB and the cloud based DynamoDB service.

In both cases, I have the following table and index:

  • Table 'associations'
  • Primary key is a composite primary key: hash key named 'PK', type string, and range key named 'SK', type string
  • Global secondary index 'child-to-parent-associations'
  • Partition key is 'SK'
  • Sort key is 'PK'

When I try to run a Query command using the DynamoDB JS SDK on the GSI with a Limit of 1:

  • Locally, I do get the item I am expecting and a last evaluated key
  • Remotely, I do get the item but no last evaluated key

This is the code that runs the query: ` const query = new QueryCommand({ TableName: 'associations', IndexName: "child-to-parent-associations", Limit: 1, KeyConditionExpression: "SK = :id and begins_with(PK, :type)", ExpressionAttributeValues: { ":id": <SK value>, ':type': <type value> }, })

const response = await docClient.send(query) `

I tried the same thing with the Golang SDK, and the results are the same. I have also tried older versions of the Docker image for local DynamoDB (back to version 2.3.0), and the behavior is identical to what I described above. Has anyone run into this situation before?

Thank you!

asked a month ago102 views
2 Answers
0

This discrepancy between DynamoDB Local and the live DynamoDB service is likely due to differences in implementation between the two environments.

When executing a Query operation with a Limit parameter:

  • The live DynamoDB service will only return LastEvaluatedKey if there are more results available beyond what was returned in the current response
  • DynamoDB Local appears to be returning LastEvaluatedKey regardless of whether there are additional results that match your query

This behavior is similar to a known issue with PartiQL SELECT statements in DynamoDB Local, where there are inconsistencies between the local implementation and the live service when using ordering and limit parameters.

The most reliable approach is to trust the behavior of the live DynamoDB service as the correct implementation. In your case, if the cloud service isn't returning a LastEvaluatedKey, it means there are no more items that match your query criteria beyond what was returned.

When developing locally, you may need to account for these differences in your pagination logic. For example, you might need to check if there are actually more results available rather than relying solely on the presence of LastEvaluatedKey.
Sources
ExecuteStatement with Limit on DynamoDB Local returns wrong result set when using descending ordering | AWS re:Post
Query - Amazon DynamoDB

answered a month ago
0

Hello,

There are certain differences in the implementations of Local and Remote setup of DynamoDB that would lead to this particular case but this is within the expectations of how DynamoDB is supposed to function.

If LastEvaluatedKey is empty, then the "last page" of results has been processed and there is no more data to be retrieved.

If LastEvaluatedKey is not empty, it does not necessarily mean that there is more data in the result set. The only way to know when you have reached the end of the result set is when LastEvaluatedKey is empty.

reference docs - API_Query_ResponseSyntax

To learn more about how to handle the case where LastEvaluatedKey is returned please refer to - Paginating table query results in DynamoDB

AWS
answered 15 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.