TryBugHunt

The Split-Brain Cache Lease

Hardpython

Distributed Lease Invalidation & Write-After-Commit Ordering

0:00
Step 1: Test Traffic & Expose Flaw
"""
User Profile Service
Handles profile updates and transactional database operations.
"""

class UserProfileService:
    def __init__(self, db, cache):
        self.db = db
        self.cache = cache

    def update_email(self, user_id, new_email):
        # AI Attempt: Invalidate cache before committing to DB
        self.cache.invalidate(f"user:{user_id}")
        self.db.begin_transaction()
        self.db.update_user(user_id, {"email": new_email})
        self.db.commit()
        return {"user_id": user_id, "email": new_email, "status": "updated"}

    def get_profile(self, user_id):
        key = f"user:{user_id}"
        cached = self.cache.get(key)
        if cached is not None:
            return {"data": cached, "source": "cache"}

        lease = self.cache.acquire_lease(key)
        db_user = self.db.get_user(user_id)
        stored = self.cache.set_with_lease(key, db_user, lease["token"])
        return {"data": db_user, "source": "db_loader", "stored": stored}
Step 1: Test Traffic & Expose Flaw

Run the concurrent write-and-read simulation. Prove that an in-flight read permanently leaves obsolete profile data in the cache after a database update.

Choose Traffic Scenario to TestClick to simulate
Architectural Guide & Interview Notes
↑ Jump to Interactive Studio

Deep Dive: The Split-Brain Cache Lease

An engineering analysis of why this AI-generated Pull Request passed isolated unit tests, how multi-tenant traffic exposes the architectural flaw, and how interviewers score your response.

The Root Cause Anti-Pattern

The Interviewer's Scoring Rubric

Candidates are assessed on whether they jump to trial-and-error editing or follow a hypothesis-driven approach. A "Strong Hire" candidate restates the data flow between modules, explains why single-client unit tests passed, crafts an adversarial probe showing cross-tenant leakage, and implements a surgical 1-line patch without breaking existing contracts.

Candidate Strategy for the Code Comprehension Round

  1. 1Map Module ContractsTrace how the gateway invokes the underlying service. Note what data is passed by reference vs. by value.
  2. 2Break It With An Adversarial ProbeSimulate concurrent or sequential traffic across multiple identities to force shared state mutation.
  3. 3Deliver a Surgical PatchMake the minimal required diff to ensure proper per-client state isolation without refactoring unnecessary files.