Spring Boot with Amazon DynamoDB
Learn how to integrate Spring Boot with Amazon DynamoDB step by step using the AWS SDK for Java 2.x. This guide covers DynamoDB architecture, table design, partition keys, CRUD operations, GSIs, scans vs queries, Spring Boot implementation, and production best practices.
Introduction
Not every application is best suited for a relational database.
Applications such as:
- Shopping carts
- User sessions
- Product catalogs
- IoT platforms
- Gaming leaderboards
- Chat applications
- Banking event logs
often require:
- Very low latency
- Massive scalability
- Flexible schema
- Millions of requests per second
Amazon DynamoDB is AWS's fully managed NoSQL database service designed to provide single-digit millisecond performance at virtually unlimited scale.
In this article, we will integrate a Spring Boot application with Amazon DynamoDB using the AWS SDK for Java 2.x and implement CRUD REST APIs.
Learning Objectives
After completing this article, you will understand:
- What is DynamoDB?
- NoSQL vs SQL
- DynamoDB Architecture
- Tables
- Items
- Attributes
- Partition Keys
- Sort Keys
- CRUD Operations
- Query vs Scan
- Global Secondary Index (GSI)
- Spring Boot Integration
- AWS SDK Configuration
- Production Best Practices
What is DynamoDB?
Amazon DynamoDB is a fully managed NoSQL database service.
Unlike relational databases, DynamoDB stores data as Items inside Tables.
There are:
- No joins
- No foreign keys
- No fixed schema
It automatically scales to millions of requests without managing servers.
SQL vs NoSQL
| SQL Database | DynamoDB |
|---|---|
| Tables | Tables |
| Rows | Items |
| Columns | Attributes |
| Joins | Not Supported |
| Fixed Schema | Flexible Schema |
| Scale Vertically | Scale Horizontally |
| ACID Transactions | Supported (Limited Scope) |
DynamoDB Architecture
flowchart LR
USER[Client]
API[Spring Boot]
SDK[AWS SDK]
DDB[(Amazon DynamoDB)]
USER --> API
API --> SDK
SDK --> DDB
Enterprise Architecture
flowchart TD
Users
ALB
SpringBoot
DynamoDB
CloudWatch
Users --> ALB
ALB --> SpringBoot
SpringBoot --> DynamoDB
SpringBoot --> CloudWatch
DynamoDB Terminology
| Term | Description |
|---|---|
| Table | Collection of data |
| Item | One record |
| Attribute | Field inside item |
| Partition Key | Primary key |
| Sort Key | Optional second key |
| GSI | Secondary index |
| LSI | Local secondary index |
Example Table
Employee Table
| EmployeeId | Name | Department |
|---|---|---|
| 100 | Venu | Architecture |
| 101 | John | Cloud |
Each row is an Item.
Primary Key
Every table must have:
Partition Key
Example
EmployeeId
Composite Key
Partition Key
Sort Key
Example
CustomerId
OrderDate
This allows:
Customer 100
↓
All Orders
Real-Time Use Cases
DynamoDB is commonly used for:
- Shopping carts
- Login sessions
- IoT devices
- Mobile applications
- Chat messages
- Product catalog
- Gaming
- Event logs
Why DynamoDB?
Benefits:
- Fully managed
- Serverless
- Single-digit millisecond latency
- Auto Scaling
- Global Tables
- Streams
- Backup
- Encryption
High-Level Flow
flowchart LR
A[Browser]
B[Spring Boot]
C[AWS SDK]
D[DynamoDB]
A --> B
B --> C
C --> D
Prerequisites
Install:
Java 17
Maven
AWS CLI
Spring Boot
AWS SDK
Verify:
java -version
Verify AWS CLI:
aws --version
Step 1 Create DynamoDB Table
AWS Console
↓
DynamoDB
↓
Create Table
Table Name
employees
Partition Key
employeeId
Type
String
Billing
On Demand
Create Using AWS CLI
aws dynamodb create-table \
--table-name employees \
--attribute-definitions AttributeName=employeeId,AttributeType=S \
--key-schema AttributeName=employeeId,KeyType=HASH \
--billing-mode PAY_PER_REQUEST
Verify Table
aws dynamodb list-tables
Output
employees
Step 2 Spring Boot Project
Dependencies
- Spring Web
- Validation
- AWS SDK DynamoDB
Maven Dependency
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>dynamodb</artifactId>
<version>2.25.60</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
application.yml
aws:
region: us-east-1
dynamodb:
table-name: employees
Configure DynamoDbClient
@Configuration
public class DynamoConfig {
@Bean
public DynamoDbClient dynamoDbClient() {
return DynamoDbClient.builder()
.region(Region.US_EAST_1)
.credentialsProvider(DefaultCredentialsProvider.create())
.build();
}
}
Entity Example
public class Employee {
private String employeeId;
private String name;
private String department;
}
DynamoDB Item
{
"employeeId":"100",
"name":"Venu",
"department":"Architecture"
}
Repository Layer
Unlike JPA,
AWS SDK directly performs operations.
Example:
@Service
public class EmployeeService {
private final DynamoDbClient dynamoDbClient;
}
Insert Item
Flow
flowchart LR
API["API"]
SDK["AWS SDK"]
PUT["PutItem"]
DB["DynamoDB"]
API --> SDK
SDK --> PUT
PUT --> DB
Create API
POST
/employees
Input
{
"employeeId":"100",
"name":"Venu",
"department":"Architecture"
}
Output
{
"message":"Employee Created"
}
Read Item
GET
/employees/100
Output
{
"employeeId":"100",
"name":"Venu",
"department":"Architecture"
}
Update Item
PUT
/employees/100
Input
{
"name":"Venu Reddy",
"department":"Cloud"
}
Delete Item
DELETE
/employees/100
CRUD Flow
flowchart LR
A[Client]
B[Controller]
C[Service]
D[AWS SDK]
E[DynamoDB]
A --> B
B --> C
C --> D
D --> E
Query vs Scan
Query
Uses Primary Key
Fast
Recommended
Scan
Reads entire table
Slow
Expensive
Avoid in production.
Query Flow
graph LR
App["Application"] --> PK["Partition Key"]
PK --> DB["DynamoDB"]
Scan Flow
flowchart LR
A[Application]
B[Entire Table]
C[Results]
A --> B
B --> C
Global Secondary Index
Need to search by
Department
instead of
EmployeeId
Create
GSI
DepartmentIndex
Now Query
Cloud
without scanning.
Example GSI
Partition Key
department
Query
Architecture
↓
Returns all architects.
DynamoDB Streams
Whenever data changes
↓
Generate Stream Event
↓
Lambda
↓
SQS
↓
EventBridge
DynamoDB Streams Architecture
flowchart TD
DynamoDB
Stream
Lambda
SNS
SQS
DynamoDB --> Stream
Stream --> Lambda
Lambda --> SNS
Lambda --> SQS
TTL
Automatically delete expired records.
Useful for
- Sessions
- OTP
- Temporary data
Auto Scaling
On-Demand Mode
↓
Automatically scales
No capacity planning required.
Backup
Supports
- On-demand backup
- Continuous backup
- Point in Time Recovery
Security
Use
- IAM Roles
- Encryption
- KMS
- VPC Endpoint
Never expose credentials.
Monitoring
CloudWatch Metrics
Monitor
- Read Capacity
- Write Capacity
- Throttling
- Latency
- Errors
Production Architecture
flowchart TD
Users
SpringBoot
DynamoDB
Streams
Lambda
CloudWatch
Users --> SpringBoot
SpringBoot --> DynamoDB
DynamoDB --> Streams
Streams --> Lambda
SpringBoot --> CloudWatch
Common Errors
Resource Not Found
Wrong table name.
Access Denied
IAM permissions missing.
Validation Exception
Primary key missing.
Throttling
Increase capacity
or
Use On-Demand mode.
Best Practices
- Design partition keys carefully
- Avoid table scans
- Use Query instead of Scan
- Use GSIs for alternate access patterns
- Enable PITR
- Enable Encryption
- Use IAM Roles
- Monitor CloudWatch
- Use DynamoDB Streams
- Keep items small
- Avoid hot partitions
- Use Batch APIs when possible
DynamoDB vs Aurora
| Feature | Aurora | DynamoDB |
|---|---|---|
| Database Type | SQL | NoSQL |
| Joins | Yes | No |
| Schema | Fixed | Flexible |
| Scaling | High | Massive |
| Transactions | Full SQL | Limited |
| Best For | Enterprise OLTP | Key-Value & Document |
Interview Questions
What is DynamoDB?
A fully managed NoSQL database service from AWS.
What is a Partition Key?
Primary key used to distribute data across partitions.
Difference between Query and Scan?
Query uses keys.
Scan reads the entire table.
What is GSI?
Global Secondary Index enables querying using another attribute.
What is DynamoDB Streams?
A feature that captures table changes for downstream processing.
Why use On-Demand billing?
Automatically scales without capacity planning.
Does DynamoDB support ACID transactions?
Yes, using DynamoDB Transactions for supported operations.
Summary
In this article, we learned how to integrate Spring Boot with Amazon DynamoDB.
We covered:
- DynamoDB fundamentals
- Table creation
- Partition keys
- CRUD APIs
- Query vs Scan
- Global Secondary Index
- Streams
- Auto Scaling
- Security
- Monitoring
- Production best practices
Amazon DynamoDB is an excellent choice for high-scale Spring Boot applications that require predictable low latency, automatic scaling, and flexible schemas.
Comments
Share a question, correction, or practical insight about this article.
Checking login status...
Loading approved comments...