We replaced Node.js with Bun for 5x throughput
9 hours ago
- #Performance Optimization
- #Memory Leak
- #Bun
- Node.js replaced with Bun in Firestarter service, resulting in 5x throughput increase.
- Memory leak discovered in Bun's HTTP model due to unresolved Promise<Response> on client disconnect.
- SQLite replaced with composite-key Map for O(1) lookup, improving performance 2.2x.
- Bun.serve() and optimizations (Zod replacement, direct header access, logging fix) doubled throughput.
- Compiled Bun binary further enhanced throughput by 14%, reduced latency, and decreased image size.
- Fixed memory leak by resolving Promise<Response> with status 499 on client disconnect.
- Production metrics showed stable memory (~85 MB), reduced CPU spikes, and minimal event loop lag.
- Key lessons: profile before optimizing, understand Bun's HTTP model, compile binaries, benchmark each step.
- Debugging Bun: use custom heap stats, address AbortSignal double-fire, monitor native memory with mimalloc.
- Used k6 for load testing with controller simulator and realistic payloads for performance validation.