24.1 When to Use Serverless for AI
| Use Case | Service | Why Serverless |
|---|---|---|
| Document ingestion trigger | Lambda / Azure Functions | Event-driven, sporadic traffic |
| Lightweight inference (small models) | Lambda / Functions | Low latency at low volume, auto-scale to zero |
| ETL pipeline steps | Lambda + Step Functions | Orchestrated, pay-per-use |
| API backend with variable traffic | Lambda + API Gateway | Scales to zero, no idle cost |
| Batch processing | AWS Batch / Fargate | Cost-effective for bursty batch, not true serverless but no idle infra |
When NOT serverless: GPU inference (use SageMaker/Azure ML endpoints or EKS/AKS GPU nodes), long-running training jobs, stateful multi-turn agent workflows that need warm in-memory context.
24.2 Example Serverless AI Pipeline
S3 Upload (document)
→ Lambda: OCR (Textract)
→ Lambda: Parse & validate
→ Lambda: LLM call (Bedrock) for extraction/classification
→ Lambda: Store results (DynamoDB)
→ SQS: Queue for human review if confidence is low
Approx. cost per document: $0.01–$0.05
24.3 Cold Start Mitigation Strategies
| Strategy | Mechanism | Trade-off |
|---|---|---|
| Provisioned Concurrency (Lambda) | Pre-warmed execution environments kept ready | Pay for idle capacity, defeats pure pay-per-use |
| Minimum instance count (Azure Functions / Container Apps) | Keep N instances always warm | Same cost trade-off as above |
| Smaller deployment package | Trim dependencies, lazy-import SDKs | Reduces init time, no runtime cost |
| SnapStart (Lambda, Java) | Snapshot a pre-initialized JVM, resume from snapshot | Java-specific, near-eliminates JVM cold start |
| Keep runtime lightweight | Avoid bundling large ML libraries into the function; call out to a managed endpoint instead | Function itself becomes thin orchestration, GPU work moves to SageMaker/Vertex endpoint |
| Route latency-critical paths off serverless | Use ECS Fargate/AKS with always-on replicas for P99-sensitive endpoints | Loses scale-to-zero cost benefit |
24.4 Interview Q&A
Q1: Your team wants to put an LLM call behind a Lambda for a customer-facing API. What do you flag as risks?
A: Three risks. First, cold start — Java/
.NETLambdas can take seconds to initialize, and stacking that under an LLM call that itself takes 1–5 seconds produces unacceptable P99 latency; I’d mitigate with Provisioned Concurrency or move the endpoint to always-on Fargate/Container Apps if traffic justifies it. Second, the 15-minute timeout — fine for a single LLM call, but if this evolves into a multi-step agent workflow it will blow past that; I’d design for Step Functions/Durable Functions orchestration from day one so the pattern extends cleanly. Third, cost at scale — Lambda’s per-invocation pricing plus the LLM’s per-token pricing compounds; at high steady-state volume I’d model whether a warm pool on ECS/EKS calling the model gateway directly is actually cheaper.
Q2: How do you keep a serverless AI pipeline resilient to a provider outage?
A: Put a circuit breaker in front of the LLM call — after N consecutive failures, short-circuit to a fallback (secondary model provider, cached response, or a “processing delayed” queue for later replay) rather than letting every invocation hang until timeout. Use SQS/Service Bus as a buffer between ingestion and the AI step so a provider outage causes backlog, not data loss — messages redrive once the provider recovers. Set aggressive but sane per-call timeouts so a single stuck call doesn’t hold a Lambda concurrency slot for the full 15 minutes.