Xây dựng Dịch vụ Rút gọn QR Code

Embed This Widget

Theme


      
    

Widget powered by . Free, no account required.

Architecture guide for building a dynamic QR code service: URL shortener, redirect engine, analytics pipeline, and scaling.

Building a QR Code Shortener Service

This guide outlines the architecture for building a complete dynamic QR code service with URL shortening, redirect handling, and scan analytics.

System Architecture

Client → Load Balancer → Redirect Server → Database
                                        → Analytics Pipeline
                                        → QR Generation Service

Core Components

1. Short Code Generator - Generate unique 6-8 character alphanumeric codes - Use cryptographically random generation (not sequential) - Check for collisions against existing codes - Consider base62 encoding (a-z, A-Z, 0-9) for URL-safe codes

2. Redirect Engine - Accept requests: GET /{short_code} - Look up destination in database (with Redis cache) - Log scan metadata asynchronously (do not block the redirect) - Return HTTP 302 redirect (302 for dynamic destinations, 301 for permanent)

3. Analytics Pipeline - Async event ingestion (queue-based: Redis, RabbitMQ, or Kafka) - IP geolocation lookup (MaxMind GeoLite2) - User-Agent parsing (device, OS, browser) - Time-series aggregation for dashboards - Raw event storage for detailed analysis

4. Admin Interface - CRUD operations for URL mappings - Analytics dashboard (scans, geography, devices) - Batch creation support - Expiration rules configuration - API key management

Database Schema

CREATE TABLE short_urls (
    id SERIAL PRIMARY KEY,
    short_code VARCHAR(10) UNIQUE NOT NULL,
    destination_url TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT NOW(),
    expires_at TIMESTAMP,
    is_active BOOLEAN DEFAULT TRUE,
    max_scans INTEGER
);

CREATE TABLE scan_events (
    id BIGSERIAL PRIMARY KEY,
    short_url_id INTEGER REFERENCES short_urls(id),
    scanned_at TIMESTAMP DEFAULT NOW(),
    ip_address INET,
    user_agent TEXT,
    country_code CHAR(2),
    city VARCHAR(100)
);

Performance Targets

Metric Target
Redirect latency < 50ms (P99)
Throughput 1,000+ redirects/second
Analytics delay < 5 seconds (event to dashboard)
Uptime 99.9%

Key Takeaways

  • The redirect engine must be extremely fast (sub-50ms) — cache with Redis
  • Log analytics asynchronously to avoid blocking redirects
  • Use HTTP 302 for dynamic destinations (not 301, which browsers cache)
  • Queue-based analytics ingestion handles traffic spikes gracefully
  • Start simple; scale components independently as traffic grows