0
0
AWScloud~5 mins

Why DynamoDB for NoSQL in AWS - Why It Works

Choose your learning style9 modes available
Introduction
When you need a fast and flexible database that can handle lots of data without slowing down, DynamoDB is a great choice. It solves the problem of managing data that doesn't fit neatly into tables with fixed columns.
When your app needs to store user profiles with different fields for each user.
When you want a database that can quickly scale up or down based on how many people use your app.
When you need to handle sudden bursts of traffic without your database crashing.
When you want a fully managed database so you don't have to worry about servers or backups.
When you want to store session data or shopping cart information that changes often and fast.
Commands
This command creates a new DynamoDB table named 'Users' with a primary key called 'UserId'. It sets the read and write capacity to handle moderate traffic.
Terminal
aws dynamodb create-table --table-name Users --attribute-definitions AttributeName=UserId,AttributeType=S --key-schema AttributeName=UserId,KeyType=HASH --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
Expected OutputExpected
{ "TableDescription": { "TableName": "Users", "TableStatus": "CREATING", "ProvisionedThroughput": { "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 }, "TableSizeBytes": 0, "ItemCount": 0 } }
--table-name - Names the table you want to create
--attribute-definitions - Defines the data type of the key attribute
--key-schema - Sets the primary key for the table
This command checks the status and details of the 'Users' table to confirm it was created successfully.
Terminal
aws dynamodb describe-table --table-name Users
Expected OutputExpected
{ "Table": { "TableName": "Users", "TableStatus": "ACTIVE", "ProvisionedThroughput": { "ReadCapacityUnits": 5, "WriteCapacityUnits": 5 }, "ItemCount": 0 } }
--table-name - Specifies which table to describe
This command adds a new item to the 'Users' table with a user ID, name, and age.
Terminal
aws dynamodb put-item --table-name Users --item '{"UserId": {"S": "user123"}, "Name": {"S": "Alice"}, "Age": {"N": "30"}}'
Expected OutputExpected
{}
--table-name - Specifies the table to add the item to
--item - Defines the data to store in the table
This command retrieves the item with UserId 'user123' from the 'Users' table to verify it was stored correctly.
Terminal
aws dynamodb get-item --table-name Users --key '{"UserId": {"S": "user123"}}'
Expected OutputExpected
{ "Item": { "UserId": {"S": "user123"}, "Name": {"S": "Alice"}, "Age": {"N": "30"} } }
--table-name - Specifies the table to query
--key - Defines the primary key of the item to retrieve
Key Concept

If you remember nothing else from this pattern, remember: DynamoDB is a fast, flexible, and fully managed NoSQL database that scales automatically and handles different data shapes easily.

Common Mistakes
Not specifying the correct key schema when creating the table
Without the right key, DynamoDB cannot organize or find your data properly.
Always define the primary key attribute and its type clearly using --attribute-definitions and --key-schema.
Using too low read/write capacity units for expected traffic
This causes throttling, making your app slow or fail to save data.
Set provisioned throughput based on your app's needs or use on-demand mode for automatic scaling.
Incorrect JSON format in put-item or get-item commands
DynamoDB expects a specific JSON structure with data types, or the command will fail.
Use the correct attribute type notation like {"S": "string"} for strings and {"N": "number"} for numbers.
Summary
Create a DynamoDB table with a clear primary key and capacity settings.
Check the table status to ensure it is ready before adding data.
Add and retrieve items using JSON with explicit data types for each attribute.