0Pricing
System Design Basics for Backend Developers · レッスン

キャッシュの追い出しポリシー

メモリがいっぱいになったときにキャッシュが保持・破棄するデータを決める仕組みを、LRU、LFU、FIFO、TTL とそれぞれのトレードオフを通じて学びます。

「キャッシュの追い出しポリシー」はCoddyKit上の無料System Design Basics for Backend Developersレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSystem Design Basics for Backend Developers学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 System Design Basics for Backend Developersコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Why Eviction Is Necessary

A cache is fast because it lives in limited memory. When it fills up, it must evict something to make room for new data. The eviction policy decides what to drop.

A good policy keeps the data most likely to be reused.

Hit Rate Is the Goal

The metric that matters is the hit rate: the fraction of requests served from cache. A better eviction policy raises the hit rate, which means fewer slow trips to the database or origin.

hits = 850
misses = 150
hit_rate = hits / (hits + misses)
print('hit rate:', hit_rate)

FIFO

FIFO (First In, First Out) evicts the oldest inserted item regardless of usage. It is simple but ignores access patterns, so a frequently used old item can be wrongly evicted.

LRU: Least Recently Used

LRU evicts the item that has not been accessed for the longest time. It assumes recently used data will be used again soon — true for most workloads, which is why LRU is the default in many caches.

Implementing LRU

A classic LRU uses an ordered map. On access, move the key to the most-recent end; when full, evict from the least-recent end.

from collections import OrderedDict
class LRU:
    def __init__(self, cap):
        self.cap = cap
        self.d = OrderedDict()
    def get(self, k):
        if k in self.d:
            self.d.move_to_end(k)
            return self.d[k]
    def put(self, k, v):
        self.d[k] = v
        self.d.move_to_end(k)
        if len(self.d) > self.cap:
            self.d.popitem(last=False)

c = LRU(2)
c.put('a', 1); c.put('b', 2); c.get('a'); c.put('c', 3)
print(list(c.d.keys()))

LFU: Least Frequently Used

LFU evicts the item accessed the fewest times. It favors long-term popular items over recent bursts. The downside: a once-popular item can linger long after it stops being useful.

TTL-Based Expiration

A TTL (time to live) expires items after a fixed duration regardless of memory pressure. It bounds staleness and is often combined with LRU: TTL controls freshness, LRU controls memory.

SET session:42 "..." EX 3600
# expires in 3600 seconds

Random and Allkeys Variants

Some caches offer random eviction (cheap, surprisingly decent) and scope variants: evict only keys with a TTL set, or evict from all keys. Redis exposes policies like allkeys-lru and volatile-ttl.

Thundering Herd on Eviction

When a hot key is evicted or expires, many clients may simultaneously rebuild it — a thundering herd that hammers the origin. Mitigate with request coalescing (single-flight) or slightly randomized TTLs to spread out expirations.

Choosing a Policy

Match the policy to the workload:

  • Recency-driven traffic: LRU
  • Stable popularity: LFU
  • Freshness-critical data: TTL
  • Uniform access: random is fine and cheap

Eviction in a CDN

CDNs apply the same ideas at the edge. Each edge node has finite storage and evicts (often LRU) plus honors Cache-Control: max-age as a TTL. Understanding eviction explains why a cold edge node has a low initial hit rate.

Quick Check

Test your understanding of eviction policies.

Recap

You learned how caches manage limited memory:

  • Eviction policies aim to maximize hit rate
  • FIFO, LRU, LFU, TTL, and random each suit different patterns
  • Thundering herds need coalescing or jittered TTLs
  • CDNs apply the same eviction logic at the edge

よくある質問

「キャッシュの追い出しポリシー」レッスンは無料ですか?

はい。「キャッシュの追い出しポリシー」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、System Design Basics for Backend Developersコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 System Design Basics for Backend Developersコースには全4レッスンが含まれています。

「キャッシュの追い出しポリシー」で何を学びますか?

メモリがいっぱいになったときにキャッシュが保持・破棄するデータを決める仕組みを、LRU、LFU、FIFO、TTL とそれぞれのトレードオフを通じて学びます。 ブラウザで直接実行するハンズオンコードでSystem Design Basics for Backend Developersを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

System Design Basics for Backend Developersを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのSystem Design Basics for Backend Developersは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「キャッシュの追い出しポリシー」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このSystem Design Basics for Backend Developersレッスンでコードを書いて実行できますか?

はい。すべてのSystem Design Basics for Backend Developersレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. キャッシュ無効化パターン
  2. CDN統合とエッジキャッシュ
  3. Redisによる分散キャッシュ
  4. キャッシュの追い出しポリシー
← System Design Basics for Backend Developersに戻る