Retrieval Error Handling and Fallbacks
What You'll Learn
You'll learn how to gracefully handle failures when retrieving paste content, distinguishing between legitimate missing content and transient system errors.
The Core Problem
When users request a paste, two distinct failure scenarios can occur:
1. The paste truly doesn't exist – The ID is invalid, expired, or was never created. This is a legitimate "not found" situation.
2. Storage systems are temporarily unavailable – S3, databases, or network connections experience transient failures. The paste exists, but we can't reach it right now.
Treating both identically creates poor user experience. A missing paste deserves a clean 404 response. A temporary system hiccup should trigger retry logic.
Implementation Strategy
Missing Paste Handling
When metadata lookup returns no record or S3 returns a genuine "NoSuchKey" error, immediately return HTTP 404 with a user-friendly message: "This paste does not exist or has expired."
Transient Error Recovery
For S3 network timeouts, throttling (503 Service Unavailable), or internal errors (500), implement exponential backoff:
- First retry: Wait 100ms
- Second retry: Wait 200ms
- Third retry: Wait 400ms
- Max retries: 3-5 attempts before giving up
This pattern prevents overwhelming already-stressed systems while giving transient issues time to resolve.
Fallback Options
If primary object storage fails repeatedly:
- Check if content is cached in a read replica
- Return metadata with a "content temporarily unavailable" message
- Queue the request for asynchronous retry and notify the user
Distinguishing Error Types
Inspect error codes carefully: distinguish between NoSuchKey (404), SlowDown (retry), InternalError (retry), and AccessDenied (possibly configuration issue—don't retry endlessly).
Key Takeaway: Distinguish between permanent failures (404 immediately) and transient errors (retry with exponential backoff) to provide appropriate user feedback and prevent cascading failures.