DynamoDB
case insensitive search
database query optimization
AWS
NoSQL

Case-insensitive queries in DynamoDB

Codemia Knowledge Hub ·

DynamoDB has no case-insensitive query option: comparisons are case-sensitive, so a query for partition key UserId "smith" will not match an item stored as "Smith". To match regardless of case, store a normalized (for example lowercase) version of the value and normalize the search term the same way before querying.

Normalize on write and on read

The most common strategy is to transform both the stored data and the query input to the same format, such as lowercase, using string functions in the application code that talks to DynamoDB. For example, if you store and query users by a primary key username, convert the username to lowercase before writing the record:

python
1# Python example: Inserting a user with normalized username
2import boto3
3dynamodb = boto3.resource('dynamodb')
4table = dynamodb.Table('Users')
5
6username = "JohnDoe"
7normalized_username = username.lower()
8
9table.put_item(
10   Item={
11        'username': normalized_username,
12        'email': '[email protected]'
13    }
14)

During a query, transform the search term to lowercase too, so it matches what was stored:

python
1# Python example: Querying with case insensitivity
2query_username = "JOHNDOE"
3normalized_query = query_username.lower()
4
5response = table.get_item(
6    Key={
7        'username': normalized_query
8    }
9)

Keep a lowercase copy in a global secondary index

Another approach uses a Global Secondary Index (GSI) to hold a case-insensitive duplicate of the key. When a record is created, the key attribute is stored both in its original form and as its lowercase equivalent, and the GSI is built on the lowercase value so queries can use it:

python
1# Assume you've created a GSI with lowercase_username as the key
2table.update_item(
3    Key={'original_username': 'JohnDoe'},
4    UpdateExpression="SET lowercase_username = :val",
5    ExpressionAttributeValues={':val': 'johndoe'}
6)

Backfill with DynamoDB Streams and AWS Lambda

For existing implementations that were not normalized from the start, a Lambda function attached to DynamoDB Streams can normalize automatically. It listens for changes in the table and writes the lowercase value to the corresponding field:

python
1# Example of a Lambda function to normalize usernames
2def lambda_handler(event, context):
3    for record in event['Records']:
4        if record['eventName'] == 'INSERT':
5            new_image = record['dynamodb']['NewImage']
6            original_username = new_image['username']['S']
7            lowercase_username = original_username.lower()
8            # Update table or GSI with lowercase value...

Trade-offs of each approach

Case-insensitive matching in DynamoDB comes from how you shape the data rather than from a database feature, and each technique has costs:

  • Storage overhead: duplicating data for normalization increases storage.
  • Consistency: asynchronous steps like streams can add latency and consistency concerns.
  • Complexity: GSIs and Lambda functions make the system more complex.
StrategyDescriptionProsCons
Data NormalizationConvert both stored and query data to a consistent format (e.g., lowercase).Simple to implement in app layerRequires consistent codebase adherence
Global Secondary Index (GSI)Use a GSI to store and query normalized data.Efficient queryingAdditional costs for maintenance
DynamoDB Streams and AWS LambdaNormalize incoming data using automation.Automated handlingPotential latency and complexity

Course
Beginner
27 lessons
10 hours
System Design Fundamentals

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

View the course
System Design practice on Codemia

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

Practice system design