Search HoldMyTwist

Browse all posts

Episode 13

Only Two Jars on the Shelf

A repeated request affects the cached copy that gets evicted using least recently used.

Episodes

Computing Basics

    Robot stands beside a large supply cupboard and a small shelf with only two spaces. Duck watches through the monitor.
    Duck requests AMBER, called A on the order card. Neither shelf space holds a working copy yet.
    Robot fetches an amber supply from the cupboard and places a small amber working copy in one shelf space. The original source remains in the cupboard.
    The cache shelf has A and one empty slot. A is its only and most recently used entry.
    Duck requests BLUE, called B on the order card. It is absent from the shelf.
    Robot fetches blue from the cupboard and puts a working copy in the remaining shelf slot. The original blue remains in the cupboard.
    Both shelf slots are full. The least-to-most recently used order is A then B.
    Duck requests AMBER again and points at its existing shelf copy.
    Robot uses the amber shelf copy directly. The cupboard remains closed. This request is a cache hit.
    Because A was used again, the least-to-most recent order changes to B then A. No new supply is fetched.
    Robot compares last-use markers for blue and amber. Blue is now the less recently used entry, even though amber arrived on the shelf first.
    Duck requests CORAL, called C on the order card. Both cache slots are occupied and no coral copy is present.
    Robot fetches a coral working copy from the cupboard and holds it outside the full two-slot shelf.
    Duck examines the last-use order B then A. The amber hit happened after the blue fetch.
    Robot pauses with the coral copy outside the shelf. The last-use evidence remains visible.
    Robot removes only the blue working copy from the shelf. The original blue supply remains in the cupboard.
    Robot opens empty pincers beside exactly two working jars, AMBER left and CORAL right. Duck cheers inside the CRT. The exact recency diagram below shows A then C.
    Duck checks the cupboard view from the monitor and sees that all three original supplies remain there.
    Robot gestures toward a blank easel while Duck thinks inside the CRT. Exactly two working jars, AMBER and CORAL, sit in the lower cubby. The exact request trace below is A,B,A,C with3misses and1hit.
    Robot paints a physical parade sign with a brush while Duck directs from inside the CRT. The two working jars remain AMBER left and CORAL right in the lower cubby.

    Episode transcript

    Only two shelf spaces

    Robot is standing next to this giant supply cabinet and this little shelf with only 2 spots on it. Duck is watching from the monitor.

    CACHE CAPACITY: 2 COPIES. ORIGINALS STAY IN CUPBOARD

    Duck: Can our fav colors be near us?

    A = AMBER. REQUEST A: MISS

    Robot: Amber is not on the shelf.

    The source is still in the cupboard.

    LEAST → MOST RECENT: [A]

    Duck: One cupboard trip.

    Robot: Room for one more.

    Fill the nearby shelf

    B = BLUE. REQUEST B: MISS

    Duck: Blue, please.

    The original blue is still in the cupboard.

    LEAST → MOST RECENT: [A,B]

    Robot: Another trip to the cupboard.

    LAST USE: A REQUEST1, B REQUEST2

    Duck: Blue was used most recently.

    Duck asks for AMBER yet again. He points to the existing shelf copy.

    REQUEST A AGAIN

    Duck: Amber again.

    A hit changes recency

    Robot is directly accessing the shelf amber copy. The cupboard is still closed. This is a cache hit.

    A: HIT. NO CUPBOARD FETCH

    Robot: It is already here.

    Since A is re-used the order of least to most recent becomes B then A. Nothing is fetched again.

    LEAST → MOST RECENT: [B,A]

    Duck: Amber is newest for us in order.

    LAST USE: B REQUEST2, A REQUEST3

    Robot: It's not about the time of arrival

    C = CORAL. REQUEST C: MISS

    Duck: Now coral.

    Make room for coral

    CACHE FULL: [B,A]. HOLD C OUTSIDE

    Robot: I need to make space.

    LEAST → MOST RECENT: [B,A]

    The original blue supply is still in the cupboard.

    EVICT B COPY. ORIGINAL B REMAINS

    Duck: Blue. It's the one that hasn't been used most recently.

    Copies and originals

    So now we have A and C in the cache. A is least recent, C is most recent.

    INSERT C. LEAST → MOST RECENT: [A,C]

    Robot: Amber and coral fit.

    ORIGINALS A,B,C REMAIN AT SOURCE

    Duck: Blue is still in the cupboard.

    Robot sees 3 cupboard retrievals and 1 direct shelf use.

    FIRST FOUR REQUESTS: A,B,A,C → 3 MISSES,1 HIT

    Robot: Three misses. One hit.

    Duck: Keep that little shelf near my next masterpiece

    A least recently used cache

    A cache has some number of copies, which may be reused.

    If you get a hit, you already have the copy you want.

    If you get a miss, you fetch it from the source.

    We'll just use the least recently used strategy for eviction. So, each time something is used, we mark it as more recent (even if we get a hit). If the cache is full, and we need a new copy, we'll get rid of the least recently used copy.

    The original source is not destroyed when we get rid of the cached copy.

    Recency is different from total number of uses.

    And a real cache would have to have some rules on how to handle changing values in the source.

    Request an evicted item again

    Answer

    Then you request B. It misses. That's because you just lost it due to eviction. Now A is least recent. So you swap A for B. This leaves you with C B.

    FIFTH REQUEST B: MISS. EVICT A COPY. LEAST → MOST: [C,B]. STOP AFTER REQUEST5.

    Notice that the original A,B,C values are still available at the source.

    Sources

    Python documentation, OrderedDict examples and recipes: LRU caches (opens in a new tab)