This project provides a URL shortening service with a scalable architecture using Docker, Redis, and Nginx for load balancing. The services can generate shortened URLs, handle redirections, and ensure smooth traffic routing.
- URL Shortening: Allows users to shorten long URLs into a simple, easy-to-share format.
- Redirection Service: Redirects users from shortened URLs to the original long URL.
- Redis Caching: Uses Redis to cache data and improve response times.
- Scalable Architecture: Can easily scale to meet high traffic demands.
- Nginx Proxy: Acts as a reverse proxy to handle and route traffic between services efficiently.
- Health Check: Health endpoint (
/healthz
) to monitor the service's status. - Security Features: Uses secure headers and secrets (JWT, Google OAuth, etc.).
This project is split into three services:
- Redis: For caching the shortened URLs and enhancing performance.
- URL Shortener Service: Responsible for generating the shortened URLs and handling requests.
- URL Redirection Service: Responsible for redirecting the users to the original URLs when accessing a shortened URL.
- Nginx: Acts as a reverse proxy to distribute the traffic between the services.
- Nginx: The entry point for user requests. Nginx routes
/api/shorten
requests to the URL shortener and/api/redirect
requests to the URL redirection service. - Redis: Redis acts as an intermediary data store, caching the shortened URLs to boost speed and reduce the need for repeated database calls.
- Docker: Used for containerizing the services.
- Nginx: As a reverse proxy for load balancing and traffic routing.
- Redis: To store and cache shortened URLs.
- OAuth (Google): For user authentication and security.
- JWT: Secure authentication and authorization.
-
Docker installed on your machine.
-
Docker Compose installed.
-
Environment variables set up in a
.env
file:REDIS_URL=<Your Redis URL> DATABASE_URL=<Your Database URL> FRONTEND_URL=<Your Frontend URL> GOOGLE_CLIENT_ID=<Google OAuth Client ID> GOOGLE_CLIENT_SECRET=<Google OAuth Client Secret> GOOGLE_CALLBACK_URL=<Google OAuth Callback URL> JWT_SECRET_KEY=<Secret key for JWT> SESSION_SECRET=<Secret key for sessions> GUEST_ID=<Guest ID>
To run the project, follow these steps:
-
Clone the repository:
git clone https://github.com/Abdev1205/slink.git cd url-shortener
-
Build and start the services using Docker Compose:
docker-compose up --build
-
The services will start in the following order:
- Redis
- URL Shortener Service
- URL Redirection Service
- Nginx
-
Once the containers are running:
- Access the URL shortening API at
http://localhost/api/shorten
. - Access the redirection service at
http://localhost/api/redirect
.
- Access the URL shortening API at
-
To check the health of the services:
curl http://localhost/healthz
- Separation of Concerns: Different services handle distinct tasks (shortening URLs, redirecting, caching).
- Environment Variables: All sensitive configurations such as database URLs, JWT secrets, and OAuth credentials are managed through environment variables.
- Caching with Redis: Caching the shortened URLs in Redis improves performance by reducing repeated database lookups.
- Dockerized Services: Each service is containerized, allowing for easy scaling and management of the services.
- Security Best Practices:
- Secure headers in Nginx.
- Use of HTTPS for proxies with
proxy_ssl_protocols
to secure data transmission.
- Nginx Debug Logs: Debug-level logging enabled for Nginx to easily track issues during development.
- Health Check Endpoint: Provides a simple
/healthz
endpoint for monitoring and automated health checks. - CI/CD Friendly: Designed for seamless integration with CI/CD pipelines using Docker.