The Journey Of A DynamoDB Query: A Behind The Scenes Adventure
Uriel Bitton
AWS Cloud Consultant | The DynamoDB guy | AWS Certified | I help you supercharge your DynamoDB database ????
Welcome to the 33rd edition of Excelling With DynamoDB!
In this week’s issue, I'm going to take you on a journey alongside a DynamoDB query, how it starts, what goes on behind the scenes and how to fulfill its purpose - all in a super easy way to understand.
Have you ever wondered what happens when you invoke DynamoDB's query API?
Maybe not, but it would still be interesting to know how the internal cogs of DynamoDB turn and grind to retrieve data from your table.
In this article, let's dive into this journey and see how it works behind the scenes, in a simplified manner.
When you execute a DynamoDB query, you're tapping into a meticulously engineered system that is designed for low latency, scalability and efficiency.
Let's take a look at what goes on step by step.
1. The Request is made
A DynamoDB starts with a request payload from the client.
Here's what that includes:
Here's an example query:
"TableName": "Orders",
"KeyConditionExpression": "customerId = :customer_id AND orderDate BETWEEN :start_date AND :end_date",
"ExpressionAttributeValues": {
":customer_id": "123",
":start_date": "2024-11-01",
":end_date": "2024-12-31"
}
2. The Request Router
Before your query even touches DynamoDB’s storage layers, it passes through the Request Router, DynamoDB’s entry point.
Here's how it processes your requests:
3. Parsing and Validating the query
Before moving to the next step, DynamoDB performs the following validations:
4. Finding the right partition
Every DynamoDB table is internally divided into partitions.
A partition is a data storage node - each node contains a part of your data.
This is why a well-designed partition key ensures better data scalability, and on the other hand, ignoring this leads to hot partitions.
5. Navigating the partition with sort keys
Once the partition is located, here's what happens:
Queries are made efficiently lowering latency and costs, no matter how much data is stored on your database.
6. Reading from storage
After it identifies the relevant items, DynamoDB fetches the actual data.
Depending on your request settings - consistent or eventually consistent queries - DynamoDB will retrieve the latest version of the data or (infrequently) a slightly outdated version of the data (faster and cheaper).
If a ProjectionExpression is provided, DynamoDB only fetches the attributes you specify to get back, which minimizes unnecessary reads.
7. Applying filters (Post-query processing)
Once the raw results are retrieved, DynamoDB applies any optional filter expressions.
Unlike the KeyConditionExpression, which limits the data DynamoDB reads, filters are applied after data retrieval.
For example, if your query retrieves 100 items but the filter excludes 90 of them, your query will return 10 items but will still be billed for reading all 100 items.
8. Response is returned
After filtering, DynamoDB will package the results by converting the raw data into a JSON response.
It will then add the details of the response such as:
Here's an example response:
{
"Items": [
{"orderId": "A123", "total": 100.0},
{"orderId": "B456", "total": 150.0}
],
"Count": 2,
"ScannedCount": 10,
"ConsumedCapacity": {
"TableName": "Orders",
"CapacityUnits": 5
}
}
At this step, DynamoDB will also process the Limit attribute if one is provided and in return provide a LastEvaluatedKey for you to create a pagination of your data.
Conclusion
Understanding how DynamoDB processes a query is key to designing more scalable, performing, and cost-efficient databases.
By breaking down each step—from request routing and validation to partition navigation and result filtering—you can optimize your queries for better outcomes.
?? My name is Uriel Bitton and I hope you learned something in this edition of Excelling With DynamoDB.
?? You can share the article with your network to help others learn as well.
?? If you're looking for help with DynamoDB, let's have a quick chat:
?? I hope to see you in the next week's edition!