cache design
LRU
least recently used
memory management
algorithm

LRU cache design

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction to LRU Cache Design

In the realm of computer science, cache design is crucial for optimizing data retrieval and minimizing access time. One of the widely used cache algorithms is the "Least Recently Used" (LRU) cache. This article delves into the design, technicalities, and applications of LRU caches, offering a comprehensive understanding of its workings.

What is an LRU Cache?

An LRU cache is a data structure that stores a limited amount of data with the goal of keeping the most frequently accessed items available for quick retrieval. The "Least Recently Used" policy determines which item should be removed or replaced when the cache reaches its capacity limits.

Why Use an LRU Cache?

LRU caches are ideal in scenarios where the access pattern is predictably non-uniform, and recent accesses are more likely to be accessed again. For example, operating systems utilize LRU caches for swapping pages in memory management, databases use them for caching queries, and web browsers use LRU logic to store recent pages.

Technical Implementation of an LRU Cache

The primary components of an LRU cache are often a combination of a hash map (or dictionary) and a doubly linked list, each serving a critical function:

  • Hash Map: Provides average O(1)O(1) time complexity for accessing the cache elements.
  • Doubly Linked List: Maintains the order of usage in the cache.

Basic Operations in an LRU Cache

  1. Get Operation:
    • Retrieve an item from the cache.
    • If the item is found, move it to the head of the doubly linked list to signify recent usage.
  2. Put Operation:
    • Add a new item to the cache.
    • If the cache exceeds its size, remove the tail of the list, which represents the least recently used item.

Code Example

Here's a simple Python implementation of an LRU cache using the above data structures:

  • Node Class: Represents an entry in the cache with pointers to previous and next nodes.
  • LRUCache Class: Manages the cache. The doubly linked list is bounded by `head` and `tail` sentinel nodes.
  • Methods `_add` and `_remove` update the order of the list.
  • `get` method retrieves the value, promoting the node to the head.
  • `put` adds a new node and manages cache overflow.

Course illustration
Course illustration

All Rights Reserved.