การปรับขนาดและการควบคุม Concurrency

Serverless Applications with AWS Lambda

Claudio Canales

Senior DevOps Engineer

การ Scale ในภาพเดียว

  • ความต้องการเพิ่ม -> รันพร้อมกันมากขึ้น
  • แต่ละการรันใช้ Execution Environment
  • Concurrency = จำนวนการรันที่เกิดขึ้นในขณะนี้

ความต้องการพุ่งสูง -> สร้าง Environment เพิ่ม

Serverless Applications with AWS Lambda

Concurrency คืออะไร?

  • หนึ่ง Invocation = หนึ่งหน่วยงาน
  • Concurrency = จำนวนที่กำลังรันอยู่ขณะนี้
  • ถ้ารันอยู่ 10 รายการ Concurrency = 10

Invocation แบบขนาน

Serverless Applications with AWS Lambda

การประมาณ Concurrency ที่ต้องการ

  • หลักการคร่าวๆ: concurrency = rps * duration
  • ลด Duration เพื่อลด Concurrency
  • ใช้สูตรนี้กำหนดขนาด Limit อย่างปลอดภัย

สูตรคำนวณ Concurrency

Serverless Applications with AWS Lambda

ตัวอย่าง: 50 rps ที่ 200 ms

  • 200 ms = 0.2 วินาที
  • 50 * 0.2 = 10 Concurrent Executions
  • โค้ดที่เร็วกว่าหมายถึงการรันแบบขนานน้อยลง

ตัวอย่างการคำนวณ Concurrency

Serverless Applications with AWS Lambda

Limit: Account Pool กับ Function Slice

  • Concurrency ถูกจำกัดในระดับ Account
  • จองส่วนหนึ่งของ Pool ไว้สำหรับฟังก์ชันสำคัญ
  • ฟังก์ชันอื่นใช้ส่วนที่เหลือร่วมกัน

Account Concurrency Pool

Serverless Applications with AWS Lambda

Reserved Concurrency: Hard Cap

  • ทำหน้าที่เป็นวาล์วนิรภัย
  • จำกัดการทำงานแบบขนาน
  • เกิน Cap จะเกิดการ Throttle
  • ฟังก์ชันที่ใช้งานหนักไม่สามารถกินทรัพยากรจนหมด

Reserved Concurrency Cap

Serverless Applications with AWS Lambda

Throttling: ลักษณะที่เกิดขึ้น

  • Lambda จะ Throttle เมื่อถึง Concurrency Limit
  • ผู้เรียกจะไม่ได้รับการประมวลผลตามปกติ
  • ตรวจสอบ Throttle ได้จาก Monitoring

สัญญาณ Throttling

Serverless Applications with AWS Lambda

Provisioned Concurrency: Warm Capacity

  • Pool ของ Environment ที่เตรียมพร้อมไว้ล่วงหน้า
  • มักอยู่เบื้องหลัง Alias
  • Request เริ่มต้นได้เร็วกว่า

Provisioned Concurrency Warm Pool

Serverless Applications with AWS Lambda

Cold Start กับ Provisioned

Cold Start

  • ความล่าช้าจากการ Initialize ครั้งแรก
  • Request แรกแบกรับต้นทุนการตั้งค่า

Provisioned

  • ทำงาน Init ล่วงหน้าแล้ว
  • Request แรกไม่ต้องรับภาระต้นทุนการตั้งค่า

Timeline เปรียบเทียบ Cold Start กับ Provisioned

Serverless Applications with AWS Lambda

Reserved กับ Provisioned (แก้ปัญหาต่างกัน)

  • Reserved ควบคุม Load
  • Provisioned ควบคุม Startup Latency
  • ฟังก์ชันใน Production มักใช้ทั้งสองแบบ

เปรียบเทียบ Reserved กับ Provisioned

Serverless Applications with AWS Lambda

ปกป้อง Downstream Systems

  • ฐานข้อมูลรองรับ Connection ได้จำกัด
  • Concurrency Cap ช่วยป้องกันปัญหานี้
  • Traffic ที่พุ่งสูงอาจทำให้เกิด Outage ได้

Concurrency Cap ปกป้องฐานข้อมูล

Serverless Applications with AWS Lambda

Traffic พุ่งสูง กับ Traffic ปกติ

  • Traffic พุ่งสูงสร้าง Environment แบบขนานจำนวนมาก
  • Handler ที่ช้าทำให้ Concurrency สูงอยู่นาน
  • Cap ช่วยบรรเทาการพุ่งสูงได้
  • การลด Duration ก็เป็นกลยุทธ์การ Scale เช่นกัน

Traffic พุ่งสูง กับ Steady State

Serverless Applications with AWS Lambda

Failure และ Retry เพิ่ม Load

  • การ Retry อาจขยาย Traffic ได้
  • Concurrency Limit ช่วยลดขอบเขตความเสียหาย
  • Failure Loop อาจทำให้ Dependency ล้มได้

Retry ทำให้ Load เพิ่มหลายเท่า

Serverless Applications with AWS Lambda

ขั้นตอนการปรับแต่งเบื้องต้น

  • เริ่มจากการวัด
  • กำหนด Cap เมื่อเริ่มเห็น Throttle หรือแรงกดดัน
  • เพิ่ม Provisioned Capacity เมื่อ Cold Start กระทบ Latency

วัด -> กำหนด Cap -> อุ่นล่วงหน้า

Serverless Applications with AWS Lambda

สรุปประเด็นสำคัญ

  • Concurrency ช่วยให้คิดเรื่องการ Scale ได้ชัดขึ้น
  • ประมาณด้วยสูตร: rps * duration
  • Reserved จำกัด Load; คอยดู Throttle
  • Provisioned ลด Cold-Start Latency

สรุปประเด็นสำคัญของ Concurrency

Serverless Applications with AWS Lambda

มาฝึกกันเถอะ!

Serverless Applications with AWS Lambda

Preparing Video For Download...