DynamoDB
hash key
query
database
AWS

dynamodb query with hash key only

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Amazon DynamoDB is a fully managed NoSQL database service offered by Amazon Web Services (AWS) that provides fast and predictable performance with seamless scalability. DynamoDB is designed to handle large volumes of concurrent read and write operations with minimal latency, making it an ideal choice for applications such as mobile backends, web services, and IoT applications.

One crucial element of designing a DynamoDB table is optimizing its schema using hash (partition) keys and optional sort keys. This article walks you through querying with a hash key only, which is often sufficient for many use cases in DynamoDB.

Understanding Hash Keys

In DynamoDB, the hash key is a unique identifier for each item within a table. The hash key is used to distribute data across partitions to ensure uniform load distribution and scalability. When designing a table, selecting an appropriate hash key is critical to optimizing data access and ensuring predictable performance.

Designing Hash Keys for Queries

When querying with a hash key:

  1. Uniform Distribution: Choose a hash key that will distribute requests evenly across partitions. A poorly selected hash key can result in "hot partitions" where one partition receives a disproportionate number of requests, potentially leading to performance bottlenecks.
  2. Unique Identifier: The hash key should effectively distinguish between items. Common choices for hash keys include user IDs, order IDs, email addresses, or any other unique identifiers.
  3. Projection of Attributes: By default, DynamoDB retrieves all attributes for items that match the hash key. However, using projections, you can specify a subset of attributes to retrieve, reducing data size and query latency.

Querying with Hash Key

To query using only the hash key, you specify the hash key value in your query. DynamoDB will then return all items that match the hash key. Here's an example in both JavaScript using AWS SDK and an explanation in Python:

JavaScript Example

javascript
1const AWS = require('aws-sdk');
2const dynamoDB = new AWS.DynamoDB.DocumentClient();
3
4const params = {
5  TableName: 'Users',
6  KeyConditionExpression: 'userId = :uid',
7  ExpressionAttributeValues: {
8    ':uid': '12345'
9  }
10};
11
12dynamoDB.query(params, (err, data) => {
13  if (err) {
14    console.error('Error querying items', err);
15  } else {
16    console.log('Query successful', data.Items);
17  }
18});

Python Example

python
1import boto3
2
3dynamodb = boto3.resource('dynamodb')
4table = dynamodb.Table('Users')
5
6response = table.query(
7    KeyConditionExpression=Key('userId').eq('12345')
8)
9
10for item in response['Items']:
11    print(item)

Advantages of Querying with Hash Key Only

  1. Efficiency: Queries using the hash key are efficient and result in O(1) complexity because the hash key directly maps to the partition where data is stored.
  2. Performance: Because you are directly querying the partition with the hash key value, query latency is minimized, providing faster response times.
  3. Cost-Effective: Reduces the read capacity units (RCUs) consumed as only the relevant partition is scanned rather than the entire table.

Limitations of Hash Key Query

  1. Limited Flexibility: Filtering data beyond the hash key requires the use of filters which are applied after the data retrieval, possibly increasing costs and latency.
  2. Limited Results: Without a sort key, you cannot use range queries. Each hash key maps to a single item or a set of items only if the optional sort key is added.

Key Considerations

Designing the table with proper selection of hash keys is essential. Here is a summary of key considerations:

AspectDescription
Hash Key ChoiceChoose unique, high cardinality values for even data distribution.
Query SpeedDirectly accesses specific partitions, ensuring faster query performance.
Partition LoadSelecting poor hash keys may result in uneven distribution, causing "hot" partitions.
Range QueriesWithout a sort key, cannot perform range queries.

Additional Techniques

  • Global Secondary Index (GSI): Create GSIs to enable queries on non-primary key attributes, expanding the flexibility of querying beyond just the hash key.
  • Local Secondary Index (LSI): Enhance query capabilities by allowing secondary sort keys, making range queries possible.

Conclusion

Optimizing for hash key effectiveness in DynamoDB is crucial for application scalability and performance. By carefully designing your hash keys and understanding the limitations of querying with hash keys only, you can benefit from efficient data retrieval, reduced latency, and decreased costs. As your needs grow, consider leveraging secondary indexes to expand the query capabilities of your DynamoDB tables.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.