Understanding Serverless with Go
Serverless platforms work differently from traditional servers. Understanding these characteristics helps you design better serverless applications:| Aspect | Impact on Mizu Apps |
|---|---|
| Cold starts | When your function hasn’t run recently, the platform needs to start a new instance. This “cold start” adds latency (100-500ms). Warm instances respond immediately |
| Stateless | Each request might run on a different instance, so you can’t store data in memory between requests. Use databases or caches instead |
| Timeout limits | Functions have maximum execution times (15-30 minutes). Long-running tasks need different architectures |
| Concurrency | Each instance typically handles one request at a time. High traffic means many parallel instances |
AWS Lambda
AWS Lambda is the original and most popular serverless platform. You upload a function, and AWS runs it in response to events—HTTP requests, database changes, file uploads, or scheduled triggers. For web applications, you need a way to receive HTTP requests. AWS offers two options: Function URLs (simpler, direct HTTP access) or API Gateway (more features like rate limiting, API keys, and request transformation).Lambda with Function URLs
Function URLs are the simplest way to invoke Lambda via HTTP. AWS gives you a URL likehttps://xyz123.lambda-url.us-east-1.on.aws/ that directly invokes your function. No API Gateway setup required.
main.go:
Lambda with API Gateway
For more control over HTTP handling. template.yaml (AWS SAM):Using aws-lambda-go-api-proxy
A library that simplifies Lambda + Mizu integration:Google Cloud Functions
Google Cloud Functions is Google’s serverless platform. It’s tightly integrated with other Google Cloud services and has a simpler deployment model than Lambda—you can deploy directly from source code without building a container.HTTP Function
The simplest approach is an HTTP-triggered function. Your function receives standardhttp.ResponseWriter and *http.Request parameters, so Mizu works seamlessly:
function.go:
Cloud Functions Gen2 (Recommended)
Based on Cloud Run, better performance.Azure Functions
main.go:Vercel
Vercel supports Go with their Serverless Functions. api/index.go:Optimizing for Serverless
Serverless performance is all about reducing cold starts and minimizing execution time (since you pay per millisecond). These optimizations can significantly reduce costs and improve user experience.Reduce Cold Start Time
Cold starts happen when there’s no warm instance available to handle your request. The key insight is that initialization code runs once per instance, not once per request. Put expensive setup (like database connection pools) ininit() so it only runs during cold starts:
Keep Connections Alive
Binary Size Optimization
Comparison
| Platform | Cold Start | Max Timeout | Pricing |
|---|---|---|---|
| AWS Lambda | 100-500ms | 15 min | Per invocation |
| Cloud Functions | 100-500ms | 9 min (gen1), 60 min (gen2) | Per invocation |
| Azure Functions | 100-500ms | 10 min | Per invocation |
| Vercel | 100-300ms | 10 sec (hobby), 60 sec (pro) | Per invocation |
| Cloud Run | 0ms (min instances) | 60 min | Per request |
When to Use Serverless
Good for:- Variable/unpredictable traffic
- Event-driven workloads
- Cost optimization at low scale
- APIs with bursty traffic
- Long-running processes
- WebSocket connections
- High-volume, consistent traffic
- Latency-sensitive applications
Next Steps
Traditional Deployment
VPS with systemd and reverse proxy.
CI/CD
Automate serverless deployments.