AI summary by TwinMind
Serverless kinda sucked without this...
Theo - t3․gg
12m 23s
Lambda Execution Limitations
- Vercel solved major serverless pain point: Lambda dies after sending response
- waitUntil() allows async code execution after response (like Go's defer)
- Lambda has 3 invocation types: synchronous, asynchronous, polling
- Synchronous: caller waits for completion, function returns value
- Most web traffic (API Gateway, CloudFront) uses synchronous invocation
- Synchronous has limitation: dies once response returned
- Asynchronous invocation requires building event queue system
- Must manually attach to web layer
- Complex to implement successfully
- Even experienced developers (including Dax) struggle with implementation
Technical Demo Implementation
- Built simple web form with name input + submit button
- Server action function with "use server" directive
- Database: simple db.txt file for appending names
- Two versions: regular append vs append with 5-second delay
- Without await: immediate response but potential data loss on Lambda
- With await: user waits full 5 seconds for response
- Problem: removing await on Lambda = high chance append operation fails/never executes
waitUntil API Usage
- Import:
import { waitUntil } from '@vercel/functions' - Installation:
bun add @vercel/functions - Usage: wrap async operations with
waitUntil() - Ensures code execution completion even after response sent
- Maintains Lambda instance alive for background operations
Vercel's Infrastructure Architecture
- Vercel doesn't use AWS Gateway directly
- Custom orchestration layer before Lambda execution
- User → Vercel Function Edge Layer → Lambda
- Traditional: Gateway kills Lambda immediately after response
- Vercel's approach: keeps Lambda alive longer through custom protocol
- Uses non-traditional web requests (possibly WebSocket) between edge layer and Lambda
- Enables flexible Lambda management without standard AWS limitations
Real-World Use Cases
- Logging operations
- Database writes that don't block user response
- Background image generation
- Any async work not required for immediate response
- Goal: fastest possible user response + background processing
Advanced Streaming Example (Guillermo's Demo)
- Reimplemented React Server Components without React/Next.js
- Used confetti library via CDN embed
- Transform stream with readable/writable pattern
- Immediate "Hello" response, then streamed "World" + confetti after 1.5 seconds
- Clever hack: 1,024 nbsp characters to exceed minimum chunk size for gzip/brotli compression
- Forces immediate chunk flush to user
- Pure HTML streaming without framework dependencies
Business Value Proposition
- Vercel pricing justified by avoiding complex infrastructure building
- Alternative: build custom queuing/management system (years of engineering effort)
- Fundamental shift: prioritize fast user response + background processing
- Can still use traditional await when blocking behavior needed
