การสร้างระบบไร้เซิร์ฟเวอร์ที่ทนทาน
ออกแบบสถาปัตยกรรมไร้เซิร์ฟเวอร์ที่พร้อมใช้งานสูงและทนต่อข้อขัดข้อง โดยนำรูปแบบต่าง ๆ เช่น ตัวตัดวงจร การลองใหม่ และการทำงานซ้ำอย่างปลอดภัยมาใช้กับฟังก์ชันของคุณ
การสร้างระบบไร้เซิร์ฟเวอร์ที่ทนทาน เป็นบทเรียน Serverless AWS Lambda Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Serverless AWS Lambda Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Serverless AWS Lambda Development มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Building Robust Serverless Systems
Welcome! In this lesson, we'll dive into designing highly resilient and fault-tolerant serverless applications. Even though AWS manages much of the infrastructure, your functions still need to handle failures gracefully.
We'll explore key architectural patterns to ensure your applications remain stable and performant, even when things go wrong.
The Reality of Distributed Systems
In a serverless world, your functions often interact with many other services: databases, APIs, message queues. These interactions happen over a network, and networks can be unreliable.
- Transient Failures: Brief network glitches or service slowdowns.
- Downstream Service Issues: A service your Lambda calls might be temporarily unavailable.
- Unexpected Data: Malformed input can cause your function to crash.
Designing for these "failures" is crucial for a stable system.
Lambda's Built-in Retry Logic
For certain invocation types, AWS Lambda automatically retries your function if it fails. This is a powerful built-in resilience mechanism for asynchronous invocations.
For example, if an SQS queue triggers your Lambda and your function errors, Lambda (or SQS) will retry the invocation a few times. This helps overcome transient issues without any code changes.
However, retries aren't a silver bullet; they can lead to duplicate processing if not handled carefully.
Making Operations Idempotent
When retries happen, your function might execute the same operation multiple times. This is where idempotency comes in.
An idempotent operation is one that can be applied multiple times without changing the result beyond the initial application.
- Example: Setting a value (
x = 5) is idempotent. - Non-Example: Incrementing a value (
x++) is NOT idempotent, as each retry would change the value.
For resilient systems, many operations should strive to be idempotent.
Keys to Idempotent Functions
To make your Lambda functions idempotent, you often need to track the state of a request. This typically involves:
- Generating a unique Idempotency Key for each request (e.g., from request ID, event source ID).
- Checking if this key has already been processed before performing the core logic.
- Storing the result or status of the operation associated with the key.
This ensures that even if a function is retried, the core side-effect only occurs once.
import hashlib
import json
# Imagine a database or cache for storing processed requests
# For simplicity, using a global dict here. A real app uses persistent storage.
processed_requests = {}
def is_idempotent(event_payload):
# Create a unique key from the event payload
# For a real app, use a proper hashing/unique ID strategy
event_hash = hashlib.md5(json.dumps(event_payload, sort_keys=True).encode('utf-8')).hexdigest()
if event_hash in processed_requests:
print(f"Request with hash {event_hash} already processed.")
return True
processed_requests[event_hash] = "processing" # Mark as processing
return False
def lambda_handler(event, context):
if is_idempotent(event):
return {
'statusCode': 200,
'body': json.dumps('Request already processed or is being processed.')
}
# Simulate actual work (e.g., writing to a database)
print(f"Processing new request: {event}")
# In a real scenario, update processed_requests[event_hash] = "completed"
# after successful processing and store the result in persistent storage.
return {
'statusCode': 200,
'body': json.dumps('Request processed successfully!')
}
Preventing Cascading Failures
The Circuit Breaker pattern is a powerful way to prevent a failing service from causing cascading failures throughout your application.
Imagine a call to an external API that starts failing. Continuously retrying it will just waste resources and slow down your function. A circuit breaker detects this and "opens" the circuit, stopping calls to the failing service temporarily.
This gives the failing service time to recover and prevents your application from getting bogged down.
How a Circuit Breaker Works
A circuit breaker typically has three states:
- Closed: Operations proceed as normal. If failures exceed a threshold, it transitions to Open.
- Open: All calls to the protected service immediately fail (or return a fallback). After a timeout, it transitions to Half-Open.
- Half-Open: A limited number of test calls are allowed through. If these succeed, it transitions back to Closed. If they fail, it returns to Open.
This intelligent behavior allows for self-healing.
Implementing a Simple Circuit Breaker
Implementing a full circuit breaker involves managing state (failures, success counts, last failure time). For serverless, this state might be stored in a shared cache (like ElastiCache) or a database.
While complex to implement from scratch in a simple Lambda, understanding the logic is key. Libraries exist for various languages to help, or you can leverage AWS services like Step Functions to orchestrate retry logic with delays.
import time
class CircuitBreaker:
def __init__(self, failure_threshold=3, reset_timeout=5):
self.state = "CLOSED"
self.failure_count = 0
self.last_failure_time = 0
self.failure_threshold = failure_threshold
self.reset_timeout = reset_timeout # seconds
def call(self, func, *args, **kwargs):
if self.state == "OPEN":
if time.time() - self.last_failure_time > self.reset_timeout:
self.state = "HALF-OPEN"
# In a real app, log this state change
else:
raise Exception("Circuit is open, service unavailable.")
try:
result = func(*args, **kwargs)
if self.state == "HALF-OPEN":
self.state = "CLOSED"
self.failure_count = 0
# In a real app, log this state change
return result
except Exception as e:
self.failure_count += 1
self.last_failure_time = time.time()
if self.failure_count >= self.failure_threshold:
self.state = "OPEN"
# In a real app, log this state change
raise e
Timeouts Prevent Hanging
Another crucial resilience pattern is using timeouts for external calls. If your Lambda function calls another service (e.g., a database, an HTTP API), that call could hang indefinitely if the service is unresponsive.
Configuring a timeout ensures your function doesn't wait forever, freeing up resources and allowing for retry logic to kick in faster. AWS Lambda itself has a configurable timeout, but you should also set timeouts within your code for specific external requests.
Resilient Design Challenge
Consider a Lambda function that processes incoming orders. If the function fails after successfully deducting payment but before updating the order status in a database, and then retries, what problem could arise if the payment deduction is NOT idempotent?
Summary: Building for Failure
We've covered essential patterns for building resilient serverless applications:
- Retries: Lambda's built-in mechanism for transient errors.
- Idempotency: Ensuring operations can be safely retried without unintended side-effects (e.g., duplicate charges).
- Circuit Breakers: Preventing cascading failures by intelligently stopping calls to failing services.
- Timeouts: Protecting against unresponsive external services.
By applying these principles, you can create serverless systems that gracefully handle inevitable failures.
คำถามที่พบบ่อย
บทเรียน “การสร้างระบบไร้เซิร์ฟเวอร์ที่ทนทาน” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การสร้างระบบไร้เซิร์ฟเวอร์ที่ทนทาน” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Serverless AWS Lambda Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Serverless AWS Lambda Development มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การสร้างระบบไร้เซิร์ฟเวอร์ที่ทนทาน”
ออกแบบสถาปัตยกรรมไร้เซิร์ฟเวอร์ที่พร้อมใช้งานสูงและทนต่อข้อขัดข้อง โดยนำรูปแบบต่าง ๆ เช่น ตัวตัดวงจร การลองใหม่ และการทำงานซ้ำอย่างปลอดภัยมาใช้กับฟังก์ชันของคุณ คุณปฏิบัติ Serverless AWS Lambda Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Serverless AWS Lambda Development หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Serverless AWS Lambda Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน
บทเรียน “การสร้างระบบไร้เซิร์ฟเวอร์ที่ทนทาน” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Serverless AWS Lambda Development นี้ได้ไหม
ได้ บทเรียน Serverless AWS Lambda Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การติดตั้งใช้งานแบบ Canary และ Blue/Green
- การสร้างระบบไร้เซิร์ฟเวอร์ที่ทนทาน
- รูปแบบสถาปัตยกรรมไร้เซิร์ฟเวอร์
- การเพิ่มประสิทธิภาพค่าใช้จ่ายในสถาปัตยกรรมแบบไร้เซิร์ฟเวอร์