Scrivio Interview library
ConceptFoundations7 min read · updated 12 November 2026

How do you access DynamoDB from a Spring Boot service, and how do partition keys and sort keys determine what queries are possible?

From Spring Boot we usually use the AWS SDK for Java v2 with its DynamoDB Enhanced Client, which maps annotated bean classes to items, or Spring Cloud AWS's DynamoDbTemplate built on top of it; the Spring team does not provide an official Spring Data DynamoDB module. Every item is found through its partition key, and within one partition the sort key orders items and allows range conditions in a Query. A Query must give the partition key; anything else needs a Scan, which reads the whole table, or a secondary index, so we design the table around access patterns first, often as a single table with composite keys.

Why this question is asked

DynamoDB is a common choice for Spring Boot services on AWS because it needs no servers to manage and scales smoothly. But developers who approach it like a relational database quickly hit a wall: the query they need is not possible without scanning the table. Interviewers ask this question to check that we know how to call DynamoDB from Spring and, more importantly, that we understand how keys determine everything we can ask. In this answer, we will recap the data model, look at the client options with code, see how partition and sort keys shape Query, compare Query with Scan, add secondary indexes, and introduce access-pattern-first design.

1A Quick Recap of the DynamoDB Data Model

DynamoDB stores items, which are like rows, in tables. Each item has a primary key and any number of other attributes, and items in the same table can have different attributes. The primary key is either a partition key alone, or a partition key plus a sort key. DynamoDB hashes the partition key to decide which internal partition, a slice of storage and throughput on its servers, holds the item. All items with the same partition key are stored together, sorted by the sort key. That group is called an item collection.

So the partition key answers where is it, and the sort key answers in what order within that place. An item can be at most 400 KB.

2Accessing DynamoDB From Spring Boot

The most direct option is the AWS SDK for Java v2 with the Enhanced Client, which maps a Java bean to items through annotations on its getters.

Java
@DynamoDbBean
public class OrderItem {
private String pk; // "CUSTOMER#c-42"
private String sk; // "ORDER#2026-11-05T09:30:00Z#o-1001"
private BigDecimal total;
private String status;
@DynamoDbPartitionKey public String getPk() { return pk; }
@DynamoDbSortKey public String getSk() { return sk; }
// other getters and setters
}
@Bean DynamoDbEnhancedClient enhanced(DynamoDbClient client) {
return DynamoDbEnhancedClient.builder().dynamoDbClient(client).build();
}
@Bean DynamoDbTable<OrderItem> orders(DynamoDbEnhancedClient c) {
return c.table("shop", TableSchema.fromBean(OrderItem.class));
}

Spring Cloud AWS, a community project in the Spring ecosystem, adds auto-configuration of the clients and a DynamoDbTemplate with save, load, query and scan methods built on the Enhanced Client. It is convenient, but it is not a Spring Data module: there are no derived repository methods like findByCustomerId. Older community libraries offered a Spring Data DynamoDB module, but there is no official one from the Spring team, so check the maintenance state of any such library before adopting it.

3How Keys Shape a Query

A Query operation reads one item collection: it requires the partition key with equality, and may add a condition on the sort key such as equals, less than, between, or begins_with. Results come back in sort key order, ascending or descending. Because our sort key starts with ORDER# and a timestamp, we can ask for one customer's orders in a time range, newest first.

Java
QueryConditional cond = QueryConditional.sortBetween(
Key.builder().partitionValue("CUSTOMER#c-42").sortValue("ORDER#2026-10-01").build(),
Key.builder().partitionValue("CUSTOMER#c-42").sortValue("ORDER#2026-11-01").build());
List<OrderItem> october = orders.query(r -> r.queryConditional(cond).scanIndexForward(false).limit(20))
.items().stream().toList();
// reads only c-42's item collection, from the newest October order backwards: 20 items, cheap
// not possible as a Query: all orders with status SHIPPED (status is not a key)
What each key allows
RequestPossible?Why
Orders of c-42Querypartition key given
Orders of c-42 in OctoberQueryrange on sort key
Order o-1001 of c-42GetItemfull primary key given
All SHIPPED ordersScan, or a GSI on statusstatus is not a key
Orders over 500 for c-42Query + filter (pays for all read)total is not a key

Notice how much the design of the sort key matters. Because it begins with a sortable timestamp string, a date range becomes a simple between condition. A filter expression on status could be added to a Query, but it is applied after items are read, so we still pay for reading every item in the range.

4Query Versus Scan

A Scan reads every item in the table, page by page, up to 1 MB per request, and can filter the results afterwards. It is billed for everything it reads, not for what it returns. On a table with 50 million orders, finding the shipped ones with a Scan reads all 50 million items every time, which is slow, expensive and consumes throughput that customer requests need. Scans are fine for one-off exports, small tables or offline jobs, not for request paths. When a request needs a non-key attribute, the answer is an index.

5Secondary Indexes

A global secondary index, a GSI, is a second copy of selected attributes organised by a different partition key and sort key. DynamoDB maintains it asynchronously, so reads from a GSI are eventually consistent: an order shipped a moment ago may not appear yet. A GSI on status as partition key with the order date as sort key lets us Query shipped orders by date.

A local secondary index, an LSI, keeps the same partition key and offers an alternative sort key, for example the order total within a customer. It supports strongly consistent reads but must be defined when the table is created, and it limits each item collection to 10 GB. Both kinds of index cost extra writes, because every change to an indexed attribute is also written to the index. Note that a GSI on a low-variety value such as status concentrates traffic on few partition key values, which can create hot partitions; adding a shard suffix or using a date bucket spreads it.

6Model Around Access Patterns First

Because keys decide what is possible, DynamoDB design starts by listing every access pattern the service needs, such as get order by id, list a customer's orders by date, list orders by status for the warehouse, and only then designing keys and indexes that serve each one with a Query or GetItem. Single-table design takes this further: different entity types share one table, distinguished by key prefixes, so related items can be read together in one Query.

Java
pk sk attributes
CUSTOMER#c-42 PROFILE name, email
CUSTOMER#c-42 ORDER#2026-11-05T09:30:00Z#o-1001 total=49.80, status=PAID
CUSTOMER#c-42 ORDER#2026-10-12T14:02:00Z#o-0877 total=19.90, status=SHIPPED
// one Query on pk = CUSTOMER#c-42 returns the profile and the orders together

This is powerful for known patterns and awkward for new ones, which is the central trade-off of DynamoDB.

7Follow-up questions

What is the difference between GetItem and Query?

GetItem fetches exactly one item by its full primary key, partition key and sort key if the table has one. Query returns all items in one partition key's item collection that match an optional sort key condition, in sort key order, paginated.

Can I make a GSI read strongly consistent?

No. Global secondary indexes support only eventually consistent reads. If a read must reflect the latest write, read from the base table by key, or use a local secondary index when its constraints are acceptable.

How do I test DynamoDB code locally?

With DynamoDB Local or LocalStack in Testcontainers, creating the table and indexes in the test setup, so the same key design is exercised as in AWS.

8Conclusion

In this answer, we accessed DynamoDB from Spring Boot through the AWS SDK v2 Enhanced Client with annotated beans, or Spring Cloud AWS's DynamoDbTemplate, noting there is no official Spring Data DynamoDB module. Items are located by partition key and ordered by sort key within a partition, so Query needs the partition key and can apply range conditions on the sort key. Anything else is a full-table Scan or a secondary index: GSIs offer new key combinations with eventually consistent reads, LSIs share the partition key. So we model tables around access patterns first, often with single-table design.

What to say in the interviewAbout 90 seconds aloud

Open with

From Spring Boot I would use the AWS SDK for Java v2 with the DynamoDB Enhanced Client: a bean class with @DynamoDbBean, @DynamoDbPartitionKey and @DynamoDbSortKey on the getters, and a DynamoDbTable from TableSchema.fromBean.

Then, if they want more

Spring Cloud AWS adds auto-configuration and a DynamoDbTemplate on top, but there is no official Spring Data DynamoDB module from the Spring team, so no derived finder methods. The keys decide everything. DynamoDB hashes the partition key to place the item, and all items with the same partition key are stored together, sorted by the sort key.

A Query must give the partition key with equality and can add a sort key condition like between or begins_with, returning items in order. So if the partition key is the customer and the sort key starts with ORDER and a timestamp, I can get a customer's October orders newest first very cheaply.

Anything that doesn't start from the partition key is a Scan, which reads and bills the whole table, so it is not for request paths, or it needs a secondary index. A global secondary index gives a different partition and sort key, like status and date, but only eventually consistent reads.

A local secondary index shares the partition key with another sort key, is strongly consistent, but must be created with the table. That is why I design DynamoDB from the access patterns first, often single-table with prefixed composite keys.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Spring Data reference
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How do you access DynamoDB from a Spring Boot service, and how do partition keys and...": https://scrivio-interview-library.netlify.app/databases/spring-data-nosql/4d77b20b1b/