☁️ CloudProjectHub

DynamoDB

What is Amazon DynamoDB?

Amazon DynamoDB is a fully managed NoSQL database service that provides fast and predictable performance with seamless scalability. It supports both document and key-value store models, and is designed for applications that need consistent, single-digit millisecond latency at any scale. DynamoDB handles all the administrative burdens of operating and scaling a distributed database.

Key Features

  • Serverless: No servers to manage or provision
  • Scalability: Automatic scaling based on throughput requirements
  • Performance: Single-digit millisecond latency
  • Durability: Data stored across multiple Availability Zones
  • Security: Encryption at rest and in transit
  • Integration: Works with other AWS services

Prerequisites

  • AWS Account
  • Understanding of NoSQL database concepts
  • Knowledge of partition keys and sort keys
  • IAM permissions for DynamoDB operations
  • Basic understanding of read/write capacity units

Step-by-Step: Creating a DynamoDB Table

  1. Sign in to AWS Management Console: Go to console.aws.amazon.com
  2. Navigate to DynamoDB: Search for "DynamoDB" in services
  3. Create Table: Click "Create table" button
  4. Table Details:
    • Table name: Enter a unique table name
    • Partition key: Primary key attribute (required)
    • Sort key: Optional secondary key for composite primary key
  5. Table Settings:
    • Capacity mode: On-demand or Provisioned
    • Read/write capacity: For provisioned mode (RCUs/WCUs)
    • Auto scaling: Enable for provisioned mode
  6. Secondary Indexes:
    • Global Secondary Index (GSI): For different access patterns
    • Local Secondary Index (LSI): Same partition key, different sort key
  7. Encryption: Enable encryption at rest (default: AWS owned key)
  8. Tags: Add key-value pairs for organization
  9. Create Table: Review and create the table

Adding Items to DynamoDB Table

  1. Navigate to Table: Select your table from the Tables list
  2. Create Item: Click "Create item" or "Explore items" → "Create item"
  3. Enter Attributes:
    • Partition key (required)
    • Sort key (if defined)
    • Additional attributes as needed
  4. Data Types: Choose appropriate types (String, Number, Boolean, etc.)
  5. Save Item: Click "Create item" to save

Querying and Scanning Data

  1. Query Operation: Use partition key and optional sort key conditions
  2. Scan Operation: Examine all items in the table (expensive, use sparingly)
  3. Use Indexes: Query GSIs and LSIs for different access patterns
  4. Filter Expressions: Apply additional filtering on results
  5. Pagination: Handle large result sets with pagination

Best Practices

  • Design partition keys for even distribution
  • Use sort keys to enable range queries
  • Choose appropriate capacity mode (On-demand vs Provisioned)
  • Use GSIs for alternative access patterns
  • Implement proper error handling and retries
  • Monitor performance with CloudWatch metrics
  • Use DynamoDB Streams for change data capture
  • Implement backup and restore strategies

Capacity Modes

  • On-Demand: Pay per request, automatic scaling
  • Provisioned: Set RCUs/WCUs, cost-effective for predictable workloads
  • Auto Scaling: Automatically adjust capacity based on usage

Data Types

  • Scalar Types: String, Number, Boolean, Null, Binary
  • Document Types: List, Map
  • Set Types: String Set, Number Set, Binary Set

Common Use Cases

  • Serverless web applications
  • Real-time gaming applications
  • IoT data ingestion and processing
  • User session management
  • Shopping cart and user preferences
  • Time-series data storage
  • Metadata storage for S3 objects