AWS Lambda
DynamoDB
NoSQL database
cloud computing
serverless architecture

Querying DynamoDB without PrimaryKey with Lambda

System Design practice on Codemia

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

Practice system design

Overview of DynamoDB and Lambda

Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. It executes queries based on primary keys (Partition Key and optional Sort Key) efficiently, but querying without a primary key can be a bit tricky. AWS Lambda, a serverless computing service, allows execution of code in response to events, enabling dynamic interactions with DynamoDB.

In this article, we will explore how to query DynamoDB without using the primary key by leveraging AWS Lambda functions. We'll look into secondary indexes, scanning, and filter expressions, which help in retrieving data that isn't directly addressable by primary keys.

Querying Strategies

1. Using Secondary Indexes

DynamoDB allows the creation of secondary indexes, which enable querying data using non-primary key attributes. There are two types of secondary indexes:

  • Global Secondary Index (GSI): A separate table that allows queries on any attribute.
  • Local Secondary Index (LSI): Allows querying on non-primary key attributes that share the same partition key but have a different sort key.

Example:

Suppose we have a DynamoDB table Orders:

OrderIDCustomerIDDateAmount
101C0012023-10-05
102C0022023-10-06

To query orders by CustomerID, a GSI can be used. Define the CustomerID as the partition key for the GSI.

json
1{
2    "IndexName": "CustomerIdIndex",
3    "KeySchema": [
4        {"AttributeName": "CustomerID", "KeyType": "HASH"}
5    ],
6    "Projection": {
7        "ProjectionType": "ALL"
8    }
9}

2. Using Scans

Scanning the table is a straightforward way to retrieve items without regard to primary key, but it reads every item in the table, which can be inefficient for large datasets.

Example Lambda Function Using a Scan

Below is a Python AWS Lambda function using Boto3 to scan the Orders table for all orders over $100.

python
1import boto3
2
3def lambda_handler(event, context):
4    dynamodb = boto3.resource('dynamodb')
5    table = dynamodb.Table('Orders')
6    
7    response = table.scan(
8        FilterExpression=Attr('Amount').gt(100)
9    )
10    
11    return response['Items']

3. Utilizing Filter Expressions

Filter expressions allow you to limit data returned by a scan or query operation by filtering out items after reading. This does not reduce the read capacity consumed but reduces the data returned.

Example:

Extend the previous example to use a filter expression:

python
1import boto3
2from boto3.dynamodb.conditions import Attr
3
4def lambda_handler(event, context):
5    dynamodb = boto3.resource('dynamodb')
6    table = dynamodb.Table('Orders')
7
8    # Scan and filter
9    response = table.scan(
10        FilterExpression=Attr('Amount').gt(100)
11    )
12    
13    # Return filtered items
14    return response['Items']

Key Considerations

StrategyEfficiencyUse Case/Limitations
Secondary IndexesHighEfficient way to query non-primary key attributes. Requires planning and creation.
ScansLow for large dataUseful for small tables; unfiltered scan reads every item.
Filter ExpressionsModerateReduce returned data at the cost of reading all items first.

Benefits and Drawbacks

  • Secondary Indexes:
    • Benefits: Efficient querying, adds flexibility to how you access data.
    • Drawbacks: Must anticipate indexing needs upfront due to additional read/write capacity cost.
  • Scans:
    • Benefits: Simplicity in implementation.
    • Drawbacks: Expensive for large datasets; should be used sparingly.
  • Filter Expressions:
    • Benefits: Can refine scan/query results.
    • Drawbacks: Does not improve efficiency; still reads all data initially.

Advanced Topics

  • DynamoDB Streams: Capture and respond to table changes via Lambda, aiding in reactive data processing.
  • Optimizing Queries: Properly configure read capacity, use batched operations, and explore AWS Pricing Calculator for cost management.
  • Combining Filters and Queries: Consider scenarios where both filtering and index-based queries can work in tandem to offer highly efficient results.

In conclusion, querying DynamoDB without a primary key using Lambda is versatile and powerful when leveraging advanced features like secondary indexes and filter expressions. While LSI and GSI provide targeted access to attributes, scans remain a handy fallback when other strategies aren't feasible. Always consider the trade-offs in efficiency and cost when tailoring your approach for accessing and modifying data within DynamoDB.


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.