Thuta Learning
AWS Advanced
Data at ScaleDevOpsအဆင့်မြင့်

DynamoDB Single-Table Design: Access Pattern ကနေ စတဲ့ Data Modeling

AWS Advancedသင်ခန်းစာ 2055 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Table မဆွဲခင် access pattern စာရင်းကို ရေးပြီး key design ကို အဲဒီကနေ ဆင်းသက်နိုင်ဖို့
  • CUSTOMER#123 / ORDER#2026-... လို composite key တွေနဲ့ item collection တွေ ဒီဇိုင်းဆွဲနိုင်ဖို့
  • GSI overloading, sparse index နဲ့ adjacency list ကို ဘယ်အချိန် သုံးရမလဲ သိဖို့
  • Hot partition ကို write sharding နဲ့ ဖြေရှင်းပြီး transaction နဲ့ conditional write ကို မှန်မှန်ကန်ကန် သုံးနိုင်ဖို့
  • Single-table design က မတန်တဲ့ အခြေအနေတွေကို ခွဲခြားနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

Relational database မှာ data ကို ပုံစံကျကျ သိမ်းပြီး မေးခွန်းတွေကို နောက်မှ JOIN နဲ့ စဉ်းစားပါတယ်။ DynamoDB မှာတော့ ပြောင်းပြန်ပါ: မေးမယ့် မေးခွန်းတွေကို အရင် ရေးပြီး အဖြေတွေကို request တစ်ခုတည်းနဲ့ ရအောင် data ကို ကြိုတင် စီထားပါတယ်။ ဒီ query တွေက data များလာလည်း latency မပြောင်းဘဲ မြန်နေတာ ဒါကြောင့်ပါ။

Single-table design ဆိုတာ customer, order, payment စတဲ့ entity အမျိုးမျိုးကို table တစ်ခုတည်းထဲမှာ ထားပြီး partition key (PK) နဲ့ sort key (SK) ကို 'CUSTOMER#123' လို prefix ပါတဲ့ string တွေနဲ့ ဆောက်တာပါ။ Customer တစ်ယောက်နဲ့ သူ့ order တွေက PK တူတူ ရှိလို့ Query တစ်ခုတည်းနဲ့ တစ်ပြိုင်နက် ပြန်ရပါတယ်။ AWS Fundamentals မှာ partition key တစ်ခုပါတဲ့ table ရိုးရိုး တစ်ခု ဆောက်ခဲ့ပါပြီ — အခု အဲဒါကို production schema အဖြစ် ချဲ့ပါမယ်။

Problem → Why → Solution

ပြဿနာ: relational ပုံစံနဲ့ DynamoDB ကို သုံးတာ

Team တစ်ခုက customers, orders, order_items ဆိုပြီး table သုံးခု ဆောက်ပြီး ID ကို partition key အဖြစ် သုံးပါတယ်။ 'Customer တစ်ယောက်ရဲ့ order နောက်ဆုံး ဆယ်ခု' ကို ပြဖို့ customer ကို GetItem၊ orders table ကို Scan နဲ့ FilterExpression၊ ပြီးရင် order တစ်ခုချင်းစီအတွက် items ကို ထပ်ခေါ်ရပါတယ်။

Data နည်းတုန်းက မြန်ပေမယ့် order သန်းချီ ဖြစ်လာတော့ Scan က table တစ်ခုလုံးကို ဖတ်ပြီး filter မလုပ်ခင် ဖတ်သမျှ အားလုံးအတွက် ငွေပေးရပါတယ်။ Page တစ်ခု load လုပ်တာ စက္ကန့်ချီ ကြာလာပြီး read capacity bill က တက်လာပါတယ်။

relational_style.py
import boto3
from boto3.dynamodb.conditions import Attr

orders = boto3.resource("dynamodb", region_name="us-east-1").Table("orders")


def recent_orders(customer_id: str) -> list:
    # Scan reads EVERY item in the table, then filters: cost grows with table size,
    # not with the size of the answer. (Pagination is ignored here, too.)
    resp = orders.scan(FilterExpression=Attr("customerId").eq(customer_id))
    return sorted(resp["Items"], key=lambda o: o["createdAt"], reverse=True)[:10]

ဘာကြောင့် ပြဿနာ ဖြစ်တာလဲ

DynamoDB မှာ JOIN မရှိပါဘူး၊ Scan ရဲ့ ကုန်ကျစရိတ်က table အရွယ်အစားနဲ့ ညီပါတယ်။ Key design က access pattern နဲ့ မကိုက်ရင် DynamoDB ရဲ့ အားသာချက် (data ဘယ်လောက်များများ latency တည်ငြိမ်တာ) ကို လုံးဝ ဆုံးရှုံးသွားပါတယ်။

အဓိက သဘောတရား: access pattern ကနေ key ဆီ

Access pattern စာရင်းရေး

'Customer profile ကို ID နဲ့ ယူ'၊ 'customer ရဲ့ order တွေကို ရက်စွဲအလိုက် ယူ'၊ 'pending order အားလုံး ယူ' စသဖြင့် application လုပ်မယ့် read/write တိုင်းကို ရေးပြီး ဘယ်လောက် မကြာခဏ ခေါ်လဲ မှတ်ပါ။

Entity တွေကို key ပေါ် map လုပ်

အတူတူ ဖတ်လေ့ရှိတဲ့ item တွေကို PK တူတူ ပေးပြီး SK ကို sort/filter လုပ်ချင်တဲ့ ပုံစံနဲ့ ဆောက်ပါ။

ကျန်တဲ့ pattern တွေကို GSI နဲ့ ဖြေ

Base table key နဲ့ မဖြေနိုင်တဲ့ pattern တွေအတွက် GSI1PK/GSI1SK လို generic attribute တွေနဲ့ GSI ထပ်ထည့်ပါ။

Item collection
Partition key တူတူ ရှိတဲ့ item အားလုံးရဲ့ အစုပါ။ SK အလိုက် စီထားလို့ Query တစ်ခုနဲ့ range တစ်ခုကို ဖတ်လို့ ရပါတယ်။

Order ဥပမာမှာ customer profile ကို PK=CUSTOMER#123, SK=PROFILE နဲ့၊ order တွေကို PK=CUSTOMER#123, SK=ORDER#2026-02-11#o-2 နဲ့ သိမ်းပါတယ်။ SK ထဲမှာ ရက်စွဲကို ISO format (YYYY-MM-DD) နဲ့ ထည့်ထားလို့ string sort က အချိန်အစဉ် ဖြစ်သွားပြီး begins_with(SK, 'ORDER#') က order တွေကိုပဲ၊ between က ရက်စွဲ range ကိုပဲ ရွေးပေးပါတယ်။ Query တစ်ခုက page တစ်ခုမှာ 1 MB အထိ ပြန်ပေးပြီး ScanIndexForward=false နဲ့ အသစ်ဆုံးကနေ စဖတ်နိုင်ပါတယ်။

GSI overloading ဆိုတာ GSI တစ်ခုတည်းရဲ့ GSI1PK/GSI1SK attribute တွေထဲကို entity အမျိုးအစား မတူရင် အဓိပ္ပာယ် မတူတဲ့ value တွေ ထည့်တာပါ — order မှာ STATUS#PENDING၊ customer မှာ EMAIL#... စသဖြင့်ပါ။ ဒါမှ GSI အနည်းငယ်နဲ့ pattern အများကြီးကို ဖြေနိုင်ပါတယ်။ Sparse index ဆိုတာ GSI key attribute ပါတဲ့ item တွေပဲ index ထဲ ဝင်တာကို အသုံးချတာပါ: pending order တွေမှာပဲ GSI1PK ထည့်ထားရင် index က ငယ်ငယ်လေး ဖြစ်ပြီး ဖတ်ရ ဈေးပေါပါတယ်။

Table တစ်ခုတည်း သုံးရတဲ့ တကယ့် အကြောင်းရင်းက table အရေအတွက် လျှော့ဖို့ မဟုတ်ပါဘူး။ Entity အမျိုးအစား မတူတဲ့ item တွေကို item collection တစ်ခုထဲ စုထားလို့ application က request တစ်ခုတည်းနဲ့ page တစ်ခုအတွက် လိုတာ အားလုံး ရတာပါ။ Item တစ်ခုရဲ့ အများဆုံး အရွယ်အစားက 400 KB ဖြစ်လို့ order တစ်ခုရဲ့ item တွေကို list attribute ကြီးတစ်ခုထဲ ထည့်မယ့်အစား item သီးခြားစီ ခွဲသိမ်းတာက ပိုလုံခြုံပါတယ်။

text
ONE TABLE, SEVERAL ENTITIES
---------------------------
  PK               SK                         GSI1PK          GSI1SK
  ---------------  -------------------------  --------------  ----------
  CUSTOMER#123     PROFILE                    -               -
  CUSTOMER#123     ORDER#2026-01-05#o-1       -  (shipped)    -
  CUSTOMER#123     ORDER#2026-02-11#o-2       STATUS#PENDING  2026-02-11
  CUSTOMER#123     ORDER#2026-03-02#o-3       STATUS#PENDING  2026-03-02
  EMAIL#mya@ex...  EMAIL                      -               -
  |<--- item collection for CUSTOMER#123 --->|  |<- sparse GSI1 ->|
စိတ်ထဲက ပုံ
  1. မေးခွန်းတွေ အရင် ရေး
  2. အတူဖတ်တာကို PK တူတူ ပေး
  3. SK ကို sort/range အတွက် ဆောက်
  4. ကျန်တာကို GSI နဲ့ ဖြေ

Access pattern အသစ် ပေါ်လာရင် GSI အသစ် ဒါမှမဟုတ် data backfill လိုနိုင်ပါတယ် — ဒါက single-table design ရဲ့ ပေးရတဲ့ တန်ဖိုးပါ။

Generic key နဲ့ table ဆောက်ပုံ

create-table.sh
cat > gsi.json <<'JSON'
[{"IndexName": "GSI1",
  "KeySchema": [{"AttributeName": "GSI1PK", "KeyType": "HASH"},
                {"AttributeName": "GSI1SK", "KeyType": "RANGE"}],
  "Projection": {"ProjectionType": "ALL"}}]
JSON

aws dynamodb create-table --table-name shop \
  --attribute-definitions AttributeName=PK,AttributeType=S \
    AttributeName=SK,AttributeType=S \
    AttributeName=GSI1PK,AttributeType=S AttributeName=GSI1SK,AttributeType=S \
  --key-schema AttributeName=PK,KeyType=HASH AttributeName=SK,KeyType=RANGE \
  --global-secondary-indexes file://gsi.json \
  --billing-mode PAY_PER_REQUEST \
  --stream-specification StreamEnabled=true,StreamViewType=NEW_AND_OLD_IMAGES

Key attribute တွေကို PK, SK, GSI1PK, GSI1SK လို generic နာမည်တွေ ပေးထားလို့ entity အသစ်တွေကို schema မပြောင်းဘဲ ထည့်နိုင်ပါတယ်။ Key မဟုတ်တဲ့ attribute တွေကို attribute-definitions ထဲ မထည့်ရပါဘူး — DynamoDB က schemaless ပါ။

လက်တွေ့ ဥပမာ: item collection, sparse GSI နဲ့ transaction

ဒီ script က shop table ထဲကို customer တစ်ယောက်နဲ့ order သုံးခု ရေးပါတယ်။ Pending order တွေမှာပဲ GSI1 key တွေ ထည့်ပါတယ်။ ပြီးရင် access pattern နှစ်ခုကို Query နဲ့ ဖြေပြီး email ကို unique ဖြစ်အောင် TransactWriteItems နဲ့ customer နှစ်ယောက် register လုပ်ကြည့်ပါတယ်။

single_table.py
import boto3
from boto3.dynamodb.conditions import Key

client = boto3.client("dynamodb", region_name="us-east-1")
table = boto3.resource("dynamodb", region_name="us-east-1").Table("shop")

# 1. Customer profile + orders share one partition: an item collection
table.put_item(Item={"PK": "CUSTOMER#123", "SK": "PROFILE", "name": "Aye Aye"})
orders = [
    ("2026-01-05#o-1", "SHIPPED", 40),
    ("2026-02-11#o-2", "PENDING", 75),
    ("2026-03-02#o-3", "PENDING", 18),
]
for date_id, status, total in orders:
    item = {"PK": "CUSTOMER#123", "SK": "ORDER#" + date_id, "status": status,
            "total": total}
    if status == "PENDING":  # sparse index: only pending orders get GSI1 keys
        item["GSI1PK"] = "STATUS#PENDING"
        item["GSI1SK"] = date_id
    table.put_item(Item=item)

# Pattern: "customer 123's orders from February 2026 on, newest first"
resp = table.query(
    KeyConditionExpression=Key("PK").eq("CUSTOMER#123")
    & Key("SK").between("ORDER#2026-02", "ORDER#2026-99"),
    ScanIndexForward=False,
)
print("recent orders:", [i["SK"] for i in resp["Items"]])

# Pattern: "all pending orders across customers", via the sparse GSI
resp = table.query(
    IndexName="GSI1", KeyConditionExpression=Key("GSI1PK").eq("STATUS#PENDING")
)
print("pending (GSI1):", [i["GSI1SK"] for i in resp["Items"]])


# 2. New customer + unique email claim: both writes or neither
def register(customer_id: str, email: str) -> str:
    def put_new(pk: str, sk: str, extra: dict) -> dict:
        item = {"PK": {"S": pk}, "SK": {"S": sk}, **extra}
        return {"Put": {"TableName": "shop", "Item": item,
                        "ConditionExpression": "attribute_not_exists(PK)"}}

    try:
        client.transact_write_items(TransactItems=[
            put_new("CUSTOMER#" + customer_id, "PROFILE", {"email": {"S": email}}),
            put_new("EMAIL#" + email, "EMAIL", {"customerId": {"S": customer_id}}),
        ])
        return "created"
    except client.exceptions.TransactionCanceledException as err:
        reasons = [r["Code"] for r in err.response["CancellationReasons"]]
        return "rejected " + str(reasons)


print("register 456:", register("456", "mya@example.com"))
print("register 789:", register("789", "mya@example.com"))

Browser ထဲမှာ run လို့ မရပါ

ဒီ code ဟာ AWS account အစစ်ပေါ်မှာ run ရမယ့် code ဖြစ်လို့ browser playground ထဲမှာ run လို့ မရပါ။ ကိုယ့် terminal (သို့) AWS CloudShell ထဲမှာ စမ်းပါ။

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Composite SK"SK": "ORDER#" + date_id

    ORDER# prefix က item type ကို ခွဲပေးပြီး ISO ရက်စွဲက sort order ကို ပေးပါတယ်။ ရက်စွဲပြီးရင် order ID ထည့်ထားလို့ တစ်ရက်တည်းမှာ order နှစ်ခု ရှိလည်း SK မထပ်ပါဘူး။

  2. Range queryKey("SK").between("ORDER#2026-02", "ORDER#2026-99")

    String နှိုင်းယှဉ်မှု ဖြစ်လို့ "ORDER#2026-99" က 2026 ထဲက ရက်စွဲတိုင်းထက် ကြီးပါတယ်။ PROFILE item က range ထဲ မဝင်လို့ ပြန်မလာပါဘူး၊ o-1 (ဇန်နဝါရီ) လည်း မပါပါဘူး။

  3. Sparse GSIitem["GSI1PK"] = "STATUS#PENDING"

    Shipped order o-1 မှာ GSI1PK မရှိလို့ index ထဲ မဝင်ပါဘူး။ Order ship ဖြစ်သွားရင် GSI1PK ကို REMOVE လုပ်လိုက်တာနဲ့ index ကနေ အလိုအလျောက် ထွက်သွားပါတယ်။

  4. Transaction"ConditionExpression": "attribute_not_exists(PK)"

    DynamoDB မှာ PK ကလွဲရင် unique constraint မရှိလို့ EMAIL#... item ကို 'lock' အဖြစ် သုံးပါတယ်။ ဒုတိယ register မှာ email item ရှိပြီးသားဖြစ်လို့ transaction တစ်ခုလုံး cancel ဖြစ်ပြီး CUSTOMER#789 လည်း မရေးပါဘူး။ Reason list က item တစ်ခုချင်းစီရဲ့ ရလဒ်ကို အစဉ်လိုက် ပြပါတယ်။

You should see
recent orders: ['ORDER#2026-03-02#o-3', 'ORDER#2026-02-11#o-2']
pending (GSI1): ['2026-02-11#o-2', '2026-03-02#o-3']
register 456: created
register 789: rejected ['None', 'ConditionalCheckFailed']

Adjacency list နဲ့ many-to-many

Many-to-many relationship (ဥပမာ employee တွေနဲ့ project တွေ) အတွက် adjacency list pattern ကို သုံးပါတယ်။ Relationship တစ်ခုစီကို PK=PROJECT#p1, SK=EMPLOYEE#e7 လို item တစ်ခုအဖြစ် သိမ်းပြီး GSI1PK=EMPLOYEE#e7, GSI1SK=PROJECT#p1 ဆိုပြီး key တွေကို ပြောင်းပြန်ထည့်ပါတယ် (inverted index)။ ဒါဆို 'project ထဲက employee တွေ' ကို base table ကနေ၊ 'employee ရဲ့ project တွေ' ကို GSI ကနေ Query တစ်ခုစီနဲ့ ဖြေနိုင်ပါတယ်။

Relationship item ထဲမှာ role, joinedAt လို edge data ကို ထည့်နိုင်ပြီး employee နာမည်လို မကြာခဏ ဖတ်ရတဲ့ field ကို denormalize လုပ်ပြီး ကူးထည့်လေ့ ရှိပါတယ်။ ဒါပေမယ့် နာမည် ပြောင်းရင် relationship item အားလုံး update လုပ်ရမှာမို့ မပြောင်းလဲတတ်တဲ့ data ကိုပဲ ကူးပါ။

Hot partition နဲ့ write sharding

Partition တစ်ခုက တစ်စက္ကန့်ကို read capacity unit 3,000 နဲ့ write capacity unit 1,000 လောက်ပဲ ခံနိုင်ပါတယ်။ Adaptive capacity က traffic များတဲ့ key တွေကို အလိုအလျောက် ခွဲထုတ်ပေးပေမယ့် key တစ်ခုတည်းကို ရေးတဲ့ traffic က အဲဒီ limit ကို ကျော်လို့ မရပါဘူး။ Election vote counter လို key တစ်ခုထဲကို တစ်စက္ကန့် ထောင်ချီ ရေးရင် throttle ဖြစ်ပါတယ်။

Write sharding က key ကို suffix တစ်ခုနဲ့ shard N ခု ခွဲပါတယ်။ Write တွေက shard တွေပေါ် ဖြန့်သွားပြီး read လုပ်တဲ့အခါ shard အားလုံးကို ဖတ်ပြီး ပေါင်းရပါတယ်။ Suffix ကို random မဟုတ်ဘဲ voter ID ရဲ့ hash ကနေ တွက်ရင် voter တစ်ယောက်ရဲ့ item ကို ဘယ် shard မှာလဲ ပြန်ရှာလို့ ရပါတယ်။

sharded_counter.py
import hashlib

import boto3

table = boto3.resource("dynamodb", region_name="us-east-1").Table("votes")
SHARDS = 10


def shard_for(voter_id: str) -> int:
    # Deterministic: the same voter always lands on the same shard
    return int(hashlib.sha256(voter_id.encode()).hexdigest(), 16) % SHARDS


def cast_vote(candidate: str, voter_id: str) -> None:
    table.update_item(
        Key={"PK": f"CANDIDATE#{candidate}#SHARD#{shard_for(voter_id)}", "SK": "COUNT"},
        UpdateExpression="ADD votes :one",
        ExpressionAttributeValues={":one": 1},
    )


def total_votes(candidate: str) -> int:
    keys = [
        {"PK": f"CANDIDATE#{candidate}#SHARD#{n}", "SK": "COUNT"} for n in range(SHARDS)
    ]
    resp = table.meta.client.batch_get_item(RequestItems={"votes": {"Keys": keys}})
    return sum(int(item["votes"]) for item in resp["Responses"]["votes"])


for i in range(200):
    cast_vote("aung", f"voter-{i}")

print("shards used:", len({shard_for(f"voter-{i}") for i in range(200)}))
print("total votes:", total_votes("aung"))

Capacity mode, conditional write နဲ့ Streams

On-demandProvisioned
ဈေးနှုန်းRead/write request အလိုက်Capacity unit per hour အလိုက်
သင့်တော်တဲ့ trafficမခန့်မှန်းနိုင်၊ spike များ၊ table အသစ်တည်ငြိမ်ပြီး ခန့်မှန်းနိုင်
Scalingအလိုအလျောက်Auto scaling ကို ကိုယ်တိုင် configure

Conditional write က item ရဲ့ လက်ရှိ state ကို စစ်ပြီးမှ ရေးပါတယ်။ Optimistic locking အတွက် item မှာ version number ထားပြီး update တိုင်းမှာ 'version = :expected' condition နဲ့ version ကို တစ် တိုးပါ — အခြား writer က အရင် ပြောင်းသွားရင် ConditionalCheckFailedException ရပြီး ပြန်ဖတ်ကာ ပြန်ကြိုးစားရပါတယ်။ TransactWriteItems က item အများဆုံး 100 ခုကို all-or-nothing ရေးပေးပေမယ့် item တစ်ခုစီအတွက် ပုံမှန် write ရဲ့ နှစ်ဆ capacity သုံးပါတယ်။

DynamoDB Streams က item ပြောင်းလဲမှုတိုင်းကို 24 နာရီ ထိန်းထားပြီး item တစ်ခုချင်းစီအတွက် အစဉ်လိုက် ပေးပါတယ်။ Lambda ESM နဲ့ ချိတ်ပြီး OrderPlaced event ကို EventBridge ဆီ ပို့တာ၊ aggregate counter တွေ update လုပ်တာ၊ OpenSearch ထဲ index လုပ်တာ စတာတွေ လုပ်ပါတယ် — application code က table ကိုပဲ ရေးပြီး side effect တွေကို stream က ကိုင်တွယ်ပါတယ်။

Production မှာ

Production မှာ ဘယ်လို သုံးကြလဲ

Single-table design ကို အောင်မြင်စွာ သုံးတဲ့ team တွေက access pattern စာရင်းနဲ့ entity တစ်ခုစီရဲ့ key ပုံစံကို spreadsheet ဒါမှမဟုတ် NoSQL Workbench model အဖြစ် repo ထဲမှာ ထိန်းထားပါတယ်။ PK/SK လို generic နာမည်တွေက code ကို ဖတ်ရခက်စေလို့ key builder function တွေ (customer_pk(id), order_sk(date, id)) ကို library တစ်ခုထဲမှာ စုထားပြီး string ကို လက်နဲ့ မဆောက်ပါဘူး။

  • Item တိုင်းမှာ entityType attribute ထားလို့ export၊ analytics နဲ့ debug လုပ်ရ လွယ်ပါတယ်။
  • Ad-hoc analytics ကို DynamoDB မှာ မလုပ်ဘဲ S3 export နဲ့ Athena ဒါမှမဟုတ် zero-ETL integration ကို သုံးပါတယ်။
  • ThrottledRequests နဲ့ CloudWatch Contributor Insights နဲ့ hot key တွေကို စောင့်ကြည့်ပါတယ်။
  • Point-in-time recovery ကို production table တိုင်းမှာ ဖွင့်ထားပါတယ်။

Microservice တစ်ခုချင်းစီက ကိုယ်ပိုင် table တစ်ခု (service တစ်ခု table တစ်ခု) ကို ပိုင်တာ အများဆုံး ဖြစ်ပါတယ် — 'single table' ဆိုတာ company တစ်ခုလုံး table တစ်ခု မဟုတ်ဘဲ service တစ်ခုရဲ့ entity တွေ table တစ်ခုထဲ ဆိုတဲ့ အဓိပ္ပာယ်ပါ။ တခြား service တွေက အဲဒီ data ကို ပိုင်ရှင် service ရဲ့ API ဒါမှမဟုတ် stream event တွေကနေတဆင့် ဖတ်ပါတယ်။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

အမှား ၁: cardinality နည်းတဲ့ partition key

status ဒါမှမဟုတ် ရက်စွဲ တစ်ခုတည်းကို partition key (ဒါမှမဟုတ် GSI partition key) အဖြစ် သုံးရင် traffic အားလုံး key အနည်းငယ်ပေါ် စုသွားပြီး hot partition ဖြစ်ပါတယ်။ ဒီ lesson ရဲ့ STATUS#PENDING GSI က pending order နည်းနည်းပဲ ရှိရင် အဆင်ပြေပေမယ့် တစ်စက္ကန့် ထောင်ချီ ရေးမယ်ဆိုရင် STATUS#PENDING#0..9 လို shard ခွဲရပါမယ်။ GSI throttle ဖြစ်ရင် base table ရဲ့ write တွေကိုပါ throttle ဖြစ်စေနိုင်ပါတယ်။

အမှား ၂: access pattern မသိဘဲ single table ကို သုံးတာ

Product အသစ် ရှာဖွေနေဆဲ အဆင့်မှာ query တွေ အပတ်တိုင်း ပြောင်းနေရင် key design တိုင်းက migration တစ်ခု ဖြစ်လာပါတယ်။ အဲဒီလို အခြေအနေ၊ ad-hoc reporting များတဲ့ app၊ ဒါမှမဟုတ် data နည်းပြီး team က SQL ကို ကျွမ်းကျင်တယ်ဆိုရင် Aurora/RDS ဒါမှမဟုတ် entity တစ်ခု table တစ်ခု ရိုးရိုးလေးက ပိုသင့်တော်ပါတယ်။ Single-table design က scale ကြီးပြီး access pattern တည်ငြိမ်တဲ့အခါမှ တန်ပါတယ်။

အမှား ၃: GSI ကို strongly consistent လို့ ထင်တာ

GSI တွေက eventually consistent read ပဲ ထောက်ပံ့ပါတယ်။ Order တစ်ခု ရေးပြီး ချက်ချင်း GSI ကို Query လုပ်ရင် မပါသေးနိုင်ပါဘူး။ Read-after-write လိုတဲ့ pattern တွေကို base table key နဲ့ ဖြေပါ။

💰 Cost & Cleanup

On-demand table တွေကို read/write request အလိုက်၊ storage ကို GB-month အလိုက် ကျသင့်ပါတယ်။ GSI တိုင်းက ကိုယ်ပိုင် storage နဲ့ write ခ ထပ်ယူလို့ projection ALL ရဲ့ ကုန်ကျစရိတ်ကို သတိထားပါ။ Transaction တွေက item တစ်ခုစီမှာ capacity နှစ်ဆ သုံးပါတယ်။ Streams ကို Lambda trigger က ဖတ်တာ အခမဲ့ ဖြစ်ပေမယ့် Lambda invocation ကတော့ ကျသင့်ပါတယ်။ Free tier မှာ storage 25 GB နဲ့ provisioned capacity အနည်းငယ် ပါပါတယ် (pricing page မှာ စစ်ပါ)။ ဒီ lab က item နည်းနည်းပဲ ဖြစ်လို့ cent ပိုင်းပဲ ကုန်မှာဖြစ်ပေမယ့် table တွေကို ဖျက်ပါ။

စမ်းပြီးရင် ဖျက်ဖို့ command

bash
aws dynamodb delete-table --table-name shop
aws dynamodb delete-table --table-name votes
aws dynamodb wait table-not-exists --table-name shop

Mini challenge

Mini challenge: order item တွေ ထည့်

Order တစ်ခုရဲ့ line item တွေကို table ထဲ ထည့်ပြီး 'order o-2 နဲ့ သူ့ item အားလုံး' ကို Query တစ်ခုတည်းနဲ့ ယူနိုင်အောင် key design လုပ်ပါ။

  • Customer ရဲ့ order list query (PK=CUSTOMER#123) က ပြောင်းလဲမှု မရှိရဘူး
  • Order header နဲ့ item တွေ item collection တစ်ခုတည်းထဲ ရှိရမယ် (base table ဒါမှမဟုတ် GSI)
  • Query တစ်ခုက header ကို item တွေရဲ့ ရှေ့မှာ ပြန်ပေးရမယ်

ရလဒ်: GSI2 ကို GSI2PK=ORDER#o-2 နဲ့ Query လုပ်ရင် header item တစ်ခုနဲ့ ITEM#001, ITEM#002 ကို အစဉ်လိုက် ပြန်ရပါတယ်။

အရိပ်အမြွက် ကြည့်ရန်

Item တွေကို PK=ORDER#o-2, SK=ITEM#001 နဲ့ ထားပြီး order header ကို GSI2PK=ORDER#o-2, GSI2SK=#HEADER နဲ့ overload လုပ်ကြည့်ပါ။ '#' က digit နဲ့ စာလုံးတွေထက် အရင် sort ဖြစ်ပါတယ်။

အဖြေ ကြည့်ရန်
order_items.py
import boto3
from boto3.dynamodb.conditions import Key

table = boto3.resource("dynamodb", region_name="us-east-1").Table("shop")

# Header stays in the customer's collection, and also joins the order's GSI2 group
table.put_item(Item={
    "PK": "CUSTOMER#123", "SK": "ORDER#2026-02-11#o-2",
    "GSI2PK": "ORDER#o-2", "GSI2SK": "#HEADER", "total": 75,
})
for n, sku in [("001", "tea-leaf"), ("002", "longyi")]:
    table.put_item(Item={
        "PK": "ORDER#o-2", "SK": "ITEM#" + n,
        "GSI2PK": "ORDER#o-2", "GSI2SK": "ITEM#" + n, "sku": sku,
    })

resp = table.query(IndexName="GSI2", KeyConditionExpression=Key("GSI2PK").eq("ORDER#o-2"))
print([i["GSI2SK"] for i in resp["Items"]])

Quick Quiz 1 / 3

PK=CUSTOMER#123 ရဲ့ item collection ထဲမှာ PROFILE နဲ့ ORDER#... item တွေ ရှိပါတယ်။ Order တွေကိုပဲ ယူဖို့ ဘယ် Query က အကောင်းဆုံးလဲ။

Quick Quiz 2 / 3

Vote counter item တစ်ခုက တစ်စက္ကန့် write ၅,၀၀၀ ရပြီး throttle ဖြစ်နေပါတယ်။ ဘာက ပြဿနာကို ဖြေရှင်းမလဲ။

Quick Quiz 3 / 3

Single-table design က မတန်တဲ့ အခြေအနေ ဘယ်ဟာလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Access pattern စာရင်းကနေ စပြီး key design ကို အဲဒီကနေ ဆင်းသက်ပါ။
  • PK တူတူ ပေးထားတဲ့ item collection တွေနဲ့ entity အမျိုးမျိုးကို Query တစ်ခုတည်းနဲ့ ဖတ်ပါ။
  • GSI overloading နဲ့ sparse index က GSI အနည်းငယ်နဲ့ pattern အများကြီးကို ဖြေပေးပါတယ်။
  • Hot key တွေကို write sharding နဲ့၊ unique constraint တွေကို transaction + condition နဲ့ ကိုင်တွယ်ပါ။
  • Access pattern မတည်ငြိမ်ရင် single-table design ကို မသုံးပါနဲ့။

Best practices for designing with DynamoDB — AWS Documentation

DynamoDB transactions — AWS Documentation

DynamoDB Streams — AWS Documentation

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

DynamoDB Single-Table Design: Access Pattern ကနေ စတဲ့ Data Modeling | Thuta Learning