Unravel โบ Java concepts โบ NoSQL and caches
DynamoDB access
DynamoDB is a managed key-value and document database that stays fast at any size.
At a glance
A hotel key rack.
- 1Each table has a partition key (and optionally a sort key)getItem reads one item by full key; query reads many items sharing a partition key, optionally narrowed by sort-key conditions.
- 2The Enhanced Client maps beans: annotate a class @DynamoDbBean with @DynamoDbPartitionKey on the key getter, then table.putItem(entity) and table.getItem(Key.builder().partitionValue(id).build())
- 3scan reads the whole tableIt is slow and expensive and meant for rare maintenance work, not for requests.
- 4Global secondary indexes provide additional key choicesConditional writes (attribute_not_exists(id)) and transactions give safe updates.
- Choose a partition key with many distinct values and even traffic
- Use conditional expressions for optimistic locking (a version attribute) and for idempotent creation
- Items are placed on storage nodes by hashing the partition key; within a partition they are sorted by the sort key, which is why range queries are cheap
@DynamoDbBeanpublic class Session { private String sessionId; private String userId; private long expiresAt; @DynamoDbPartitionKey public String getSessionId() { return sessionId; } public void setSessionId(String sessionId) { this.sessionId = sessionId; }}DynamoDbTable<Session> table = enhanced.table("sessions", TableSchema.fromBean(Session.class));table.putItem(session);- line 1 uses DynamoDbBean, the core of DynamoDB access
- line 7 uses DynamoDbPartitionKey, the core of DynamoDB access
- line 12 uses DynamoDbTable, the core of DynamoDB access
Read the full explanation (about 1 min)
DynamoDB is a managed key-value and document database that stays fast at any size. You pay for and design around the access patterns you know in advance, because queries must use the key.
A mental model
A hotel key rack. Fetching the key for room 214 is instant no matter how big the hotel is. Asking "which guests have blue suitcases" means checking every hook.
How it works
Each table has a partition key (and optionally a sort key). getItem reads one item by full key; query reads many items sharing a partition key, optionally narrowed by sort-key conditions.
The Enhanced Client maps beans: annotate a class @DynamoDbBean with @DynamoDbPartitionKey on the key getter, then table.putItem(entity) and table.getItem(Key.builder().partitionValue(id).build()).
scan reads the whole table. It is slow and expensive and meant for rare maintenance work, not for requests.
Global secondary indexes provide additional key choices. Conditional writes (attribute_not_exists(id)) and transactions give safe updates.
Going deeper
Choose a partition key with many distinct values and even traffic. One hot key (a date, a status) throttles that partition regardless of total capacity.
Use conditional expressions for optimistic locking (a version attribute) and for idempotent creation. A ConditionalCheckFailedException is the normal signal that somebody else got there first.
Responses are paged (1 MB per call). Loop on lastEvaluatedKey or use the paginators, otherwise you silently see only the first page.
Under the hood
Items are placed on storage nodes by hashing the partition key; within a partition they are sorted by the sort key, which is why range queries are cheap. Capacity is measured in read and write units per second, and the SDK retries throttled calls with backoff.
Common mistakes
- Using scan for application queries.
- Hot partition keys.
- Ignoring pagination.
- Designing the tables like a relational schema.
How Unravel recognises it
When you paste code, Unravel looks for these signs of dynamodb access. Spotting them yourself is a good habit when you read unfamiliar code:
- Annotations:
@DynamoDbBean@DynamoDbPartitionKey@DynamoDbSortKey@DynamoDbSecondaryPartitionKey@DynamoDBTable@DynamoDBHashKey - Types:
DynamoDbClientDynamoDbEnhancedClientDynamoDbTableDynamoDbAsyncClientAttributeValuePutItemRequestGetItemRequestUpdateItemRequestQueryRequestScanRequestAmazonDynamoDBDynamoDBMapperTableSchemaQueryEnhancedRequestand 4 more
Part of a learning path
- Cloud-native Java ยท previous: Object storage SDKs (S3, Azure Blob, GCS) ยท next: Azure Service Bus, Event Hubs and Google Pub/Sub
- Data stores beyond SQL ยท previous: Cassandra and wide-column modelling ยท next: SQL mappers and builders (MyBatis, jOOQ)
Related concepts
Frequently asked questions
What is DynamoDB access?
DynamoDB is a managed key-value and document database that stays fast at any size. You pay for and design around the access patterns you know in advance, because queries must use the key.
What are the most common mistakes with DynamoDB access?
Using scan for application queries. Hot partition keys. Ignoring pagination. Designing the tables like a relational schema.
How can I see DynamoDB access in real code?
Paste your own Java into Unravel and it will point out where dynamodb access appears, explain how it works at the detail level you choose, and draw the flow of the code around it.