Access Control and Private URLs
What you'll learn: How to restrict access to certain short URLs without slowing down your redirect-heavy system.
The Core Challenge
In a URL shortener, most links are public—anyone with the code can visit. But some users want private URLs that only authorized viewers can access (think internal documents, invitation-only events, or password-protected shares). The trick is implementing access control without adding latency to the already time-sensitive redirect path.
Design Approaches
Password-Protected URLs
Store an optional password_hash column in your URL schema. On redirect, if a password exists:
- Check if the user has already authenticated (session cookie or token)
- If not, serve a lightweight password prompt page
- Validate and set a short-lived session token
- Only then redirect to the destination
Keep it light: This check must happen in milliseconds. Use fast hashing (bcrypt/argon2 precomputed) and cache authenticated tokens aggressively.
Authenticated-Only URLs
Mark URLs with an auth_required flag. On access:
- Verify JWT or session token from your existing auth system
- Check ownership or access-control list (ACL) in the database
- Serve 401/403 if unauthorized
The caching dilemma: You can't blindly cache private URLs like public ones. Use user-specific cache keys (short_code:user_id) or skip caching entirely for private URLs, accepting slightly higher database load as the cost of security.
Minimal Overhead Strategy
- Public URLs: Fast path with full caching (no auth checks)
- Private URLs: Detected via a boolean flag; lightweight token validation before redirect
- Use bloom filters or a separate "private codes" cache to quickly identify which URLs need auth checks
Key Takeaway:
Support access control by adding optional password or auth flags to your schema, validating them only when present, and using user-scoped caching to balance security with the speed demands of a redirect service.