Functions and Event-Driven Processing: Going Serverless
Core Concept
- FaaS (Functions-as-a-Service) = event-driven, short-lived compute
- Functions:
- Run on-demand
- Are stateless
- Triggered by events or requests
Often called serverless (but not exactly the same)
When to Use FaaS
Best for:
- Event-driven tasks
- Short-lived processing
- Low/variable traffic
Examples:
- Sending emails
- Notifications
- API transformations
Benefits
1. Extremely simple deployment
- Just write code → no infra management
- Faster time to production
2. Auto-scaling
- Automatically scales with traffic
- Handles failures automatically
3. Strong decoupling
- Each function is independent
- Encourages modular systems
4. Cost efficient (for low traffic)
- Pay per request
- No idle cost
Challenges
1. Hard to debug & observe
- No clear system view
- Complex interactions between functions
2. No local state
- Must use external storage
- Adds latency + complexity
3. Risk of hidden loops
- Functions calling each other → infinite loops
- Hard to detect due to decoupling
4. Monitoring is critical
- Need strong:
- Logging
- Alerting
- Observability
When NOT to Use FaaS
1. Long-running tasks
- Example:
- Video processing
- Batch jobs
FaaS has execution time limits
2. Memory-heavy workloads
- Cold start + data loading → high latency
3. High sustained traffic
- Pay-per-request becomes expensive
- Containers/VMs become cheaper
Key Patterns
1. Decorator Pattern (very important)
- Function sits between client and service
- Transforms request/response
Example:
- Add default values
- Validate input
Lightweight and stateless → perfect for FaaS
2. Event Handling
- Trigger functions on events:
- User signup
- File upload
- Login
Example:
- 2FA → send OTP asynchronously
Improves UX by offloading slow tasks
3. Event Pipelines
- Chain multiple functions:
- Each does one small task
- Connected via events/webhooks
Example:
Signup → Send email → Subscribe user → Analytics
Like microservices but event-driven
FaaS vs Microservices
| Feature | FaaS | Microservices |
|---|---|---|
| Lifecycle | Short-lived | Long-running |
| Trigger | Event-driven | Request-driven |
| State | Stateless | Can hold state |
| Scaling | Automatic | Managed |
Key Insights
- FaaS = extreme decoupling
- Great for:
- Glue logic
- Async tasks
- Not for:
- Core heavy systems
Trade-offs
Pros
- Fast development
- Auto scaling
- Cost-efficient (low usage)
- Highly modular
Cons
- Hard debugging
- Cold starts
- Costly at scale
- Limited execution model
One-line Summary
FaaS enables event-driven, stateless, auto-scaling functions that are ideal for lightweight, asynchronous tasks—but not suitable for long-running, stateful, or high-throughput systems.
#Distributed Systems #System Design #Serverless #FaaS #Event-Driven