19 Serverless Best Practices
DodaTech
1 min read
title: Serverless Best Practices — Idempotency and Timeouts weight: 29 date: 2026-06-28 lastmod: 2026-06-28 description: Learn serverless best practices including idempotency for reliability, timeout configuration, error handling, connection pooling, and performance optimization. tags: [api-development, serverless]
Serverless best practices focus on idempotency for safe retries, appropriate timeout limits (29s for API Gateway, 15min for Lambda), connection reuse across invocations, and stateless function design with external state storage.
```python
import json
import hashlib
import boto3
import os
dynamodb = boto3.resource("dynamodb")
table = dynamodb.Table(os.environ["TABLE_NAME"])
def lambda_handler(event, context):
# Idempotency check using idempotency key
headers = event.get("headers", {})
idempotency_key = headers.get("Idempotency-Key") or headers.get("idempotency-key")
if idempotency_key:
# Check if already processed
response = table.get_item(Key={"pk": f"idempotency#{idempotency_key}"})
if "Item" in response:
return api_response(200, response["Item"]["result"])
# Process request with timeout consideration
remaining_ms = context.get_remaining_time_in_millis()
if remaining_ms < 5000:
return api_response(503, {"error": "Timeout approaching"})
result = {"message": "Processed", "idempotency_key": idempotency_key}
# Store result with idempotency key
if idempotency_key:
table.put_item(Item={
"pk": f"idempotency#{idempotency_key}",
"result": result,
"ttl": int(time.time()) + 86400 # Expire after 24 hours
})
return api_response(200, result)
What's Next
Now start the serverless project in Building Serverless APIs.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro