DynamoDB Tables –
Overview
Amazon DynamoDB is a fully
managed NoSQL database service designed for high
performance, scalability, and flexibility.
Key concepts for tables:
1.
Table
- A collection of items.
- Items are the records; attributes are the
fields.
- Every table must have a primary key (either simple or composite).
2.
Primary Key Types
Type |
Structure |
Use Case |
Simple Primary Key. |
Partition Key only (PK) |
When each item is uniquely
identified by one attribute |
Composite Primary Key. |
Partition Key (PK) + Sort Key (SK) |
When you need multiple related items
per partition, sorted by SK |
3.
Indexes
- Global Secondary Index (GSI) → Alternate PK/SK for queries unrelated
to the main table’s key.
- Local Secondary Index (LSI) → Same PK as the table but a different
SK for sorting/filtering.
4. Data
Model
- Items are schema-less — attributes
can vary between items.
- You can store:
- Strings, Numbers, Booleans
- Lists, Maps (nested JSON-like)
- Binary data
5.
Partitioning
- DynamoDB automatically splits tables into
partitions based on PK ( Public Key) hash.
- The goal: evenly distribute reads/writes.
Sample
Table Designs
Example
1 – Orders Table (E-commerce)
Primary Key:
- PK: OrderId (string)
- SK: none (simple PK)
Attributes:
- CustomerId
- OrderDate
- Status
- Items (list of maps)
- TotalAmount
GSI1:
- PK: CustomerId
- SK: OrderDate
Allows: “Get all orders for a customer, sorted by date”
Example
2 – IoT Device Readings
Primary Key:
- PK: DeviceId
- SK: Timestamp (ISO 8601)
Attributes:
- Temperature
- Humidity
- BatteryLevel
GSI1:
- PK: Timestamp (for time-based aggregation
across devices)
- SK: none
Sample Item:
# json
{
"DeviceId": "SENSOR-001",
"Timestamp": "2025-08-11T15:00:00Z",
"Temperature": 23.5,
"Humidity": 45,
"BatteryLevel": 88
}
Example 3 – User
Profiles with Flexible Attributes
Primary Key:
- PK: UserId (string)
Attributes:
- Name
- Email
- Preferences (map)
- JoinedDate
No sort key — each user is a single item.
Sample Item:
# json
{
"UserId": "U1001",
"Name": "twtech",
"Email": "twtech671@gmail.com",
"Preferences": {
"Theme": "Dark",
"Notifications": true
},
"JoinedDate": "2024-05-12"
}
Here are twtech sample of DynamoDB tables …showing the key structures and attributes defined.
Example:
Orders Table
Attribute
Name |
Type |
Role |
Notes |
OrderId. |
String. |
Partition Key (PK). |
Uniquely identifies the order.
Example: ORD12345 |
CustomerId. |
String. |
Sortable/secondary attribute. |
Could be used in a Global Secondary
Index (GSI) |
OrderDate. |
String. |
ISO 8601 date/time string. |
Example: 2025-08-11T15:30:00Z |
Status. |
String. |
Attribute. |
Example values: Pending, Shipped, Delivered |
Items. |
List<Map>. |
Attribute. |
Each item has ProductId, Quantity, Price |
TotalAmount. |
Number. |
Attribute. |
Example: 149.99 |
Primary
Key Design
- Partition Key: OrderId
- No Sort Key in this example (simple
primary key).
If twtech needs multiple orders per customer sorted by date, it could design: - Partition Key: CustomerId
- Sort Key: OrderDate
Sample
Item JSON
# json
{
"OrderId": "ORD12345",
"CustomerId": "CUST987",
"OrderDate":
"2025-08-11T15:30:00Z",
"Status": "Shipped",
"Items": [
{ "ProductId": "P1001",
"Quantity": 2, "Price": 25.00 },
{ "ProductId": "P2005",
"Quantity": 1, "Price": 99.99 }
],
"TotalAmount": 149.99
}
Optional
Index
If twtech often query by customer:
- Global Secondary Index (GSI)
- Partition Key: CustomerId
- Sort Key: OrderDate
- Lets you query: "All orders for CUST987
in the last 30 days"
DynamoDB Table: Orders
# yaml
┌──────────────────────────────┐
│
DynamoDB Table:
Orders
│
└──────────────────────────────┘
Partition Key:
OrderId
Attributes:
CustomerId,
OrderDate,
Status,
Items,
TotalAmount
├──────────────────┤
├────── ─────────┤
├────────────────────────────┤
│
Partition
1
│
│
Partition
2 │
│
Partition
3
│
│
(Hash(OrderId)
→
P1)
│
│(Hash(OrderI
P2) │ │ (Hash(OrderId
P3)
│
├──────────────────┤
├────── ─────────┤
├────────────────────────────┤
├──────────────────┤
├────── ─────────┤
├────────────────────────────┤
│
OrderId:
ORD12345
│
│
OrderId:
ORD54321
│
│
OrderId:
ORD67890
│
│
CustomerId:
CUST987
│
│
CustomerId:
CUST111 │
│
CustomerId:
CUST222 │
│
OrderDate:
2025-08-11T15:30│
│
OrderDate:
2025-08-09T10:00││
OrderDate:
2025-08-08T09:15 │
│
Status:
Shipped
│
│
Status:
Pending
│
│
Status:
Delivered
│
│
Items: [
...]
│
│
Items: [
...]
│
│
Items: [
...]
│
│
TotalAmount:
149.99
│
│
TotalAmount:
50.00
│
│
TotalAmount:
300.50
│
├──────────────────┤
├────── ─────────┤
├────────────────────────────┤
Global Secondary Index (GSI1)
- Partition Key:
CustomerId
- Sort Key:
OrderDate
# yaml
┌────────────────────────────────────────────┐
│
Global Secondary Index:
GSI1
│
└────────────────────────────────────────────┘
PK
=
CustomerId,
SK
=
OrderDate
Lets you query:
"Orders for a given customer by date"
┌─────────────────────────────────────────┐
│
CustomerId:
CUST987
│
│
2025-08-11T15:30
→
OrderId:
ORD12345
│
├─────────────────────────────────────────┤
│
CustomerId:
CUST111
│
│
2025-08-09T10:00
→
OrderId:
ORD54321
│
├─────────────────────────────────────────┤
│
CustomerId:
CUST222
│
│
2025-08-08T09:15
→
OrderId:
ORD67890
│
└─────────────────────────────────────────┘
How it works
1. Partitioning
DynamoDB distributes items into partitions using the hash
value of the Partition Key (OrderId
).
2. GSI (Global Secondary Index)
The GSI creates an alternate sorted view of the table using CustomerId
and OrderDate
for fast
queries by customer over time.
3. Scaling
As data grows, DynamoDB automatically adds partitions behind the scenes.
Example
1 – Orders Table (E-commerce)
# yaml
MAIN TABLE:
Orders GSI1: CustomerId
+ OrderDate
(PK = OrderId) (PK = CustomerId, SK = OrderDate)
│ OrderId: ORD12345 │ │ CustomerId: CUST987 │
│ CustomerId: CUST987│ │ 2025-08-11T15:30 → ORD12345 │
│ OrderDate: 2025-08-11T15:30 │
├───────────────────────────────────────┤
├───────────────────────────────────────┤
│ Status: Shipped │ │ CustomerId: CUST111 │
│ Items: [...] │ │ 2025-08-09T10:00
→ ORD54321 │
│ TotalAmount: 149.99 │
├───────────────────────────────────────┤
├────────────────────────────────┤
│ OrderId: ORD54321 │
│ CustomerId: CUST111 │
│ OrderDate: 2025-08-09T10:00 │
│ Status: Pending │
│ Items: [...] │
│ TotalAmount: 50.00 │
├─────────────────────────────┤
Example 2 – IoT Device Readings
# yaml
MAIN TABLE:
DeviceReadings GSI1: Timestamp
(PK = DeviceId, SK = Timestamp) (PK = Timestamp, SK = none)
├────────────────────────────────────────────┤
│ DeviceId: SENSOR-001 │ │ Timestamp: 2025-08-11T15:00 │
│ Timestamp: 2025-08-11T15:00 │ │ DeviceId: SENSOR-001 │
│ Temp: 23.5 │
├────────────────────────────────────────────┤
│ Humidity: 45 │ │ Timestamp: 2025-08-11T15:00 │
│ Battery: 88 │ │ DeviceId:
SENSOR-002 │
├────────────────────────────────────────────┤
┌────────────────────────────────┐
│ DeviceId: SENSOR-002 │
│ Timestamp: 2025-08-11T15:00 │
│ Temp: 24.0 │
│ Humidity: 50 │
│ Battery: 90 │
└────────────────────────────────┘
Example
3 – User Profiles
# yaml
MAIN TABLE: Users // No GSI (Global Secondary Index) for this example
(PK = UserId)
┌────────────────────────────────┐
│ UserId: U1001 │
│ Name: Foncha Robert │
│ Email: foncha@gmail.com │
│ Preferences: {Theme: Dark} │
│ JoinedDate: 2024-05-12 │
├────────────────────────────────┤
│ UserId: U1002 │
│ Name: Eliza Abunga │
│ Email: abunga@gmail.com │
│ Preferences: {Theme: Light} │
│ JoinedDate: 2025-01-20 │
└────────────────────────────────┘
What This Shows
- Main Table contains the full item data.
- GSI (Global Secondary Index) is a projection:
- Uses a different PK/SK.
- Can contain all attributes (if ProjectionType
= ALL) or just keys + select attributes.
- Items in the GSI point back to the
same data in the main table.
No comments:
Post a Comment