BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ NoSQL and caches

Persistence

DynamoDB access

DynamoDB is a managed key-value and document database that stays fast at any size.

๐Ÿ”Ž Unravel this example

At a glance

Mental model

A hotel key rack.

What happens, in order
  1. 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.
  2. 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())
  3. 3scan reads the whole tableIt is slow and expensive and meant for rare maintenance work, not for requests.
  4. 4Global secondary indexes provide additional key choicesConditional writes (attribute_not_exists(id)) and transactions give safe updates.
Remember
Watch out
Using scan for application queriesHot partition keysIgnoring paginationDesigning the tables like a relational schema
Example (highlighted lines explain it)
@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);
  1. line 1 uses DynamoDbBean, the core of DynamoDB access
  2. line 7 uses DynamoDbPartitionKey, the core of DynamoDB access
  3. 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:

Part of a learning path

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.