The TinyURL Backend Service is a URL shortening application developed in C#. This service allows users to shorten lengthy URLs into more manageable and shareable forms. The shortened URLs can then be expanded back to their original form. This service is especially useful for social media sharing, marketing, and managing large URLs.
- URL Shortening: Converts a long URL into a shorter, more manageable version.
- URL Expansion: Reverts a shortened URL back to its original long URL.
- Custom Memory Caching: Implements an efficient caching mechanism for quick retrieval of URLs.
- MongoDB Integration: Utilizes MongoDB for persistent storage of URL mappings.
- Base62 Encoding: Employs Base62 encoding for generating the shortened URLs.
- Configurable Settings: Provides customizable settings for database connection, URL shortening, and caching.
- Core service for shortening and expanding URLs.
- Validates URLs, checks cache, queries the database, and handles the generation of shortened URLs.
- A custom memory caching system that stores recently accessed URLs.
- Optimizes performance by reducing database queries.
- Implements Base62 encoding and decoding.
- Ensures the short URLs are compact and unique.
- The API controller handling HTTP requests.
- Provides endpoints for shortening and expanding URLs, and redirecting short URLs.
appsettings.jsoncontains configurable parameters like MongoDB connection details, base URL for shortening, and cache capacity.
- Shorten a URL: Send a POST request to
/url/shortenwith the original URL in the request body. - Expand a URL: Send a GET request to
/url/expand/{shortUrl}with the short URL code. - Redirect Short URL: Access a short URL directly to get redirected to the original URL.
End of README
The cache in the TinyURL Backend Service is designed to store recently accessed URLs, enhancing retrieval speed and reducing the need for frequent database queries.
- LRU (Least Recently Used) Strategy: Implements an eviction policy where the least recently used items are removed first upon reaching full capacity.
- Concurrent Access: Utilizes
ConcurrentDictionaryfor thread-safe operations, suitable for a multi-threaded environment. - Doubly Linked List: Maintains the order of items based on usage, facilitating efficient additions and deletions.
In the design of the custom memory cache for the TinyURL Backend Service, I implemented a size-limitation approach based on fixed capacity. This decision aimed to balance memory efficiency with performance.
- Fixed Capacity: The cache has a predefined maximum capacity, limiting the number of items it can hold.
- Memory Efficiency: Limits memory usage, important in resource-constrained environments.
- Simplicity and Predictability: Straightforward and predictable in terms of memory usage.
- Prevents Memory Bloat: Avoids memory leaks or bloat in an unbounded cache.
- Fast Lookup: Ensures quick access for frequently accessed data.
- Potential for Cache Misses: If the cache is too small, it might lead to increased cache misses.
- Static Allocation: Doesn't allow dynamic resizing based on workload, which could be inefficient or insufficient depending on the situation.
This approach was chosen for its balance between performance and resource usage, with the key being to size the cache appropriately based on expected workload and system resources.