Short and Sweet: Building url.dmt.dev (in less than 5MB)

Background & Motivation

Having implemented URL shorteners multiple times as technical assessments during interviews, I was intimately familiar with their architectural patterns and common pitfalls. These technical tests typically focus on the basic mechanics: generate a short code, store a mapping, and handle redirects. However, building a production-ready URL shortener for real-world use presents a different set of challenges and considerations.

My primary motivation was practical: I needed a reliable way to share and track links to my blog posts and technical articles across various platforms. While commercial solutions exist, having complete control over the analytics and infrastructure made more sense for my use case.

Technical Implementation

The service is hosted at dmt.dev/url, built as a lightweight Lambda function that integrates with DynamoDB. One of the key achievements was keeping the entire package size under 5MB, ensuring quick cold starts and efficient deployment. This was accomplished through:

  • Careful dependency management
  • Efficient code bundling
  • Minimizing external libraries
  • Strategic use of AWS SDK modular imports

Architecture Decisions

Nothing groundbreaking here - but the implementation does reflect lessons learned from previous projects:

  1. Database Design: Previous implementations taught me the importance of proper indexing for quick lookups, especially when thinking at scale.
  2. Collision Handling: Rather than using simple incremental IDs, implementing a robust hash-based system with collision detection
  3. Analytics Storage: Separating redirect mapping from analytics data to prevent performance degradation
  4. Rate Limiting: Implementing proper throttling to prevent abuse

Core Features

The system provides:

  1. Fast Redirects: Sub-100ms response times for cached routes
  2. Analytics Tracking:
    • Click counts
    • Referrer tracking
    • Geographic distribution
    • Time-based access patterns
  3. API Access: RESTful endpoints for programmatic access
  4. Abuse Prevention: Rate limiting and URL validation

Technical Architecture

AWS Lambda Implementation

The Lambda function is remarkably efficient:

  • Cold start times < 300ms
  • Package size < 5MB
  • Average response time ~80ms

DynamoDB Schema

interface UrlMapping {
  shortCode: string;      // Partition key
  longUrl: string;       // Original URL
  created: number;       // Timestamp
  clicks: number;        // Counter
  lastAccessed: number;  // Last click timestamp
}

Analytics Integration

Unlike typical tech test implementations, this production version includes comprehensive analytics:

  1. Click Tracking: Asynchronous logging ensures redirect speed isn't impacted
  2. Traffic Analysis: Identifying traffic patterns and popular access times
  3. Referrer Tracking: Understanding where links are being shared
  4. Geographic Data: Regional access patterns and user distribution

Deployment and Hosting

The service is integrated into my main domain at dmt.dev/url, leveraging existing infrastructure and SSL certificates. This integration provides:

  • Secure HTTPS endpoints
  • Custom domain benefits
  • Unified monitoring
  • Shared infrastructure cost benefits

Lessons from Production

The transition from technical test to production system highlighted several key differences:

  1. Scale Considerations: While tech tests focus on algorithmic efficiency, production systems need practical scalability
  2. Error Handling: Production requires robust error handling and logging
  3. Monitoring: Real-time monitoring and alerting become crucial
  4. Cost Optimization: Balancing performance with AWS costs

Future Enhancements

While the current implementation serves its purpose effectively, planned improvements include:

  1. Custom Short Codes: Allow user-defined short codes for specific URLs
  2. Enhanced Analytics: More detailed traffic pattern analysis
  3. API Expansion: Additional endpoints for bulk operations
  4. Automated Cleanup: Expiration of unused short codes

Conclusion

Firstly; this implementation is built with cost in mind - utilising serverless this costs nothing to run. Building a production URL shortener has been an interesting journey from technical test implementations to a real-world service. The sub-5MB package size and efficient architecture demonstrate that lightweight solutions can effectively serve production needs. While the basic concept of URL shortening remains simple, the addition of analytics, security measures, and production-grade reliability transforms it into a genuinely useful tool for tracking and managing shared links.

Further Reading

  • System Design Interview (Amazon)
  • System Design Interview: Volume 2 (Amazon)
  • Designing Data-Intensive Applications: The Big Ideas Behind Reliable, Scalable, and Maintainable Systems (Amazon)