Skip to content
IT glossary
AlgorithmsUpdated December 3, 2026

LRU cache

LRU-кэш

A cache that makes room for a new entry by evicting the one that hasn’t been used for the longest time. Reading an entry makes it recent again.

Also known as: LRU

What is an LRU cache?

LRU stands for least recently used. It answers one question: when the cache is full, which entry should go? LRU evicts the entry whose last use was furthest in the past. It works well when recently requested items are likely to be requested again, such as database records that several requests need in quick succession.

How it keeps track of recent use

An exact LRU cache keeps entries in order from most recently used to least recently used. A common implementation pairs a hash map with a doubly linked list. The map associates each key with its list node, and each node holds the key and value. The list puts the newest use at the front and the eviction candidate at the back.

  • On a get hit, move the existing node to the front and return its value. A miss leaves the order unchanged.
  • On a put for an existing key, replace its value and move its node to the front. For a new key, insert a node at the front. If the cache now exceeds capacity, remove the back node and delete its key from the map.

The map finds a node without scanning the list. The node’s two links let you detach and reinsert it in constant time. Together, the structures give get and put average O(1) time and use O(n) space for a cache with room for n entries, excluding the size of the stored data itself.

Example: a read changes what gets evicted

Suppose a cache can hold two entries. Put A in first, then B, so the order is [B, A] from most to least recent. Now read A: the order becomes [A, B]. Inserting C evicts B, leaving [C, A]. A first-in, first-out cache would have evicted A because it was inserted first. Least frequently used (LFU) instead ranks entries by how often they are used, not simply by when they were last used.

When LRU works well

LRU is useful for in-process caches of database lookups or expensive computations when requests revisit the same keys close together. A long scan of distinct keys can have the opposite effect: it can evict frequently reused entries to make room for items that will never be read again. Eviction also doesn’t tell you whether a value is stale. A time-to-live (TTL) or explicit invalidation serves that separate purpose. And a limit on the number of entries won’t bound memory use in bytes if values vary widely in size.

What interviewers look for

In a coding interview, explain why the map stores node references rather than just values and why you remove an evicted entry from both structures. Test capacity one, overwriting an existing key without evicting another, and reading a key right before an insertion. Agree on the miss result and whether zero capacity is allowed. If thread safety comes up, point out that a successful get is not read-only: it mutates the recency list.