웹 앱을 위한 서버 사이드 QR Code 생성
Generating QR codes on the server: dynamic image endpoints, caching strategies, SVG vs PNG trade-offs, and CDN distribution.
Server-Side QR Code Generation for Web Apps
Generating QR codes on the server enables dynamic image endpoints, consistent rendering, and CDN caching for high-traffic applications.
Dynamic Image Endpoints
Serve QR codes as images from URL parameters:
GET /api/qr?data=https://example.com&size=300&ec=M
The server generates the QR code on the fly and returns a PNG or SVG response. This pattern works with any web framework.
Caching Strategy
QR code generation is deterministic — the same input always produces the same output. Cache aggressively:
- In-memory cache: LRU cache keyed on input hash. Fastest for hot paths.
- CDN caching: Set
Cache-Control: public, max-age=86400for static data QR codes. - File system cache: Write generated images to disk; serve directly via Nginx.
- Redis cache: Distributed cache for multi-server deployments.
SVG vs PNG Trade-offs
| Format | Generation Speed | File Size | Scalability | Client Compatibility |
|---|---|---|---|---|
| SVG | Very fast | Small (2-10 KB) | Infinite | Modern browsers |
| PNG | Moderate | Medium (5-50 KB) | Fixed resolution | Universal |
Recommendation: SVG for web display, PNG for email embedding and print.
Security Considerations
When exposing a QR code generation API:
- Validate input data to prevent injection
- Rate limit requests (prevent abuse as a free QR generation service)
- Sanitise SVG output (prevent XSS via embedded scripts)
- Set appropriate Content-Type headers
- Consider authentication for internal-use endpoints
Architecture Patterns
Synchronous: Generate and return in the same request. Simple but adds latency.
Async with polling: Return a job ID immediately; client polls for completion. Good for batch generation.
Pre-generation: Generate QR codes at data creation time (e.g., when a product is added). No latency at request time.
Key Takeaways
- Dynamic QR endpoints with caching serve most web application needs
- SVG output is fastest and most scalable for web display
- Cache aggressively — QR generation is deterministic
- Rate limit and validate input for public-facing endpoints
- Pre-generate for high-traffic pages to eliminate runtime latency