DynamoDB Basics
Introduction
Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. It is a key component of AWS's serverless architecture, allowing developers to build applications without worrying about the underlying infrastructure.
Key Concepts
- Tables: The primary structure for storing data.
- Items: Individual records in a table, similar to rows in a relational database.
- Attributes: Data fields associated with items, similar to columns.
- Primary Key: A unique identifier for each item, consisting of either a partition key or a composite key.
Data Model
DynamoDB uses a flexible schema, allowing you to easily change the structure of your data. There are two types of primary keys:
- Partition Key: A single attribute that uniquely identifies each item.
- Composite Key: Consists of two attributes: a partition key and a sort key.
Creating Tables
To create a DynamoDB table, follow these steps:
- Sign in to the AWS Management Console.
- Open the DynamoDB Console.
- Click on "Create Table".
- Define your table name and primary key structure.
- Configure settings such as read/write capacity and encryption.
- Click "Create" to provision the table.
Here’s a sample code snippet using the AWS SDK for JavaScript to create a table:
const AWS = require('aws-sdk');
const dynamoDB = new AWS.DynamoDB();
const params = {
TableName: 'MyTable',
KeySchema: [
{ AttributeName: 'id', KeyType: 'HASH' } // Partition key
],
AttributeDefinitions: [
{ AttributeName: 'id', AttributeType: 'S' } // String type
],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
}
};
dynamoDB.createTable(params, (err, data) => {
if (err) console.log("Error", err);
else console.log("Table Created", data);
});
Best Practices
- Use a well-thought-out primary key design to optimize data access.
- Monitor your table's performance and adjust read/write capacity as needed.
- Utilize DynamoDB Streams for real-time data processing.
- Implement fine-grained access control using IAM policies.
- Consider using Global Secondary Indexes (GSIs) to support additional query patterns.
FAQ
What is DynamoDB?
DynamoDB is a fully managed NoSQL database service provided by AWS, designed for high availability and performance.
How does DynamoDB scale?
DynamoDB automatically scales to accommodate workload demands, handling millions of requests per second with low latency.
What is the difference between a partition key and a composite key?
A partition key is a single attribute that uniquely identifies an item, while a composite key consists of both a partition key and a sort key.