Resolve Short Code
Resolve a short code and redirect to the original URL.Endpoint
Path Parameters
The 4-10 character alphanumeric code (e.g.,
aB3xYz7). Must match the pattern [a-zA-Z0-9]{4,10}.Authentication
This endpoint is public and does not require authentication.
Response
The endpoint does not return a JSON body. Instead, it issues an HTTP redirect using theLocation header.
Response Headers
The destination URL. Clients should follow this redirect automatically.
Status Codes
Short code successfully resolved. The
Location header contains the original URL.Behavior:- Increments the
redirectCountfor this link - Updates the
avgRedirectMsusing Welford’s algorithm - Records the redirect latency from resolution to response
Short code not found or expired. Redirects to the frontend SPA error page instead of returning raw JSON.Error Types:Short code does not exist in the database.
- 404 Not Found
- 410 Gone
HTTP Status Code Matrix
| Short Code Status | HTTP Status | Location Header | Description |
|---|---|---|---|
| Valid & Active | 302 Found | Original URL | Successful redirect |
| Not Found | 302 Found | /r/{code}?error=not_found | Code doesn’t exist |
| Expired | 302 Found | /r/{code}?error=expired | Link has expired (8h TTL) |
| Deleted | 302 Found | /r/{code}?error=not_found | Soft-deleted by user |
Why Not 404 or 410 Directly?Returning
302 Found for all cases (even errors) provides a better user experience:- Browser users see a friendly error page instead of browser’s default error screen
- The frontend can customize error messages, suggest actions, and maintain branding
- API clients can still distinguish errors by parsing the
Locationheader
Latency Tracking
Each successful redirect measures and records its latency:- Start Timer: Record timestamp when request arrives
- Resolve Code: Query database for original URL
- Calculate Latency:
System.currentTimeMillis() - start - Update Statistics: Apply Welford’s algorithm to update running average
- Increment Counter: Increase
redirectCountby 1 - Return Redirect: Send
302 Foundresponse
Welford Algorithm Implementation
- Constant memory: No need to store all historical latencies
- Constant time: Update completes in microseconds
- Numerical stability: Minimizes floating-point errors
Latency Components
Measured latency includes:- Database query time
- Business logic execution
- Network round-trip time (excluded)
Automatic Cleanup
Expired anonymous links are automatically cleaned up every 15 minutes by a scheduled job:- Mark as Expired: Set
status = EXPIREDfor links past theirexpiresAttimestamp - Physical Deletion: Remove
DELETEDandEXPIREDrecords older than 7 days
Production ScalingThe cleanup job uses
@Scheduled which runs within the JVM. For production:- Keep backend
desiredCount=1to avoid duplicate job execution - Or migrate to ECS Scheduled Tasks / AWS EventBridge for better scalability
- Or use Quartz Scheduler with database-backed locking
Example Redirect Flow
Server Resolves Code
- Start latency timer
- Query database:
SELECT * FROM short_urls WHERE short_code = 'aB3xYz7' - Check status:
ACTIVE,EXPIRED, orDELETED - Calculate latency:
System.currentTimeMillis() - start
Error Handling Examples
Short Code Not Found
Request:Expired Link
Request:Path Pattern Matching
The endpoint uses Spring’s@GetMapping with a regex constraint:
aB3xYz7✓abc123✓WXYZ✓ (minimum 4 chars)abcd123456✓ (maximum 10 chars)
abc✗ (too short)abcdefghijk✗ (too long)abc-123✗ (contains hyphen)abc_123✗ (contains underscore)/api/urls✗ (handled by other endpoints)
Production Considerations
CDN Caching
Consider caching 302 redirects at CDN edge:
- Reduces server load
- Improves redirect speed
- Be careful with cache invalidation
Rate Limiting
Implement rate limits to prevent abuse:
- Per IP: 100 redirects/minute
- Per short code: 1000 redirects/hour
- Use Redis for distributed rate limiting
Analytics
Enhance tracking with:
- Geolocation data (MaxMind GeoIP)
- User agent parsing (referrer, device)
- Timestamp bucketing for trends
Monitoring
Set up alerts for:
- High 404 rates (broken links)
- Latency spikes (database issues)
- Redirect errors (expired links)
Performance Benchmarks
Typical redirect latency on AWS ECS Fargate (db.t3.micro RDS):| Metric | Value |
|---|---|
| Median (p50) | 35-45 ms |
| 95th percentile (p95) | 60-80 ms |
| 99th percentile (p99) | 100-150 ms |
| Database query time | 10-20 ms |
| Business logic | 5-10 ms |
| Network overhead | 15-25 ms |