Course contentsShow
← System Design
Lesson 1567 of 1,91938. Real-World System: PastebinFree lesson

Lazy vs Eager Deletion Strategies

Comparing deletion on access (lazy) versus scheduled cleanup jobs (eager) for expired pastes and their tradeoffs.

Lazy vs Eager Deletion Strategies

What you'll learn: How Pastebin systems can delete expired pastes either on-demand when accessed (lazy) or through background cleanup jobs (eager), and the tradeoffs of each approach.

The Two Deletion Philosophies

When pastes reach their TTL expiration, the system must decide when to physically remove them from storage. There are two fundamental strategies:

Lazy Deletion (Delete-on-Access): The system checks expiration only when someone tries to read a paste. If expired, it deletes the content at that moment and returns a "not found" error. Think of this like a grocery store only removing expired milk when a customer picks it up from the shelf.

Eager Deletion (Scheduled Cleanup): Background jobs periodically scan for expired pastes and proactively remove them. This is like a night crew walking the aisles and pulling all expired items on a schedule.

Tradeoffs

Lazy deletion is simpler—no extra infrastructure needed—and spreads the deletion load naturally over time. However, expired pastes consume storage until accessed (or never, if unpopular), wasting space. The database still holds dead data, and you can't reclaim storage predictably.

Eager deletion reclaims storage promptly and keeps the database clean. You control when deletion happens (e.g., low-traffic hours). But it requires running scheduled jobs, indexing expiration_time for efficient scans, and can spike database load during cleanup windows. You need monitoring and retry logic if jobs fail.

Hybrid Approach

Many production systems use both: lazy checks on read paths (fast user-facing validation) plus periodic cleanup jobs to catch unaccessed expired pastes. This balances storage efficiency with operational simplicity.

Key Takeaway: Lazy deletion is simple but wastes storage; eager deletion reclaims space predictably but requires background job infrastructure—most systems combine both for optimal resource use and user experience.