🛒 Case Study: ทำ E-commerce Microservice (Order, Payment, Notification) บน AWS

Sharing is caring!

บทนำ

ในยุคที่ระบบ E-commerce เติบโตอย่างรวดเร็ว การสร้างระบบให้รองรับผู้ใช้จำนวนมาก และมีการประมวลผลแบบแยกส่วน (Microservices) ถือเป็นสิ่งจำเป็น AWS เป็นแพลตฟอร์มที่เหมาะสมอย่างยิ่งสำหรับแนวคิดนี้ — ด้วยบริการอย่าง API Gateway, Lambda, DynamoDB, SNS, SQS ที่ช่วยให้เราสร้างระบบ Order, Payment, Notification ได้แบบ Serverless อย่างแท้จริง

1. สถาปัตยกรรมระบบ E-commerce Microservice

ระบบนี้จะแบ่งเป็น 3 บริการหลัก:

  • Order Service — รับคำสั่งซื้อและบันทึกข้อมูล
  • Payment Service — ดำเนินการชำระเงินและตรวจสอบสถานะ
  • Notification Service — แจ้งเตือนผู้ใช้เมื่อมีการชำระเงินสำเร็จ

2. โครงสร้างการทำงานของแต่ละ Service

บริการแต่ละตัวจะสื่อสารกันผ่าน Event-driven Architecture โดยใช้ SNS และ SQS ทำให้ระบบมีความยืดหยุ่น และขยายเพิ่มบริการอื่นได้ในอนาคต

3. Order Service (API Gateway + Lambda + DynamoDB)

เริ่มจาก Order Service ที่รับ Request จากผู้ใช้ผ่าน API Gateway แล้วบันทึกข้อมูลคำสั่งซื้อลง DynamoDB จากนั้นส่ง Event ไปยัง SNS เพื่อแจ้ง Payment Service

// orderService.js
const AWS = require("aws-sdk");
const dynamo = new AWS.DynamoDB.DocumentClient();
const sns = new AWS.SNS();

exports.handler = async (event) => {
  const order = JSON.parse(event.body);
  order.status = "PENDING";
  order.id = Date.now().toString();

  await dynamo.put({
    TableName: "Orders",
    Item: order
  }).promise();

  await sns.publish({
    TopicArn: process.env.PAYMENT_TOPIC_ARN,
    Message: JSON.stringify(order)
  }).promise();

  return {
    statusCode: 200,
    body: JSON.stringify({ message: "Order created", order })
  };
};
    

4. Payment Service (SQS + Lambda + DynamoDB)

Payment Service จะทำงานโดยการรับข้อความจากคิว SQS ที่ส่งมาจาก SNS และจำลองการชำระเงิน เช่น ผ่าน Payment Gateway ภายนอก

// paymentService.js
const AWS = require("aws-sdk");
const dynamo = new AWS.DynamoDB.DocumentClient();
const sns = new AWS.SNS();

exports.handler = async (event) => {
  for (const record of event.Records) {
    const order = JSON.parse(record.body);
    order.status = "PAID";

    await dynamo.update({
      TableName: "Orders",
      Key: { id: order.id },
      UpdateExpression: "set #s = :s",
      ExpressionAttributeNames: { "#s": "status" },
      ExpressionAttributeValues: { ":s": "PAID" }
    }).promise();

    await sns.publish({
      TopicArn: process.env.NOTIFICATION_TOPIC_ARN,
      Message: JSON.stringify({ orderId: order.id, status: order.status })
    }).promise();
  }
};
    

5. Notification Service (SNS + Lambda + SES)

เมื่อ Payment สำเร็จ ระบบ Notification จะรับ Event จาก SNS แล้วส่งอีเมลแจ้งผู้ใช้ผ่าน AWS SES

// notificationService.js
const AWS = require("aws-sdk");
const ses = new AWS.SES();

exports.handler = async (event) => {
  for (const record of event.Records) {
    const msg = JSON.parse(record.Sns.Message);

    await ses.sendEmail({
      Destination: { ToAddresses: ["[email protected]"] },
      Message: {
        Subject: { Data: `Order ${msg.orderId} confirmed` },
        Body: { Text: { Data: `Your payment is ${msg.status}` } }
      },
      Source: "[email protected]"
    }).promise();
  }
};
    

6. การใช้ SNS + SQS สำหรับ Event-driven Communication

SNS ทำหน้าที่เป็น Publisher ส่งข้อความแจ้ง Event ไปยังหลายบริการ ส่วน SQS เป็นคิวที่ช่วยเก็บ Event ชั่วคราวเพื่อป้องกันการสูญหายของข้อมูล ระบบนี้จึงสามารถทำงานแบบ Asynchronous ได้อย่างมีประสิทธิภาพ

7. การตรวจสอบระบบด้วย CloudWatch

ใช้ Amazon CloudWatch เพื่อตรวจสอบ Logs, Metrics และ Alarm ของ Lambda เพื่อให้มั่นใจว่าระบบทั้งหมดทำงานต่อเนื่องและไม่มี Error เกิดขึ้น

8. ข้อดีของการใช้ Microservice บน AWS

  • ✅ ขยายระบบได้ง่าย (Scalable)
  • ✅ ระบบไม่ล่มพร้อมกันทั้งหมด (Isolated Failure)
  • ✅ พัฒนาและ Deploy แยกส่วนได้อิสระ
  • ✅ ใช้จ่ายตามการใช้งานจริง (Pay-as-you-go)

9. สรุปภาพรวมการทำงาน

จาก Case Study นี้ เราสามารถสร้างระบบ E-commerce ขนาดเล็กแต่ทรงพลังบน AWS ได้ โดยใช้บริการที่มีอยู่แล้วโดยไม่ต้องสร้างเซิร์ฟเวอร์เองเลยแม้แต่เครื่องเดียว

🧩 เขียนโดย King Pool

ภาพประกอบ: Order, Payment, Notification, SNS, SQS, Lambda

อ่านต่อ: การ Integrate ระบบ Machine Learning ด้วย SageMaker และ Bedrock

Leave a Reply

อีเมลของคุณจะไม่แสดงให้คนอื่นเห็น ช่องข้อมูลจำเป็นถูกทำเครื่องหมาย *