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

Aurora နဲ့ RDS Proxy: Relational Database ကို Scale ချဲ့ခြင်း

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

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

  • Aurora cluster volume (AZ သုံးခုမှာ copy ခြောက်ခု) နဲ့ writer/reader instance တွေ ဘယ်လို အလုပ်လုပ်လဲ ရှင်းပြနိုင်မယ်
  • Cluster endpoint, reader endpoint, custom endpoint တွေကို မှန်ကန်တဲ့ traffic အတွက် ရွေးသုံးနိုင်မယ်
  • Lambda + relational database မှာ connection storm ဘာကြောင့်ဖြစ်လဲ၊ RDS Proxy က IAM auth နဲ့ Secrets Manager ကိုသုံးပြီး ဘယ်လိုဖြေရှင်းလဲ ရှင်းပြနိုင်မယ်
  • Read replica နဲ့ Multi-AZ က မတူညီတဲ့ ပြဿနာတွေကို ဖြေရှင်းတယ်ဆိုတာ ခွဲခြားနိုင်မယ်
  • Aurora Serverless v2 နဲ့ Aurora Global Database ကို ဘယ်အချိန်သုံးသင့်လဲ ဆုံးဖြတ်နိုင်မယ်

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

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

AWS Fundamentals မှာ RDS instance တစ်ခု launch လုပ်ခဲ့ပြီးပါပြီ — ကိုယ်ပိုင် disk ပါတဲ့ database server တစ်လုံးပါ။ Aurora ကတော့ ရင်းနှီးပြီးသား MySQL နဲ့ PostgreSQL engine တွေကို ဆက်သုံးထားပေမယ့် အောက်ခံ storage ကို ပြောင်းလဲထားပါတယ်။ Server တစ်လုံးချင်းစီမှာ ကိုယ်ပိုင် disk ရှိမယ့်အစား Aurora cluster ထဲက server အားလုံးက Availability Zone သုံးခုပေါ် AWS ဖြန့်ထားတဲ့ storage volume တစ်ခုတည်းကို မျှသုံးကြပါတယ်။

စာကြည့်တိုက်တစ်ခုနဲ့ ဥပမာပေးကြည့်ရအောင်။ Classic RDS က librarian တစ်ယောက်ချင်းစီကို စာအုပ်တွေရဲ့ ကိုယ်ပိုင် copy ပေးထားသလိုပါပဲ။ Aurora ကတော့ ကောင်းကောင်းစောင့်ရှောက်ထားတဲ့ စာအုပ်စု တစ်စုတည်းကို အလယ်မှာထားပြီး librarian အများအပြားကို ဖတ်ခွင့်ပေးတယ်၊ ဒါပေမဲ့ ရေးခွင့်ကိုတော့ တစ်ယောက်တည်းကိုပဲ ပေးထားပါတယ်။ RDS Proxy ကတော့ ရှေ့ကောင်တာပါ — ဧည့်သည်တွေက librarian အားလုံးဆီ တစ်ပြိုင်နက် တိုးဝင်မယ့်အစား အဲဒီမှာ တန်းစီကြပါတယ်။

Problem → Why → Solution

ပြဿနာ: Traffic တက်လာတာနဲ့ connection တွေ ပေါက်ကွဲသွားခြင်း

Lambda နဲ့ RDS PostgreSQL instance တစ်ခုတည်းပေါ်မှာ တည်ဆောက်ထားတဲ့ order API တစ်ခုကို စဉ်းစားကြည့်ပါ။ ပုံမှန်နေ့မှာ concurrent function ၂၀ လောက်က database connection တစ်ခုစီဖွင့်ပြီး အားလုံး အဆင်ပြေပါတယ်။ ပြီးတော့ marketing email တစ်စောင် ထွက်သွားတယ်၊ concurrency က ၈၀၀ အထိ ခုန်တက်သွားတယ်၊ execution environment အသစ်တိုင်းက cold start အတွင်းမှာ ကိုယ်ပိုင် connection တစ်ခုစီ ဖွင့်ကြပါတယ်။

PostgreSQL ရဲ့ max_connections ကို instance memory အလိုက် သတ်မှတ်ထားတာမို့ limit ပြည့်သွားတာနဲ့ connection အသစ်တွေက 'too many clients' error နဲ့ fail ဖြစ်ပါတယ်။ Lambda retry တွေက ပိုဆိုးအောင် လုပ်ပြီး database ကလည်း query run မယ့်အစား connection တွေ စီမံဖို့ memory နဲ့ CPU ကုန်နေပါတယ်။ တစ်ချိန်တည်းမှာ reporting team က အဲဒီ instance ပေါ်မှာပဲ query အကြီးတွေ run နေလို့ checkout ကပါ နှေးသွားပါတယ်။

bad_handler.py
import os


def lambda_handler(event, context):
    import psycopg  # bundled in the deployment package

    # BAD: a brand-new TCP + TLS + auth handshake on EVERY invocation,
    # and nothing closes it. 800 concurrent environments = 800+ sessions
    # hitting the database directly, plus a plaintext password in env vars.
    conn = psycopg.connect(
        host=os.environ["DB_HOST"],
        user="app_user",
        password=os.environ["DB_PASSWORD"],
    )
    with conn.cursor() as cur:
        cur.execute("SELECT status FROM orders WHERE id = %s", (event["id"],))
        return {"status": cur.fetchone()[0]}

Connection တွေက ဈေးကြီးပြီး Lambda က အဲဒါကို အဆမတန် များစေတယ်

Relational connection တစ်ခုစီက database server ပေါ်မှာ process ဒါမှမဟုတ် thread တစ်ခုနဲ့ memory ကုန်ကျပြီး ဖွင့်ဖို့ TCP, TLS, authentication handshake တွေ လိုပါတယ်။ Lambda က execution environment တွေ ထပ်ထည့်ပြီး scale လုပ်တာဖြစ်လို့ environment တစ်ခုစီက ကိုယ်ပိုင် connection ကိုင်ထားပါတယ်။ ဒါကြောင့် database ဘက်က မြင်ရတဲ့ connection အရေအတွက်ဟာ query load အစစ်အတိုင်း မဟုတ်ဘဲ concurrency နဲ့အတူ တက်လာပါတယ်။

အဓိက သဘောတရား: Shared storage နဲ့ pooled connection

Cluster volume
Aurora cluster တစ်ခုအတွက် shared storage volume တစ်ခုတည်းပါ။ AWS က data အပိုင်းတိုင်းကို Availability Zone သုံးခုပေါ်မှာ copy ခြောက်ခု သိမ်းထားပြီး cluster ထဲက instance အားလုံးက ဒီ volume တစ်ခုတည်းကနေ ဖတ်ကြပါတယ်။

Aurora က database ကို layer နှစ်ခု ခွဲထားပါတယ်။ Compute layer မှာ writer instance တစ်ခုနဲ့ Aurora Replica ၁၅ ခုအထိ ရှိနိုင်ပြီး MySQL- ဒါမှမဟုတ် PostgreSQL-compatible engine ကို run ပါတယ်။ Storage layer ကတော့ cluster volume ပါ — data ကို segment အသေးလေးတွေ ခွဲပြီး segment တစ်ခုစီကို ခြောက်ကြိမ် သိမ်းပါတယ်၊ AZ သုံးခုမှာ AZ တစ်ခုကို copy နှစ်ခုစီပါ။

Write တစ်ခုက copy ခြောက်ခုထဲက လေးခု acknowledge လုပ်တာနဲ့ အောင်မြင်ပြီး read အတွက်ကတော့ သုံးခု လိုပါတယ်။ ဒီ quorum ကြောင့် AZ တစ်ခုလုံး ပျက်သွားရင်တောင် Aurora က ဆက်ရေးနိုင်ပြီး AZ တစ်ခုနဲ့ နောက်ထပ် copy တစ်ခု ပျောက်သွားရင်တောင် ဆက်ဖတ်နိုင်ပါတယ်။ Writer က data page အပြည့်အစုံ မဟုတ်ဘဲ redo log record တွေကိုပဲ storage ဆီ ပို့တာမို့ disk ကို mirror လုပ်တဲ့ classic engine ထက် write traffic အများကြီး နည်းပါတယ်။

Replica တွေက data ကို copy မကူးပါဘူး၊ တူညီတဲ့ volume ကိုပဲ ဖတ်ကြတာပါ။ ဒါကြောင့် database ဘယ်လောက်ကြီးကြီး replica အသစ်တစ်ခုက မိနစ်ပိုင်းအတွင်း အသင့်ဖြစ်ပြီး replica lag ကလည်း ပုံမှန်အားဖြင့် 100 milliseconds အောက်မှာပဲ ရှိပါတယ်။ Failover ဖြစ်တဲ့အခါ Aurora က promotion tier အလိုက် replica တစ်ခုကို writer အဖြစ် promote လုပ်ပြီး cluster endpoint ကို အဲဒီဆီ ပြန်ညွှန်ပါတယ်။ Storage ကို မျှသုံးထားလို့ catch up လုပ်ရမယ့် data မရှိပါဘူး။

Replica မရှိရင်တော့ Aurora က writer instance အသစ် ဖန်တီးရမှာမို့ အချိန် အများကြီး ပိုကြာပါတယ်။ ဒါကြောင့် production cluster တွေမှာ AZ တစ်ခုခြားမှာ replica အနည်းဆုံး တစ်ခု ထားကြပါတယ်။

Endpointဘာအတွက်သုံးလဲ
Cluster (writer) endpointလက်ရှိ writer ကို အမြဲညွှန်တယ်၊ failover ဖြစ်ရင် လိုက်ပြောင်းတယ်။ Write နဲ့ read-after-write အားလုံးအတွက်
Reader endpointConnection အသစ်တွေကို replica တွေကြား ဖြန့်ပေးတယ် (query တစ်ခုချင်းစီ မဟုတ်)
Custom endpointကိုယ်ရွေးထားတဲ့ instance အုပ်စု၊ ဥပမာ analytics အတွက် replica အကြီးတွေ
Instance endpointInstance တစ်ခုတည်း၊ failover ကို မလိုက်ဘူး — application ထဲမှာ ရှောင်ပါ

RDS Proxy က application နဲ့ database ကြားမှာ ရှိပါတယ်။ Database connection pool တစ်ခုကို ဖွင့်ထားပြီး client တွေကို transaction တစ်ခုစာ ငှားပေးတာမို့ client connection ထောင်နဲ့ချီက database connection ဆယ်ဂဏန်းလောက်ကို မျှသုံးနိုင်ပါတယ်။ Database credential တွေကို Secrets Manager ကနေ ယူတယ်၊ client တွေဆီက IAM authentication ကို မဖြစ်မနေ တောင်းနိုင်တယ်၊ failover အတွင်းမှာ client connection တွေကို ဖွင့်ထားရင်း writer အသစ်ဆီ ပြန်ချိတ်ပေးပါတယ်။

text
AURORA CLUSTER BEHIND RDS PROXY
-------------------------------
  Lambda x 800        ECS tasks          Reporting job
      |                   |                    |
      v                   v                    v
 +-----------------------------+      (reader endpoint)
 | RDS Proxy  (pool of ~40     |               |
 | DB connections, IAM auth)   |               |
 +-----------------------------+               |
      | cluster endpoint                       |
      v                                        v
 +----------+     +------------+     +------------+
 |  Writer  |     | Replica 1  |     | Replica 2  |
 |  AZ-a    |     | AZ-b tier0 |     | AZ-c tier1 |
 +----------+     +------------+     +------------+
      |  redo log         |  reads          |  reads
      v                   v                 v
 +-----------------------------------------------------+
 | Cluster volume: 6 copies (2 per AZ, AZ-a/b/c)        |
 | write quorum 4 of 6, read quorum 3 of 6              |
 +-----------------------------------------------------+
Mental model: storage က မျှသုံး၊ compute က လဲလှယ်လို့ရ
  1. App connects to RDS Proxy
  2. Proxy lends a pooled connection
  3. Writer logs redo to 6 copies / 3 AZs
  4. Replicas read the same cluster volume
  5. Failover promotes a replica

Instance တွေက လဲလှယ်လို့ရတဲ့ compute သက်သက်ပါ။ Data အစစ်က cluster volume ထဲမှာ ရှိတာမို့ replica ထပ်ထည့်တာ၊ failover လုပ်တာ မြန်ပါတယ်။

Pattern: RDS Proxy နောက်ကွယ်က Aurora Serverless v2 cluster

create-aurora.sh
# 1. Aurora PostgreSQL cluster; RDS keeps the admin password in Secrets Manager
aws rds create-db-cluster \
  --db-cluster-identifier orders \
  --engine aurora-postgresql \
  --master-username app_admin \
  --manage-master-user-password \
  --serverless-v2-scaling-configuration MinCapacity=0.5,MaxCapacity=16 \
  --db-subnet-group-name orders-db-subnets \
  --vpc-security-group-ids sg-0db1a2b3c4d5e6f70 \
  --storage-encrypted \
  --enable-iam-database-authentication

# 2. A writer and one reader (add --availability-zone to pin each to its own AZ)
for name in orders-writer orders-reader-1; do
  aws rds create-db-instance \
    --db-instance-identifier "$name" \
    --db-cluster-identifier orders \
    --engine aurora-postgresql \
    --db-instance-class db.serverless
done

# 3. RDS Proxy logs in with the app user's secret; clients need IAM auth + TLS
aws rds create-db-proxy \
  --db-proxy-name orders-proxy \
  --engine-family POSTGRESQL \
  --auth AuthScheme=SECRETS,SecretArn=$APP_USER_SECRET_ARN,IAMAuth=REQUIRED \
  --role-arn arn:aws:iam::111122223333:role/orders-proxy-secrets \
  --vpc-subnet-ids subnet-0a1b2c3d4e5f67890 subnet-0f9e8d7c6b5a43210 \
  --vpc-security-group-ids sg-0db1a2b3c4d5e6f70 \
  --require-tls

aws rds register-db-proxy-targets \
  --db-proxy-name orders-proxy \
  --db-cluster-identifiers orders

db.serverless instance class က Serverless v2 ပါ၊ capacity ကို MinCapacity နဲ့ MaxCapacity (ACU) ကြားမှာ အလိုအလျောက် ချိန်ညှိပါတယ်။ Proxy ရဲ့ IAM role က app user secret ကို ဖတ်ခွင့် (secretsmanager:GetSecretValue နဲ့ ထို secret ရဲ့ KMS key ပေါ် kms:Decrypt) လိုပါတယ်။ IAMAuth=REQUIRED ကြောင့် client တွေက password အစား ၁၅ မိနစ်သက်တမ်းရှိတဲ့ IAM token နဲ့ ချိတ်ရပါတယ်။ Register လုပ်ပြီးတဲ့အခါ proxy endpoint ကို application မှာ DB host အဖြစ် သုံးပါ။

Proxy connection တစ်ခုကို ပြန်သုံးတဲ့ Lambda function

ဒီ example မှာ Lambda function တစ်ခုက RDS Proxy ဆီ IAM authentication နဲ့ ချိတ်ပုံကို ပြထားပါတယ်။ အရေးကြီးတာ နှစ်ချက်ပါ — boto3 client နဲ့ connection ကို handler အပြင်ဘက်မှာ သိမ်းထားလို့ warm invocation တွေက ပြန်သုံးနိုင်တယ်၊ password ကို environment variable ထဲ မထည့်ဘဲ generate_db_auth_token နဲ့ token တစ်ခု ထုတ်ယူပါတယ်။

ဒီ lesson ကို database မလိုဘဲ run နိုင်အောင် psycopg.connect ကို ခေါ်မယ့်နေရာမှာ connection parameter တွေကိုပဲ သိမ်းထားပါတယ်။ Token ကို local မှာ SigV4 နဲ့ sign လုပ်တာမို့ AWS ကို network call မလုပ်ပါဘူး။

orders_handler.py
import os
from urllib.parse import parse_qs, urlsplit

import boto3

PROXY_HOST = os.environ.get(
    "PROXY_HOST", "orders-proxy.proxy-c1a2b3c4d5e6.us-east-1.rds.amazonaws.com"
)
PORT = 5432
DB_USER = "app_user"

# Created once per execution environment, reused by every warm invocation.
rds = boto3.client("rds", region_name="us-east-1")
_connection = None
connections_opened = 0


def get_connection():
    """Open (or reuse) one connection to RDS Proxy using an IAM auth token."""
    global _connection, connections_opened
    if _connection is None:
        token = rds.generate_db_auth_token(
            DBHostname=PROXY_HOST, Port=PORT, DBUsername=DB_USER
        )
        # Real code: psycopg.connect(host=..., password=token, sslmode="require")
        _connection = {"host": PROXY_HOST, "port": PORT, "user": DB_USER,
                       "password": token, "sslmode": "require"}
        connections_opened += 1
    return _connection


def lambda_handler(event, context):
    conn = get_connection()
    return {"order_id": event["order_id"], "via": conn["host"].split(".")[0]}


if __name__ == "__main__":
    for order_id in (101, 102, 103):
        print(lambda_handler({"order_id": order_id}, None))
    print("connections opened:", connections_opened)

    token = _connection["password"]
    parts = urlsplit("https://" + token)
    query = parse_qs(parts.query)
    print("token targets:", parts.netloc)
    print("action:", query["Action"][0], "| user:", query["DBUser"][0])
    print("token valid for:", query["X-Amz-Expires"][0], "seconds")
    print("TLS required:", _connection["sslmode"])

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

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

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

  1. Module-level clientrds = boto3.client("rds", region_name="us-east-1")

    Handler အပြင်ဘက်မှာ ဖန်တီးထားလို့ execution environment တစ်ခုအတွက် တစ်ကြိမ်ပဲ run ပါတယ်။ Warm invocation တွေက client ကို ပြန်သုံးပါတယ်။

  2. Lazy, cached connectionif _connection is None:

    Connection ကို ပထမဆုံး လိုတဲ့အချိန်မှ ဖွင့်ပြီး global variable ထဲ သိမ်းပါတယ်။ Invocation သုံးကြိမ်မှာ connection တစ်ခုတည်းပဲ ဖွင့်တယ်ဆိုတာ output က ပြပါတယ်။

  3. IAM auth tokenrds.generate_db_auth_token(DBHostname=PROXY_HOST, Port=PORT, ...)

    Function ရဲ့ IAM role credential နဲ့ sign လုပ်ထားတဲ့ token ပါ။ ၁၅ မိနစ် (900 seconds) သက်တမ်းရှိပြီး password အစား သုံးပါတယ်။ Role မှာ rds-db:connect permission လိုပါတယ်။

  4. TLS required"sslmode": "require"

    IAM authentication က TLS connection ကို လိုအပ်ပါတယ်။ Proxy ကို --require-tls နဲ့ ဖန်တီးထားလို့ TLS မပါတဲ့ connection ကို ငြင်းပါတယ်။

You should see
{'order_id': 101, 'via': 'orders-proxy'}
{'order_id': 102, 'via': 'orders-proxy'}
{'order_id': 103, 'via': 'orders-proxy'}
connections opened: 1
token targets: orders-proxy.proxy-c1a2b3c4d5e6.us-east-1.rds.amazonaws.com:5432
action: connect | user: app_user
token valid for: 900 seconds
TLS required: require
Writer failover အတွင်း request လမ်းကြောင်း
  1. Lambda borrows a proxy connection
  2. Writer fails; replica is promoted
  3. Proxy holds client connections open
  4. Proxy reconnects to the new writer
  5. Transactions continue on new writer

Transaction က failover အချိန်မှာ လုပ်နေဆဲဆိုရင်

Client gets an error and must retry

Transaction အသစ်ဆိုရင်

Runs on the new writer, no DNS wait

Aurora Serverless v2 နဲ့ Global Database

Serverless v2 က capacity ကို Aurora Capacity Unit (ACU) နဲ့ တိုင်းပါတယ် — ACU တစ်ခုက memory 2 GiB လောက်နဲ့ သင့်တော်တဲ့ CPU, networking ပါ။ သင်သတ်မှတ်ထားတဲ့ minimum နဲ့ maximum ကြားမှာ အဆင့်သေးသေးလေးတွေနဲ့ scale လုပ်ပြီး cluster တစ်ခုတည်းထဲမှာ provisioned instance တွေနဲ့ ရောသုံးလို့ရပါတယ်။ Load မမှန်တဲ့၊ ရုတ်တရက် တက်တတ်တဲ့ workload တွေနဲ့ dev/test environment တွေအတွက် ကောင်းပါတယ်။

Load မြင့်ပြီး တည်ငြိမ်နေရင်တော့ provisioned instance (reserved pricing နဲ့ဆို ပိုကောင်း) က များသောအားဖြင့် ပိုသက်သာပါတယ်။ Engine version အသစ်တချို့က minimum 0 ACU နဲ့ auto-pause ကိုလည်း support လုပ်ပါတယ်၊ ဒါပေမဲ့ pause ဖြစ်ပြီးနောက် ပထမ connection က instance ပြန်နိုးလာတဲ့အထိ စောင့်ရပါတယ် — ကိုယ့် engine version မှာ ရလား docs မှာ စစ်ပါ။

Aurora Global Database က region တစ်ခု ပျက်သွားတာကို ကာကွယ်ပါတယ်။ Primary region မှာ writer ရှိပြီး secondary region တွေမှာ read-only cluster တွေ ရှိပါတယ်။ Replication ကို database engine မဟုတ်ဘဲ storage layer က လုပ်တာမို့ lag က ပုံမှန်အားဖြင့် တစ်စက္ကန့်အောက်ပါ၊ secondary region တွေက local read တွေကိုလည်း မြန်မြန် ပြန်ပေးနိုင်ပါတယ်။

ရွေးချယ်စရာ နှစ်မျိုးရှိပါတယ်။ Switchover က region နှစ်ခုလုံး ကျန်းမာနေချိန်မှာ စီစဉ်ပြီးလုပ်တာပါ — sync ဖြစ်အောင် စောင့်ပြီးမှ ပြောင်းလို့ data မဆုံးရှုံးပါဘူး။ Failover ကတော့ primary region ပျက်သွားချိန် emergency အတွက်ပါ — နောက်ဆုံး replicate မဖြစ်သေးတဲ့ write အနည်းငယ် ဆုံးရှုံးနိုင်လို့ --allow-data-loss ကို တမင် ထည့်ရပါတယ်။

text
AURORA GLOBAL DATABASE
----------------------
  us-east-1 (primary)                 eu-west-1 (secondary)
 +----------------------+           +----------------------+
 | writer + replicas    |           | replicas (read-only) |
 +----------------------+           +----------------------+
 | cluster volume       | --------> | cluster volume       |
 | 6 copies / 3 AZs     |  storage  | 6 copies / 3 AZs     |
 +----------------------+  replica  +----------------------+
                          lag ~ <1s
 switchover-global-cluster : planned, waits for sync, no loss
 failover-global-cluster   : emergency, --allow-data-loss

Read replica နဲ့ Multi-AZ: ပြဿနာ မတူပါဘူး

Classic RDS (Aurora မဟုတ်) မှာ ဒီနှစ်ခုကို မကြာခဏ ရောထွေးကြပါတယ်။ Multi-AZ က availability အတွက်ပါ — AZ တစ်ခုခြားက standby ဆီ synchronous replicate လုပ်ပြီး primary ပျက်ရင် အလိုအလျောက် failover လုပ်ပါတယ်။ Read replica က scale အတွက်ပါ — asynchronous ဖြစ်လို့ lag ရှိနိုင်ပြီး read traffic ကို ခွဲပေးပါတယ်၊ ဒါပေမဲ့ promote လုပ်တာက ကိုယ်တိုင်လုပ်ရတဲ့ အလုပ်ပါ။

မေးခွန်းMulti-AZ (instance)Read replica
ဘာပြဿနာ ဖြေရှင်းလဲAZ/instance ပျက်ခြင်းRead load များခြင်း
ReplicationSynchronousAsynchronous (lag ရှိနိုင်)
Query ပို့လို့ရလားမရ (standby က ဖတ်လို့မရ)ရ (read-only)
Failoverအလိုအလျောက်Promote ကို ကိုယ်တိုင်လုပ်ရ

RDS မှာ standby နှစ်ခုကို ဖတ်လို့ရတဲ့ Multi-AZ DB cluster deployment လည်း ရှိပါတယ်။ Aurora မှာတော့ ဒီခွဲခြားမှုက ပျောက်သွားပါတယ် — storage ကို မျှသုံးထားလို့ Aurora Replica တစ်ခုက read scaling နဲ့ failover target နှစ်ခုလုံး ဖြစ်ပါတယ်။

Production မှာ

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

ပုံမှန် production setup က Aurora PostgreSQL cluster တစ်ခုမှာ writer တစ်ခုနဲ့ မတူတဲ့ AZ တွေက replica နှစ်ခု ပါပါတယ်။ Replica တစ်ခုကို tier 0 (failover ဦးစားပေး)၊ အခြားတစ်ခုကို tier 1 ထားပြီး writer နဲ့ အရွယ်တူအောင် ထားပါတယ် — သေးတဲ့ replica ကို promote လုပ်မိရင် failover ပြီးချိန်မှာ performance ပြဿနာ ချက်ချင်း ဖြစ်တတ်လို့ပါ။

Application က write အတွက် cluster endpoint၊ နည်းနည်း stale ဖြစ်လည်း ရတဲ့ read တွေအတွက် reader endpoint ကို သုံးပါတယ်။ Lambda နဲ့ အခြား connection များတဲ့ client တွေကတော့ RDS Proxy ကနေ ဖြတ်ပါတယ်။ Analytics လို query လေးလေးတွေကို custom endpoint နဲ့ replica သီးသန့်ဆီ ပို့ပြီး checkout ကို မထိခိုက်အောင် လုပ်ပါတယ်။

  • Failover drill: business hours အတွင်း aws rds failover-db-cluster နဲ့ failover ကို ပုံမှန် စမ်းပြီး application က ဘယ်နှစ်စက္ကန့် error ပြလဲ တိုင်းပါ။
  • Monitoring: DatabaseConnections, AuroraReplicaLag နဲ့ proxy ရဲ့ ClientConnections နှိုင်း DatabaseConnections ကို CloudWatch alarm တွေ ထားပါ။
  • DR: RPO/RTO လိုအပ်ချက်က region အဆင့်ဆိုရင်ပဲ Global Database ကို သုံးပြီး switchover ကို တစ်နှစ်တစ်ခါလောက် လေ့ကျင့်ပါ။

အမှားများ

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

Write ပြီးချက်ချင်း reader endpoint က ဖတ်ခြင်း

User က profile ကို update လုပ်ပြီး page က reader endpoint ကနေ ချက်ချင်း ပြန်ဖတ်တယ်ဆိုပါစို့။ Replica lag က millisecond အနည်းငယ်ပဲ ဖြစ်ပေမဲ့ data အဟောင်း ပြန်ပေါ်လာနိုင်ပါတယ်။ Read-after-write လိုတဲ့ request တွေကို cluster endpoint ကိုပဲ ပို့ပါ။

RDS Proxy က အရာအားလုံးကို ဖြေရှင်းပေးမယ်လို့ ယူဆခြင်း (pinning)

Session state ကို ပြောင်းတဲ့ statement တွေ (ဥပမာ SET နဲ့ session variable ပြောင်းတာ၊ temporary table ဖန်တီးတာ) ပါရင် proxy က အဲဒီ client ကို database connection တစ်ခုနဲ့ pin လုပ်ထားရပြီး multiplex မလုပ်နိုင်တော့ပါဘူး။ Pinning များရင် proxy ရှိနေပေမဲ့ connection storm ပြန်ဖြစ်ပါတယ်။ DatabaseConnectionsCurrentlySessionPinned metric ကို စောင့်ကြည့်ပြီး session setting တွေကို connection string ဒါမှမဟုတ် proxy ရဲ့ initialization query ထဲ ရွှေ့ပါ။

Instance endpoint ကို hard-code လုပ်ခြင်း

Instance endpoint က failover ကို မလိုက်ပါဘူး၊ writer ပြောင်းသွားရင် app က read-only ဖြစ်သွားတဲ့ instance အဟောင်းဆီ ဆက်ရေးပြီး error တက်ပါတယ်။ Cluster endpoint ကိုသုံးပြီး DNS ကို ကြာကြာ cache မလုပ်အောင် (ဥပမာ JVM DNS TTL ကို စက္ကန့်အနည်းငယ်) သတ်မှတ်ပါ၊ ဒါမှမဟုတ် RDS Proxy ကနေ ဖြတ်ပါ။

Best Practices

Best practices

  • Production cluster တိုင်းမှာ AZ တစ်ခုခြားက writer အရွယ်တူ replica အနည်းဆုံး တစ်ခု ထားပါ။
  • Lambda ကနေ relational database ကို ချိတ်ရင် RDS Proxy ကို သုံးပြီး connection ကို handler အပြင်ဘက်မှာ ဖွင့်ပါ။
  • Database password တွေကို Secrets Manager မှာ ထားပြီး application က IAM auth token နဲ့ ချိတ်ပါ။
  • Retry logic ကို connection error နဲ့ failover error တွေအတွက် exponential backoff နဲ့ ထည့်ပါ။

💰 Cost & Cleanup

Aurora က instance-hour (Serverless v2 ဆိုရင် ACU-hour)၊ storage GB-month၊ retention ကျော်တဲ့ backup storage နဲ့ Aurora Standard configuration မှာဆိုရင် I/O request တွေအတွက် ကျသင့်ပါတယ် (Aurora I/O-Optimized မှာ I/O charge မရှိပေမဲ့ instance နဲ့ storage ဈေး ပိုမြင့်ပါတယ်)။ RDS Proxy က သူ့နောက်က database ရဲ့ capacity (vCPU ဒါမှမဟုတ် ACU) အလိုက် တစ်နာရီချင်း ကျသင့်ပါတယ်။ Aurora နဲ့ RDS Proxy က classic free tier ထဲ မပါပါဘူး။ Lab ပြီးတာနဲ့ proxy, instance တွေနဲ့ cluster ကို ချက်ချင်း ဖျက်ပါ — Serverless v2 minimum 0.5 ACU ကလည်း ၂၄ နာရီ run နေရင် ငွေကုန်ပါတယ်။

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

cleanup.sh
aws rds delete-db-proxy --db-proxy-name orders-proxy

for name in orders-writer orders-reader-1; do
  aws rds delete-db-instance --db-instance-identifier "$name" --skip-final-snapshot
done
aws rds wait db-instance-deleted --db-instance-identifier orders-writer

aws rds delete-db-cluster \
  --db-cluster-identifier orders \
  --skip-final-snapshot

Mini challenge

Reporting replica နဲ့ custom endpoint ထည့်ပါ

Reporting team ရဲ့ query လေးလေးတွေက checkout ကို နှေးစေနေပါတယ်။ သူတို့အတွက် replica သီးသန့်တစ်ခု ထည့်ပြီး failover ဖြစ်ရင် အဲဒီ replica ကို writer အဖြစ် ဘယ်တော့မှ ဦးစားပေး မရွေးအောင် လုပ်ပါ။

  • orders cluster ထဲမှာ orders-reporting ဆိုတဲ့ instance အသစ် ထည့်ပါ၊ promotion tier ကို 15 ထားပါ
  • analytics ဆိုတဲ့ READER custom endpoint တစ်ခု ဖန်တီးပြီး orders-reporting တစ်ခုတည်းကိုပဲ ထည့်ပါ
  • ရလာတဲ့ endpoint address ကို describe-db-cluster-endpoints နဲ့ စစ်ပါ

ရလဒ်: analytics.cluster-custom-... ပုံစံ endpoint တစ်ခု ရပြီး orders-reporting ကိုပဲ ညွှန်ပါတယ်။ Failover ဖြစ်ရင် Aurora က orders-reader-1 (tier 1) ကို ဦးစားပေး promote လုပ်ပါတယ်။

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

Promotion tier 0 က ဦးစားပေးအမြင့်ဆုံး၊ 15 က အနိမ့်ဆုံးပါ။ Custom endpoint မှာ --static-members နဲ့ instance တွေကို ရွေးပါ။

အဖြေ ကြည့်ရန်
solution.sh
aws rds create-db-instance \
  --db-instance-identifier orders-reporting \
  --db-cluster-identifier orders \
  --engine aurora-postgresql \
  --db-instance-class db.serverless \
  --promotion-tier 15

aws rds wait db-instance-available --db-instance-identifier orders-reporting

aws rds create-db-cluster-endpoint \
  --db-cluster-identifier orders \
  --db-cluster-endpoint-identifier analytics \
  --endpoint-type READER \
  --static-members orders-reporting

aws rds describe-db-cluster-endpoints \
  --db-cluster-identifier orders \
  --query "DBClusterEndpoints[].[EndpointType,Endpoint]" \
  --output table

Quick Quiz 1 / 3

Aurora cluster ရဲ့ AZ တစ်ခုလုံး ပျက်သွားပါတယ်။ Cluster က write တွေကို ဘာကြောင့် ဆက်လက်လက်ခံနိုင်တာလဲ။

Quick Quiz 2 / 3

Traffic spike တိုင်းမှာ Lambda function တွေက PostgreSQL ကို 'too many clients' error ဖြစ်စေပါတယ်။ အကောင်းဆုံး ပြင်ဆင်နည်းက ဘာလဲ။

Quick Quiz 3 / 3

RDS for MySQL (Aurora မဟုတ်) မှာ data မဆုံးရှုံးဘဲ automatic failover လိုတယ်၊ reporting query တွေကိုလည်း primary ကနေ ခွဲထုတ်ချင်တယ်။ ဘာလိုလဲ။

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

  • Aurora က compute နဲ့ storage ကို ခွဲထားပြီး cluster volume က AZ သုံးခုမှာ copy ခြောက်ခု (write quorum 4၊ read quorum 3) သိမ်းပါတယ်။
  • Replica တွေက storage ကို မျှသုံးလို့ မြန်မြန်ထည့်လို့ရပြီး failover target လည်း ဖြစ်ပါတယ်၊ write အတွက် cluster endpoint၊ read အတွက် reader endpoint ကို သုံးပါ။
  • RDS Proxy က connection pooling၊ IAM auth နဲ့ Secrets Manager integration ကနေ Lambda ရဲ့ connection storm ကို ဖြေရှင်းပေးပါတယ်။
  • Classic RDS မှာ Multi-AZ က availability အတွက်၊ read replica က read scaling အတွက် ဖြစ်ပါတယ်။
  • Serverless v2 က load မမှန်တဲ့ workload အတွက်၊ Global Database က region-level DR အတွက် ဖြစ်ပါတယ်။

What is Amazon Aurora? — AWS Documentation

Amazon RDS Proxy — AWS Documentation

Using Amazon Aurora Global Database — AWS Documentation

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

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

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

Aurora နဲ့ RDS Proxy: Relational Database ကို Scale ချဲ့ခြင်း | Thuta Learning