Why Serverless AI?
Traditional GPU servers are expensive and idle most of the time. Serverless inference lets you pay only for actual usage.
Platforms
### Modal
- Python-native serverless cloud.
- GPU access with code changes.
- Great for ML pipelines and batch inference.
- Pricing: Per-second GPU billing.
### Replicate
- Run open-source models via API.
- No infrastructure management.
- Huge model library (Llama, Stable Diffusion, Whisper).
- Pricing: Per-second GPU time.
### Cloudflare Workers AI
- Edge-deployed models globally.
- Low latency for end users.
- Limited model selection but growing.
- Pricing: Per-request.
### Banana / Together AI
- Specialized in LLM inference.
- Auto-scaling from zero to thousands of requests.
- Competitive pricing for high-volume workloads.
Architecture Patterns
- Cold Start Mitigation: Use model caching and pre-warming.
- Request Queuing: Buffer requests during scale-up.
- Edge + Cloud: Simple models at edge, complex in cloud.
When NOT to Use Serverless
- Consistent high-throughput workloads (>80% GPU utilization).
- Models too large for available serverless GPU memory.
- Latency-sensitive applications requiring <50ms response.
The most common pattern is API Gateway → Lambda/Cloud Function → Model endpoint. For GPU inference, use services like Modal, Replicate, or AWS Lambda with GPU (coming soon). For CPU inference (small models, quantized), standard Lambda functions work well.
Cold Start Optimization
Cold starts are the biggest challenge for serverless AI. Strategies:
- Provisioned concurrency: Keep warm instances ready (AWS) at a cost premium.
- Model caching: Store models in /tmp or EFS. Lambda can reuse files across invocations if the container is warm.
- Smaller models: A 100MB model loads in ~200ms; a 2GB model takes ~4 seconds. Use quantized models (GGUF Q4) to minimize size.
- SnapStart: AWS Lambda SnapStart reduces cold starts by 90% for Java-based inference servers.