AWSが提供するフルマネージド・サーバーレスのNoSQLデータベースサービス

出典: What is Amazon DynamoDB?

“Amazon DynamoDB is a fully managed, serverless, distributed NoSQL database that delivers single-digit millisecond performance at any scale.”

データモデル

キーバリュー型とドキュメント型の両方をサポート

出典: Amazon DynamoDB Features

“DynamoDB supports key-value and document data models and offers limitless scalability with built-in availability, durability, and fault tolerance.”

キーバリュー型の例

シンプルなキーと値のペア。ユーザーIDをキーにしてデータを取得するようなケース

{
  "UserId": "user123",
  "Name": "田中太郎",
  "Email": "tanaka@example.com"
}

パーティションキー UserId で一意にアイテムを特定し、高速に読み書きする

出典: Data Models - Comparing DynamoDB and HBase

“In DynamoDB, a single attribute primary key, or partition key, is useful for quick reads and writes.”

ドキュメント型の例

ネストしたJSON構造を持つデータ。リスト、マップなどの複雑なデータ型を格納できる

{
  "OrderId": "order-456",
  "CustomerId": "user123",
  "Items": [
    {
      "ProductId": "prod-001",
      "Name": "ノートPC",
      "Price": 120000,
      "Quantity": 1
    },
    {
      "ProductId": "prod-002", 
      "Name": "マウス",
      "Price": 3000,
      "Quantity": 2
    }
  ],
  "ShippingAddress": {
    "PostalCode": "100-0001",
    "City": "東京都",
    "Street": "千代田区1-1-1"
  },
  "Status": "shipped"
}

Items はリスト型、ShippingAddress はマップ型。RDBだと複数テーブルに分割するようなデータを1アイテムで表現できる

データモデルの使い分け

特徴用途
キーバリューフラットな構造、キーで即座にアクセスセッション管理、キャッシュ、ユーザープロファイル
ドキュメントネストした構造、柔軟なスキーマ注文データ、商品カタログ、コンテンツ管理

DynamoDBは両方を同じテーブルで扱えるので、ユースケースに応じて使い分けられる

基本構造

テーブル
  └── アイテム (RDBでいう行)
        └── 属性 (RDBでいう列)

出典: Core components of Amazon DynamoDB

“The core components of DynamoDB include tables, items, and attributes. A table is a collection of items, and each item is a group of attributes.”

テーブルはアイテムの集合、アイテムは属性の集合という階層構造になっている

具体例:

Users テーブル
  ├── アイテム1
  │     ├── UserId: "user001"      ← 属性 (パーティションキー)
  │     ├── Name: "田中太郎"        ← 属性
  │     └── Email: "tanaka@example.com"  ← 属性
  │
  └── アイテム2
        ├── UserId: "user002"
        ├── Name: "鈴木花子"
        ├── Email: "suzuki@example.com"
        └── Age: 28                ← アイテムごとに属性が異なってもOK (スキーマレス)

RDBと違い、アイテムごとに持つ属性が異なっていても問題ない(スキーマレス

Global Tables (マルチリージョン対応)

複数のAWSリージョンにDynamoDBテーブルを自動レプリケーションする機能。各リージョンでローカルに読み書きできるため、レイテンシ改善が見込める

出典: Replicate DynamoDB Across Regions - Amazon DynamoDB Global Tables

“Global tables allow users to access data with low latency from any location and provide automatic failover in case of processing interruptions.”

┌─────────────────┐         ┌─────────────────┐
│  東京リージョン   │◄───────►│  バージニア      │
│  (ap-northeast-1)│  自動    │  (us-east-1)    │
│                 │ レプリ   │                 │
│  App ─► DynamoDB│ ケーション│  App ─► DynamoDB│
└─────────────────┘         └─────────────────┘

整合性モード

モード特徴
MREC (Multi-Region Eventual Consistency)デフォルト。非同期レプリケーション、低レイテンシ
MRSC (Multi-Region Strong Consistency)同期レプリケーション、3リージョン必須、RPO=0

出典: How DynamoDB global tables work

“MREC is the default mode, with asynchronous replication and eventual consistency. MRSC offers synchronous replication and strong consistency, but requires deploying in exactly three Regions.”

エンドポイント

グローバルエンドポイントは存在しない。各リージョンのリージョナルエンドポイントにアクセスする

出典: Using DynamoDB global tables

“Global tables don’t have a global endpoint; all requests are made to a Regional endpoint.”

アプリ側の変更は基本的に不要。AWS SDKはデフォルトで実行環境のリージョンエンドポイントを使う

コンフリクト解決

複数リージョンで同じアイテムを更新した場合、Last Writer Wins (最後に書いた方が勝つ) で自動解決される

出典: Global tables: How it works

“Conflicts can arise when applications update the same item in different Regions, and DynamoDB uses a ‘last writer wins’ method to reconcile between concurrent updates”