Sub topic 11
Handling Deletes, Unfollows, Blocks and Privacy
This chapter assumes you know fan-out on write, bounded per-user timelines, and cursor pagination from earlier chapters. You should also be comfortable reading short Python and SQL snippets.
Earlier chapters treated the feed as an append-only structure. Real users delete posts, unfollow accounts, block people and change privacy settings. Each of these changes makes data already copied into timelines wrong. This chapter shows how to keep the feed correct without rewriting millions of timelines.
Why these cases are hard
A pushed timeline stores only (timestamp, post_id) references. Those references were created when the post was published. If the post is deleted later, or the follower stops being allowed to see it, every copy is stale.
There are two ways to deal with this:
- Eager cleanup: find every timeline that holds the reference and remove it.
- Lazy filtering: leave the timelines alone and check visibility when the feed is read.
Eager cleanup is expensive. A post by an account with 2 million followers is stored in up to 2 million timelines. Deleting it means 2 million writes, and the cache may not even tell you which timelines hold it. Lazy filtering costs a little on every read but scales with the page size, not the follower count.
Most systems combine both. Lazy filtering guarantees correctness. Background cleanup reclaims space.
Deleted posts
Use a tombstone, which is a marker saying a record was deleted, instead of removing the row at once. The post row keeps its ID and gets a deleted_at value.
At read time the feed service loads the post records for the page of IDs and drops any with deleted_at set. The reference in the timeline is harmless until trimmed out by the length limit or removed by a background job.
Note that a page can come back short after filtering. The reader should keep pulling more candidates until the page is full or the timeline ends. Use the cursor from the last item examined, not the last item returned, or the next request will re-scan the same dropped items.
Unfollows
When user A unfollows user B, A's timeline still holds B's posts. Two approaches work:
- Filter on read. Check each item's author against A's current following set. This is simple and always correct.
- Purge on write. Scan A's timeline (at most 800 entries) and remove B's posts. This is cheap because only one timeline is touched.
Unfollows are a good case for purging, since the cost is bounded by the timeline limit. You also need to stop future pushes: remove A from B's follower set so publish no longer writes to A's feed.
FEED_LIMIT = 800
def unfollow(user_id, author_id, followers, feeds, post_author):
"""Remove the edge, then purge the author's posts from one timeline."""
followers[author_id].discard(user_id)
feeds[user_id] = [
(ts, pid) for ts, pid in feeds[user_id]
if post_author[pid] != author_id
]
# Example
followers = {"b": {"a"}}
feeds = {"a": [(3, "p3"), (2, "p2"), (1, "p1")]}
post_author = {"p1": "b", "p2": "c", "p3": "b"}
unfollow("a", "b", followers, feeds, post_author)
print(feeds["a"]) # [(2, 'p2')]For celebrity accounts using the hybrid model, nothing needs purging. Their posts are pulled at read time from the accounts the user currently follows, so removing the follow edge is enough.
Blocks
A block is stricter than an unfollow. When A blocks B:
- A must not see B's posts, in either direction of follow.
- B must not see A's posts.
- Neither should be able to refollow.
Treat block checks as a read-time filter, not only a purge. A block can be created while a post is in flight through the queue, and a purge would miss it. Keep each user's block list in a fast store and check it for every candidate. Apply the same purge as for unfollows to keep timelines tidy.
Privacy changes
Privacy is the case where eager cleanup is not optional in effect. If an account switches from public to private, followers who were never approved must stop seeing its posts at once. Leaking private content is worse than a short read delay.
Store visibility on the post or author record and evaluate it at read time:
| Rule | Where it is enforced | Why |
|---|---|---|
| Post deleted | Read-time filter, background purge | Correctness first, space second |
| Unfollow | Purge one timeline, stop future pushes | Bounded cost |
| Block | Read-time filter plus purge | Races with in-flight pushes |
| Account made private | Read-time filter, then async cleanup | Must hide immediately |
Never rely on cache cleanup alone for privacy. Cleanup jobs can lag or fail, and the read-time check is the safety net.
Failure handling
Background cleanup runs through a queue, so it can be retried. Make each cleanup job idempotent: removing a reference that is already gone must succeed. Because the read filter is the real guarantee, a failed job costs only wasted space.
Exercise
A user with a pushed timeline of 800 entries blocks another user. A post by the blocked user was published a moment earlier and is still in the fan-out queue. Why is purging the timeline alone not enough, and what fixes it?
Show answer
The purge runs before the queued push lands, so the blocked user's post is then written into the timeline after the purge. A read-time check against the viewer's block list drops it. Checking blocks in the fan-out worker before writing also helps, but the read-time filter is the guarantee.
Exercise
A page request asks for 20 items. After the visibility filter removes 6 deleted posts, only 14 remain. What should the service do, and which cursor should it return?
Show answer
It should read more candidates from the timeline until it has 20 visible items or the timeline ends. The cursor should point to the last item examined, so the next request does not re-scan the dropped items.