URLShortner Microservice
Overview
The URLShortner microservice provides URL shortening functionality for the Publisher platform. It creates short, unique identifiers for long URLs and provides redirection services, enabling easier sharing and tracking of URLs in marketing campaigns and user communications.
Business Purpose
This service enables the Publisher platform to: - Create shortened URLs for marketing campaigns and email communications - Provide clean, trackable links for analytics and reporting - Reduce URL length for better user experience in SMS and social media - Enable centralized URL management and redirection control
Architecture
Service Type
- Deployment: AWS Lambda serverless function
- Runtime: Node.js
- Database: AWS DynamoDB
- Trigger: HTTP API Gateway
Key Components
- URLShortner.js: Main Lambda handler for URL operations
- URLShortenerModel.js: DynamoDB data model using Dynogels ORM
- Configuration: Environment-specific settings
Data Flow
graph TD
A[HTTP Request] --> B{Request Method}
B -->|POST| C[Create Short URL]
B -->|GET| D[Retrieve & Redirect]
C --> E[Generate URL Key]
E --> F[Save to DynamoDB]
F --> G[Return Short URL Key]
D --> H[Lookup URL Key]
H --> I[DynamoDB Query]
I --> J{URL Found?}
J -->|Yes| K[302 Redirect]
J -->|No| L[404 Not Found]
Dependencies
External Services
- AWS DynamoDB: URL storage and retrieval
- AWS API Gateway: HTTP request routing
- AWS Lambda: Serverless compute platform
NPM Dependencies
dynogels: DynamoDB ORM for Node.jsidgen: Unique identifier generationjoi: Data validation schema
Configuration
Environment-Specific Configs
config.js: Development configurationconfig.int.js: Integration environmentconfig.prod.js: Production environment
Key Configuration Parameters
- ENV: Environment identifier (DEV/INT/PROD)
- Region: AWS region (us-east-1)
API Endpoints
POST /
Creates a new shortened URL.
Request Body:
{
"url": "https://example.com/very/long/url/path"
}
Response (200 OK):
{
"urlkey": "generated_16_char_key"
}
Error Response (500):
{
"error": "Error details"
}
GET /{urlkey}
Redirects to the original URL associated with the short key.
Parameters:
- urlkey: The 16-character unique identifier
Response (302 Found):
- Headers: location: original_url
- Body: Empty (redirect)
Error Response (404 Not Found):
URL with url key {urlkey} does not exist
Data Model
DynamoDB Table: URLShortner
Schema:
- Hash Key: urlkey (String) - 16-character unique identifier
- Attributes:
- url (String) - Original long URL
Table Configuration: - Table Name: URLShortner - Primary Key: urlkey (String) - Region: us-east-1
URL Key Generation
Key Properties
- Length: 16 characters
- Character Set: Alphanumeric (generated by idgen library)
- Uniqueness: Statistically unique across all generated keys
- Format: Random string (e.g., "a1b2c3d4e5f6g7h8")
Generation Process
- Generate 16-character random string using idgen
- Store mapping in DynamoDB
- Return key to client
- No collision detection (relies on statistical uniqueness)
Error Handling
Common Error Scenarios
- Invalid HTTP Method: Returns 400 Bad Request
- URL Not Found: Returns 404 Not Found for GET requests
- DynamoDB Errors: Returns 500 Internal Server Error
- Invalid JSON: Parsing errors for POST requests
Error Responses
- 400: Invalid HTTP method
- 404: Short URL not found
- 500: Database or internal server errors
Performance Characteristics
Scalability
- Serverless: Automatically scales with AWS Lambda
- DynamoDB: Scales based on configured read/write capacity
- Stateless: No server-side session management
Latency
- Cold Start: Initial Lambda invocation may have higher latency
- Warm Requests: Sub-second response times
- DynamoDB: Single-digit millisecond read/write operations
Security Considerations
Access Control
- No authentication required (public service)
- Rate limiting handled by API Gateway
- Input validation through Joi schemas
Data Protection
- URLs stored in plaintext (no sensitive data encryption)
- No personal data collection
- Short keys are not predictable
Development
Local Setup
- Install dependencies:
npm install - Configure AWS credentials
- Set up local DynamoDB or use AWS DynamoDB
- Run tests using Test.js
Testing
- Test.js: Basic functionality testing
- Tests both POST (create) and GET (redirect) operations
- Manual testing with sample URLs
Deployment
- Package as AWS Lambda deployment package
- Configure API Gateway endpoints
- Set up DynamoDB table with appropriate permissions
Monitoring and Logging
AWS CloudWatch
- Lambda execution logs
- Error tracking and alerting
- Performance metrics (duration, memory usage)
DynamoDB Metrics
- Read/write capacity utilization
- Throttling events
- Error rates
Custom Logging
- URL creation events
- Redirect access patterns
- Error conditions and debugging
Related Services
This service may integrate with: - Email Marketing Services: Shortened URLs in email campaigns - Analytics Services: Click tracking and reporting - Campaign Management: Marketing campaign URL generation - Social Media Tools: Shortened URLs for social sharing
Troubleshooting
Common Issues
- DynamoDB Connection Errors: Check AWS credentials and region configuration
- Lambda Timeout: Monitor execution duration and memory allocation
- High Error Rates: Check DynamoDB capacity and throttling
- Invalid URLs: Validate input URL format and accessibility
Monitoring Points
- Lambda invocation count and error rate
- DynamoDB read/write operations
- API Gateway request/response metrics
- URL creation vs. access ratio
Limitations
Current Constraints
- No URL expiration mechanism
- No analytics or click tracking
- No custom short URL aliases
- No bulk URL operations
- No URL validation beyond basic format
Potential Enhancements
- Click analytics and reporting
- URL expiration and cleanup
- Custom short URL aliases
- Bulk URL creation API
- URL validation and safety checks
- Rate limiting per client
- Admin interface for URL management