Skip to content

3r3bu5x9/EchoLink

Repository files navigation

⚡ EchoLink — Scalable URL Shortener with Custom Rate Limiting & Analytics Platform

A distributed URL shortener built with .NET microservices, showcasing real-world system design principles — including API Gateway with YARP, JWT authentication, Redis-based counter and response caching, Custom Rate Liming, RabbitMQ-driven analytics, and PostgreSQL persistence (with EF Core and Dapper).


🌟 System Highlights

  • Gateway-First Architecture: Centralized entry point using YARP for routing, JWT validation, TLS termination, and request transformation.
  • Precision Rate Limiting: Custom sliding-window and token-bucket algorithms for per-user throttling.
  • Event-Driven Analytics: Click events published via RabbitMQ, aggregated asynchronously in Redis, and flushed atomically to PostgreSQL.
  • Cache-Optimized Redirection: Redis-backed caching layer enabling ultra-fast short-link resolution with fallback to PostgreSQL.
  • DDD-Inspired Services: Independently deployable microservices modeled around clear bounded contexts — Shortening, Redirection, and Analytics.

🏗️ High Level Design

alt text


⚙️ Microservices

🚪 Gateway Service

Responsibilities

  1. Authentication & Authorization
    • Verifies and validates JWT tokens issued by the Auth service.
    • Applies fine-grained authorization policies per route and downstream service.
  2. Reverse Proxy & Routing
    • Acts as the system’s entry point and reverse proxy, routing requests to appropriate downstream microservices via YARP.
  3. Rate Limiting
    • Protects downstream services from overload using custom rate-limiting middlewares, including:
      • Sliding-Window Log (precise per-user request tracking)
      • Token Bucket (smoother refill-based throttling)
  4. Request / Response Transformation
    • Injects contextual metadata (e.g., UserId from JWT claims) into outgoing requests.
    • Normalizes or aggregates responses from multiple services when required.
  5. Response Aggregation
    • For composite endpoints, aggregates partial results from multiple services into unified API responses.
  6. TLS Termination
    • Terminates HTTPS traffic, handling encryption/decryption before routing to internal services.

Tech Stack

  • YARP (Yet Another Reverse Proxy)
  • PostgreSQL (for configuration & rate-limit persistence via EF Core)
  • ASP.NET Core 9.0

🔗 URL Shortening Service

Responsibilities

  1. URL Shortening
    • Accepts long URLs and generates unique short codes using configurable strategies:
      • Counter-Based Strategy: Base-62 encoding of incrementing counters from Redis (synced to Postgres) (fast, deterministic).
      • Hash-Based Strategy: Base-62 encoding of truncated MD5 hashes (compact but requires collision handling).
  2. Persistence & Expiration
    • Persists mappings of LongUrl ↔ ShortCode along with metadata (user, creation date, expiry).
    • Supports TTL-based cleanup for expired links.

Tech Stack

  • Redis (for counters and caching)
  • PostgreSQL with EF Core
  • ASP.NET Core 9.0

🔁 URL Redirection Service

Responsibilities

  1. Short URL Resolution
    • Resolves short codes to their corresponding original URLs via Redis cache or database lookup.
  2. Response Caching
    • Implements cache-aside pattern in Redis to minimize repetitive DB reads.
  3. Event Publishing
    • Publishes asynchronous click-tracking events (e.g., ShortUrlClicked) to RabbitMQ, consumed later by the Analytics Service.

Tech Stack

  • Redis (response cache)
  • PostgreSQL (via Dapper for lightweight querying)
  • RabbitMQ (for event messaging)
  • ASP.NET Core 9.0

📊 Analytics Service

Responsibilities

  1. Event Consumption & Aggregation
    • Consumes click events from RabbitMQ and aggregates metrics (click counts, timestamps) in Redis.
  2. Periodic Flush
    • Periodically flushes batched analytics to PostgreSQL using atomic upserts (ON CONFLICT DO UPDATE).
  3. API Exposure
    • Provides REST endpoints for analytics queries (e.g., total clicks, top links per user).

Tech Stack

  • Redis (temporary counters and batch buffers)
  • PostgreSQL with EF Core
  • RabbitMQ (message queue for analytics ingestion)
  • ASP.NET Core 9.0

🔮 Future enhancements

  • Kafka-based event propagation between services
  • UI dashboard for click analytics
  • Sliding window rate limiting using Redis with atomic operation via Lua scripts
  • Read replica of URL database via CDC with Debezium for scaling reads and fault tolerance
  • Cassandra for analytics data persistence
  • Shard PostgreSQL for scaling writes
  • Load balance reads with YARP for scaling reads and fault tolerance
  • Write-Through and Write-Behind caching in Redis via Redis Gears.

📡 API Endpoints (via Gateway)

All routes are proxied through the Gateway Service (http://localhost:5403), which handles authentication, routing, and rate limiting.

# Method Endpoint Description Auth
1 POST /user/register Registers a new user with email and password ❌ Public
2 POST /user/login Logs in a user and returns a JWT token ❌ Public
3 GET / Health check for the Gateway ❌ Public
4 POST /shorten Shortens a long URL and stores it with owner metadata ✅ Bearer JWT
5 GET /user/urls Returns all shortened URLs created by the authenticated user ✅ Bearer JWT
6 GET /echo.link/{shortCode} Redirects the short URL to its original destination ❌ Public
7 GET /analytics Returns aggregated analytics (click counts, timestamps, etc.) ✅ Bearer JWT

🔐 Authentication

  • Endpoints marked ✅ require a valid Bearer JWT in the Authorization header:Authorization: Bearer <your_jwt_token>

🐳 How to Run (Docker Compose)

🧩 Prerequisites

  • Docker & Docker Compose installed

🚀 Quick Start

🧭 Clone the Repository

git clone https://github.com/yourusername/EchoLink.git
cd EchoLink

🐳 Start All Services

docker compose up -d

This spins up:

  • gateway-service
  • shortening-service
  • redirection-service
  • analytics-service
  • auth-service
  • postgres-urls
  • postgres-users
  • postgres-analytics
  • redis
  • rabbitmq

✅ Verify Containers

docker ps

🌐 Access the Gateway

http://localhost:5403

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors