A URL shortener that leverages Redis for caching and ensuring unique URL entries, alongside RabbitMQ for managing a queue of pre-generated shortened URLs. This setup not only speeds up response times by facilitating immediate URL retrieval from RabbitMQ but also minimizes database interactions and storage usage by preventing duplicate entries for the same URL, resulting in a more efficient system.
- MessageQueue: Leverages RabbitMQ to store pre-generated shortened URLs in a queue, facilitating rapid retrieval and enhancing scalability by minimizing response times during peak traffic periods.
- IHostedService: Operates in the background to generate shortened URLs, allowing the system to instantly respond to shortening requests without real-time generation. This approach not only accelerates response times but also alleviates database strain by eliminating the need for continuous URL uniqueness checks during high traffic periods.
- Caching: Employed to swiftly retrieve previously shortened URLs and ensure record uniqueness, reducing the necessity for frequent database interactions and optimizing system performance.
- Polly: Implements retry mechanisms for reliable URL fetching.
- Redirection: Facilitates user redirection using the shortened URL.
- RateLimiting: Implement rate limiting to safeguard the API from excessive requests and potential abuse.
- reCaptcha: Add bot detection mechanisms during URL submission to prevent automated spam.
- Serilog & Seq: Integrate structured logging for better traceability and debugging capabilities.
- Prometheus & Grafana: Set up system metrics monitoring and visualization to keep an eye on performance and potential bottlenecks.
- Analytics: Collect and analyze metrics on how often each shortened URL is accessed, offering insights into user behavior.
- Custom Aliases: Allow users to specify custom short names for their URLs, e.g.,
short.url/myname
instead of a random string. - Expiration: Introduce the ability to set expiration dates for shortened URLs, if they are not accessed for a while.
- QR Code Generation: Offer a QR code representation for each shortened URL, facilitating sharing in various formats.
- Bulk Shortening: Develop an endpoint that allows users to submit and shorten multiple URLs in a single request.
-
Background Service: Continuously generates shortened URLs in the background and dispatches them to a RabbitMQ queue, ready for immediate use.
-
Shortening Endpoint:
- Initially checks the Redis cache to determine if the URL has been previously shortened, ensuring unique records without frequent database interactions.
- If not found, consults the database to check if the URL has been previously shortened (it might be in the database but expired from the cache).
- If still not found, retrieves a pre-generated shortened URL from the RabbitMQ queue, utilizing a retry logic implemented with Polly.
- Saves the newly generated shortened URL in both the Redis cache, for rapid future retrieval, and the database, ensuring data persistence.
-
Redirection Endpoint: Retrieves the original URL associated with the shortened URL directly from the database, given that the IDistributedCache lacks an implementation for key (originalUrl) retrieval, a functionality that is generally uncommon, and subsequently redirects the user to the original URL.
- .NET SDK
- Docker
- PostgreSQL
Create a database called UrlShortenerDb
then run the sql scripts located Database/Migrations
.
docker-compose up -d
dotnet run
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Distributed under the Apache-2.0 License. See LICENSE
for more information.