Skip to content

Shortlyx is a production-grade URL shortening service engineered for high throughput and low latency. It leverages a layered architecture with Redis-buffered writes to handle traffic spikes efficiently, decoupling real-time request processing from database persistence.

Notifications You must be signed in to change notification settings

vknow360/Shortlyx

Repository files navigation

Shortlyx - High-Performance URL Shortener

Shortlyx is a production-grade URL shortening service engineered for high throughput and low latency. It leverages a layered architecture with Redis-buffered writes to handle traffic spikes efficiently, decoupling real-time request processing from database persistence.

Architectural Highlights

1. High-Throughput Write Buffering

Traditional URL shorteners hit the database for every click event, creating a bottleneck at scale. Shortlyx implements a Write-Behind strategy:

  • Ingestion: Click events are atomically incremented in Redis (INCR).
  • Buffering: Affected keys are tracked in a Redis Set.
  • Persistence: A background worker asynchronously flushes aggregated stats to PostgreSQL.
  • Result: Transforms O(N) database writes into O(1) batch updates per interval, significantly increasing write capacity.

2. Layered "Hexagonal" Design

The codebase follows strict separation of concerns to ensure maintainability and testability:

  • Handlers (Transport): HTTP/JSON logic (Gin).
  • Service (Business Logic): Validations, Caching rules, and domain logic.
  • Repository (Data Access): Abstractions for PostgreSQL and Redis.

3. Rate Limiting & Security

  • Token Bucket Algorithm: Implemented via Redis to prevent abuse.
  • Secure Headers: Configurable Proxy Trust for accurate IP rate limiting in load-balanced environments.

4. Optimized ID Generation

  • Base62 Encoding: Efficiently maps unique Integer IDs to short alphanumeric strings.
  • Database Sequences: Utilizes PostgreSQL BIGSERIAL for collision-free, sequential ID generation, ensuring shortest possible URLs.

Technology Stack

  • Language: Go (Golang) 1.25+
  • Web Framework: Gin
  • Database: PostgreSQL 15+
  • Caching / Broker: Redis 7.x
  • Containerization: Docker Support (Planned)

API Reference

1. Shorten URL

POST /shorten

Request:

{
  "long_url": "https://www.google.com/search?q=system+design",
  "ttl_days": 7
}

Response:

{
  "short_url": "http://localhost:8080/1b"
}

2. Resolve URL

GET /:code

  • Redirects to the original long_url.
  • Asynchronously increments click count via Redis buffer.

3. Get Stats

GET /api/stats/:code

Response:

{
  "short_code": "1b",
  "long_url": "https://...",
  "click_count": 42,
  "created_at": "2024-01-01T12:00:00Z"
}

Setup & Installation

Prerequisites:

  • Go 1.25+
  • PostgreSQL
  • Redis

Configuration: Create a .env file in the root directory:

PORT=8080
APP_ENV=development
DATABASE_URL=postgres://user:pass@localhost:5432/shortlyx?sslmode=disable
REDIS_URL=redis://localhost:6379/0

Running the Application:

# Install dependencies
go mod download

# Run migrations (ensure DB exists)
# (Manual SQL execution of migrations/001_create_urls.sql required)

# Start server
go run main.go

About

Shortlyx is a production-grade URL shortening service engineered for high throughput and low latency. It leverages a layered architecture with Redis-buffered writes to handle traffic spikes efficiently, decoupling real-time request processing from database persistence.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages