AI summary by TwinMind
How To Avoid Big Serverless Bills
Theo - t3․gg
26m 53s
Introduction & Sponsor Disclosure
- Vercel no longer channel sponsor - can now discuss potential issues freely
- Built demo app showcasing common mistakes causing high Vercel bills
- Focus on bandwidth, serverless compute, and optimization strategies
Bandwidth Issues - Public Directory Assets
- Problem: Files in public directory automatically served via expensive CDN
- Rule: Don't put files >4KB in public directory
- Solution: Use external file hosts (UploadThing, S3, R2, Vercel Blob)
- Example: Video files in public directory create massive bandwidth costs
- UploadThing Integration:
- Zero egress charges
- Drag-and-drop replacement for public assets
- Copy file URL and swap source paths
Image Optimization Billing Issues
- Default limits: 1,000 free optimizations on free plan, 5,000 on pro
- Cost: $5 per 1,000 additional optimizations
- Critical mistake: Overly permissive image domain configuration
- Bad: Allowing all GitHub user content domains
- Good: Restrict to specific paths like
/pokeapi/sprites/master/**
- UploadThing specific: Use app-specific URLs with app IDs to prevent abuse
- Recommendation: Use Vercel image optimization until 5,000 images, then consider alternatives
Serverless Function Optimization
- N+1 Query Problem: Sequential database calls causing 4x longer request times
- Bad pattern: Separate blocking calls for posts, comments, author, comment users
- Quick fix: Use Promise.all for parallel execution of independent queries
- Best solution: Single Prisma query with relations/includes
- Real example: Reduced 20+ second requests (often timing out) to ~2 seconds
- Vercel concurrency: New feature allows request sharing during external waits
Caching Strategies
- unstable_cache: Cache expensive database operations
- Pattern: Wrap slow queries, use revalidateTag for cache invalidation
- Common use case: User data fetching on every request
- Benefits: Faster responses + lower compute costs
- DIY alternative: Manual KV store implementation shown
Static vs Dynamic Pages
- Problem: export const dynamic = 'force-dynamic' forces server rendering
- Check: Vercel build output shows static (○) vs dynamic (f) pages
- Goal: Keep static pages static (terms, blog, docs)
- Verification: Check deployment summary in Vercel dashboard
Image Optimization Alternatives
- Threshold: Vercel image optimization good until 5,000 images
- Alternatives: Cloudflare, other providers (cheaper but worse DX)
- Upcoming: image.engineering - author's new service (already dogfooding)
Analytics Cost Comparison
- Vercel Analytics: $14 per 100K events (web analytics, not product analytics)
- PostHog alternative: $0.50 per 100K events after 1M free
- Recommendation: Avoid Vercel analytics, use PostHog instead
- Distinction: Web analytics (page views) vs product analytics (user journeys)
Spend Management
- Feature: Set spending limits in Vercel dashboard
- Tradeoff: Service goes down when limit reached
- Author's experience: Never enabled due to low per-request costs
- Example: Worst DDoS attack cost ~$80 after hours of millions of requests
- Napkin math: Would need 1 billion users for $100K/month bill
