You are on page 1of 8

Tutorials DSA Data Science Web Tech Courses Sign In

System Design Tutorial What is System Design System Design Life Cycle High Level Design HLD Low Level Design LLD Design Patterns OOAD UML Diagrams System Design Interview Guide Crack

Analysis of Monolithic and


Distributed Systems - Learn System Cache Eviction Policies | System Design Trending in News View More
Design
Last Updated : 23 Jan, 2024 10 Ways to Use Microsoft OneNote for
Note-Taking
System Design of Backend for Cache eviction refers to the process of removing data from a cache to make room for new
Expense Sharing Apps like Splitwise 10 Best Yellow.ai Alternatives &
or more relevant information. Caches store frequently accessed data for quicker retrieval, Competitors in 2024
Design Principles in System Design improving overall system performance. However, caches have limited capacity, and when 10 Best Online Collaboration Tools 2024
the cache is full, the system must decide which data to remove. The eviction policy 10 Best Autodesk Maya Alternatives for
3D Animation and Modeling in 2024
Important Key Concepts and determines the criteria for selecting the data to be replaced. This post will dive deep into
Terminologies – Learn System 30 OOPs Interview Questions and
Design Cache Eviction and its policies. Answers (2024)

Why Redis is so fast and popular?

Configurations of Database
Replication in System Design

Guide to System Design for Freshers

How Did Hotstar Managed 5.9 Cr


Viewers During World Cup'23?

Complete Reference to Databases in


Designing Systems - Learn System
Design

Horizontal and Vertical Scaling |


Important Topics for the Cache Eviction Policies
System Design Geeksforgeeks.org
What are Cache Eviction Policies? asks for your consent to use
Cache Eviction Policies your personal data to:
CAP Theorem in System Design
1. Least Recently Used(LRU) Personalised advertising and content, advertising and content
perm_identity measurement, audience research and services development
Monolithic vs Microservices 2. Least Frequently Used(LFU)
Architecture 3. First-In-First-Out(FIFO)
devices Store and/or access information on a device
System Development Life Cycle vs. 4. Random Replacement
expand_more Learn more
System Design Life Cycle
Your personal data will be processed and information from your device
Blackboard Architecture
What are Cache Eviction Policies?
(cookies, unique identifiers, and other device data) may be stored by,
accessed by and shared with 544 TCF vendor(s) and 64 ad partner(s), or
Cache eviction policies areused
algorithms
specifically byor
thisstrategies
site or app. that determine which data to remove
What is High Level Design – Learn from a cache when it reaches
Some its capacity
vendors limit.
may process These
your personalpolicies
data on theaim
basisto maximize the cache’s
of legitimate
System Design interest, which you can object to by managing your options below. Look
efficiency by retaining thefor
most
a link relevant
at the bottomand frequently
of this page or in ouraccessed
privacy policyinformation.
where you can Efficient
withdraw consent.
Distributed Data Structures for Real- cache eviction policies are crucial for maintaining optimal performance in systems with
time Event Processing limited cache space, ensuring that valuable data is retained for quick retrieval.
Manage options Consent
What is Low Level Design or LLD -
Learn System Design

Event-Driven Architecture - System


Design

Web Server, Proxies and their role in


Designing Systems

Cache Eviction Policies


Cache eviction policies are algorithms or strategies implemented to decide which data
should be removed from a cache when the cache reaches its storage capacity. These
policies are essential for optimizing the use of limited cache space and maintaining the
most relevant information for faster retrieval. Some of the most important and common
cache eviction strategies are:

1. Least Recently Used(LRU)


In the Least Recently Used (LRU) cache eviction policy, the idea is to remove the least
recently accessed item when the cache reaches its capacity limit. The assumption is that
items that haven’t been accessed for a longer time are less likely to be used in the near
future. LRU maintains a record of the order in which items are accessed, and when the
cache is full, it evicts the item that hasn’t been accessed
Skip for the longest period.
to content
For Example:

Consider a cache with a maximum capacity of 3, initially containing items A, B, and


C in that order.

If a new item, D, is accessed, the cache becomes full, and the LRU policy would evict
the least recently used item, which is A. The cache now holds items B, C, and D.
If item B is accessed next, the order becomes C, D, B.
If another item, E, is accessed, the cache is full again, and the LRU policy would evict C,
resulting in the cache holding items D, B, and E. The order now is B, E, D.

LRU ensures that the most recently accessed items are retained in the cache, optimizing
for scenarios where recent access patterns are indicative of future accesses.
Geeksforgeeks.org asks for your consent to use
Advantages of Least Recently Used(LRU)
your personal data to:

Simple Implementation: Personalised advertising and content, advertising and content


perm_identityLRU is relatively easy to implement and understand, making
measurement, audience research and services development
it a straightforward choice for many caching scenarios.
Efficient Use of Cache: devices
LRU Store and/or access
is effective information onwhere
in scenarios a devicerecent accesses are good

predictors of future accesses. It ensures that frequently accessed items are more likely
to stay in the cache.
Your personal data will be processed and information from your device
Adaptability: LRU is adaptable to identifiers,
(cookies, unique various and types
otherof applications,
device including
data) may be stored by, databases,
accessed by and shared with 544 TCF vendor(s) and 64 ad partner(s), or
web caching, and file systems.
used specifically by this site or app.
Some vendors may process your personal data on the basis of legitimate
Disadvantages of Least interest,
Recently which Used(LRU)
you can object to by managing your options below. Look
for a link at the bottom of this page or in our privacy policy where you can
withdraw consent.
trict Ordering: LRU assumes that the order of access accurately reflects the future
usefulness of an item. In certain cases, this assumption may not hold true, leading to
suboptimal cache decisions.
Cold Start Issues: When a cache is initially populated, LRU might not perform
optimally as it requires sufficient historical data to make informed eviction decisions.
Memory Overhead: Implementing LRU often requires additional memory to store
timestamps or maintain access order, which can impact the overall memory
consumption of the system.

Use Cases of Least Recently Used(LRU)

Web Caching:
In web caching scenarios, LRU is commonly employed to store frequently accessed
web pages, images, or resources. This helps in reducing latency by keeping the most
recently used content readily available, improving overall website performance.
Database Management:
LRU is often used in database systems to cache query results or frequently accessed
data pages. This accelerates query response times by keeping recently used data in
memory, reducing the need to fetch data from slower disk storage.
File Systems:
File systems can benefit from LRU when caching file metadata or directory
information. Frequently accessed files and directories are kept in the cache,
improving file access speed and reducing the load on the underlying storage.

Skip to content
2. Least Frequently Used(LFU)
LFU is a cache eviction policy that removes the least frequently accessed items first. It
operates on the principle that items with the fewest accesses are less likely to be needed
in the future. LFU maintains a count of how often each item is accessed and, when the
cache is full, evicts the item with the lowest access frequency.

For Example:

Consider a cache with items X, Y, and Z. If item Z has been accessed fewer times
than items X and Y, the LFU policy will retain the items X and Y and potentially evict
item Z when the cache reaches its capacity.

In summary, LRU focuses on the recency of accesses, while LFU considers the frequency
of accesses when decidingGeeksforgeeks.org asks
which items to retain for cache.
in the your consent to use
your personal data to:
Advantages of Least Frequently Used(LFU)
Personalised advertising and content, advertising and content
perm_identity measurement, audience research and services development
Adaptability to Varied Access Patterns:
devices Store and/or access information on a device
LFU is effective in scenarios where some items may be accessed infrequently but
are still essential. It adapts well to varying access patterns and does not strictly
favor recently accessed items.data will be processed and information from your device
Your personal
(cookies, unique identifiers, and other device data) may be stored by,
Optimized for Long-Term Trends:
accessed by and shared with 544 TCF vendor(s) and 64 ad partner(s), or
used specifically by this site or app.
LFU can be beneficial when the relevance of an item is better captured by its overall
Some vendors may process your personal data on the basis of legitimate
frequency of access interest,
over timewhich rather
you can object
thantorecent
by managing your options
accesses. It below. Look
is well-suited for
for a link at the bottom of this page or in our privacy policy where you can
scenarios where items withconsent.
withdraw higher historical access frequencies are likely to be more
relevant.
Low Memory Overhead:
LFU may have lower memory overhead compared to some implementations of LRU
since it doesn’t require tracking timestamps. This can be advantageous in memory-
constrained environments.

Disadvantages of Least Frequently Used(LFU)

Sensitivity to Initial Access:


LFU may not perform optimally during the initial stages when access frequencies
are still being established. It relies on historical access patterns, and a new or less
frequently accessed item might not be retained in the cache until its long-term
frequency is established.
Difficulty in Handling Changing Access Patterns:
LFU can struggle in scenarios where access patterns change frequently. Items that
were once heavily accessed but are no longer relevant might continue to be retained
in the cache.
Complexity of Frequency Counters:
Implementing accurate frequency counting for items can add complexity to LFU
implementations. Maintaining and updating frequency counters for every item in the
cache can be resource-intensive.

Use Cases of Least Frequently Used(LFU)

Skip to content
Database Query Caching:
In database management systems, LFU can be applied to cache query results or
frequently accessed data. It ensures that items that are accessed less frequently but
are still important are retained in the cache.
Network Routing Tables:
LFU is useful in caching routing information for networking applications. Items
representing less frequently used routes are kept in the cache, allowing for efficient
routing decisions based on historical usage.
Content Recommendations:
In content recommendation systems, LFU can be employed to cache information
about user preferences or content suggestions. It ensures that even less frequently
accessed recommendations are considered over time.

3. First-In-First-Out(FIFO)
First-In-First-Out (FIFO) is a cache eviction policy that removes the oldest item from the
cache when it becomes full. In this strategy, data is stored in the cache in the order it
arrives, and the item that has been present in the cache for the longest time is the first to
be evicted when the cache reaches its capacity.

Geeksforgeeks.org asks for your consent to use


your personal data to:
Personalised advertising and content, advertising and content
perm_identity measurement, audience research and services development

devices Store and/or access information on a device

For Example:

Your personal data will be processed and information from your device
Imagine a cache with a(cookies,
capacity of identifiers,
unique three items:and other device data) may be stored by,
accessed by and shared with 544 TCF vendor(s) and 64 ad partner(s), or
used specifically by this site or app.
1. A is added to the cache.
Some vendors may process your personal data on the basis of legitimate
2. B is added to the cache.
interest, which you can object to by managing your options below. Look
3. C is added to the cache.
for a link at the bottom of this page or in our privacy policy where you can
withdraw consent.

At this point, the cache is full (capacity = 3)

If a new item, D, needs to be added, the FIFO policy would dictate that the oldest item, A,
should be evicted. The cache would then look like:

D is added to the cache (A is evicted).


The order of items in the cache now is B, C, and D, reflecting the chronological order of
their arrival.
This ensures a fair and straightforward approach based on the sequence of data
access, making it suitable for scenarios where maintaining a temporal order is
important.

Advantages of First-In-First-Out(FIFO)

1. Simple Implementation: FIFO is straightforward to implement, making it an easy


choice for scenarios where simplicity is a priority.
2. Predictable Behavior: The eviction process in FIFO is predictable and follows a strict
order based on the time of entry into the cache. This predictability can be
advantageous in certain applications.
3. Memory Efficiency: FIFO has relatively low memory overhead compared to some other
eviction policies since it doesn’t require additional tracking of access frequencies or
timestamps.

Disadvantages of First-In-First-Out(FIFO)

Skip to content
1. Lack of Adaptability: FIFO may not adapt well to varying access patterns. It strictly
adheres to the order of entry, which might not reflect the actual importance or
relevance of items.
2. Inefficiency in Handling Variable Importance: FIFO might lead to inefficiencies when
newer items are more relevant or frequently accessed than older ones. This can result
in suboptimal cache performance.
3. Cold Start Issues: When a cache is initially populated or after a cache flush, FIFO may
not perform optimally, as it tends to keep items in the cache based solely on their entry
time, without considering their actual usage.

Use Cases of First-In-First-Out(FIFO)

Task Scheduling in Operating Systems: In task scheduling, FIFO can be employed to


determine the order in which processes or tasks are executed. The first task that arrives
in the queue is the first one to be processed.
Message Queues: In message queuing systems, FIFO ensures that messages are
processed in the order they are received. This is crucial for maintaining the sequence of
operations in applications relying on message-based communication.
Cache for Streaming Applications: FIFO can be suitable for certain streaming
applications where maintaining the order of data is essential. For example, in a video
streaming cache, FIFO ensures that frames are presented in the correct sequence.

4. Random Replacement
Random Replacement is a cache eviction policy where, when the cache is full and a new
item needs to be stored, a randomly chosen existing item is evicted to make room. Unlike
some deterministic policies like LRU (Least Recently Used) or FIFO (First-In-First-Out),
Geeksforgeeks.org asks for your consent to use
which have specific criteria for selectingyour
itemspersonal
to be evicted, Random Replacement simply
data to:
selects an item at random.
Personalised advertising and content, advertising and content
perm_identity measurement, audience research and services development

devices Store and/or access information on a device

Your personal data will be processed and information from your device
(cookies, unique identifiers, and other device data) may be stored by,
accessed by and shared with 544 TCF vendor(s) and 64 ad partner(s), or
used specifically by this site or app.
Some vendors may process your personal data on the basis of legitimate
interest, which you can object to by managing your options below. Look
for a link at the bottom of this page or in our privacy policy where you can
withdraw consent.

For Example:

Consider a cache with three slots and the following data:

1. Item A
2. Item B
3. Item C

Now, if the cache is full and a new item, Item D, needs to be stored, Random Replacement
might choose to evict Item B, resulting in:

1. Item A
2. Item D
3. Item C

The selection of Item B for eviction is entirely random in this policy, making it a
straightforward but less predictable strategy compared to others. While simple, Random
Replacement doesn’t consider the frequency or recency of item access and may not
always result in the most optimal cache performance.

Advantages of Random Replacement

Skip to content
Simplicity: Random replacement is a straightforward and easy-to-implement strategy.
It does not require complex tracking or analysis of access patterns.
Avoids Biases: Since random replacement doesn’t rely on historical usage patterns, it
avoids potential biases that may arise in more deterministic policies.
Low Overhead: The algorithm involves minimal computational overhead, making it
efficient in terms of processing requirements.

Disadvantages of Random Replacement

1. Suboptimal Performance: Random replacement may lead to suboptimal cache


performance compared to more sophisticated policies. It doesn’t consider the actual
usage patterns or the likelihood of future accesses.
2. No Adaptability: It lacks adaptability to changing access patterns. Other eviction
policies, like LRU or LFU, consider the historical behavior of items and adapt to
evolving patterns, potentially providing better cache performance over time.
3. Possibility of Poor Hit Rates: The random nature of eviction may result in poor hit
rates, where frequently accessed items are unintentionally evicted, leading to more
cache misses.

Use Cases of Random Replacement

1. Non-Critical Caching Environments:


In scenarios where the impact of cache misses is minimal or where caching is
employed for non-critical purposes, such as temporary storage of non-essential
data, random replacement can be sufficient.

Geeksforgeeks.org asks for your consent to use


2. Simulation and Testing:
your personal
Random replacement is useful in simulation data to: and testing scenarios
environments
where simplicity and ease of implementation take precedence over sophisticated
Personalised advertising and content, advertising and content
perm_identity measurement,
eviction policies. It allows for a quickaudience
and straightforward
research and servicesapproach
developmentwithout the

need for complex tracking mechanisms.


devices Store and/or access information on a device

3. Resource-Constrained Systems:
In resource-constrained environments,
Your personal where and
data will be processed computational resources
information from your device are limited,
the low overhead of(cookies,
random unique identifiers, and other
replacement maydevice data) may be stored by,
be advantageous. The algorithm
accessed by and shared with 544 TCF vendor(s) and 64 ad partner(s), or
requires minimal processing power
used specifically by thiscompared
site or app. to more complex eviction policies.
Some vendors may process your personal data on the basis of legitimate
interest, which you can object to by managing your options below. Look
Conclusion for a link at the bottom of this page or in our privacy policy where you can
withdraw consent.
In conclusion, cache eviction policies play a crucial role in system design, impacting the
efficiency and performance of caching mechanisms. The choice of an eviction policy
depends on the specific characteristics and requirements of the system. While simpler
policies like Random Replacement offer ease of implementation and low overhead, more
sophisticated strategies such as Least Recently Used (LRU) or Least Frequently Used
(LFU) take into account historical access patterns, leading to better adaptation to changing
workloads

Feeling lost in the vast world of System Design? It's time for a transformation! Enroll in
our Mastering System Design From Low-Level to High-Level Solutions - Live Course and
embark on an exhilarating journey to efficiently master system design concepts and
techniques.
What We Offer:

Comprehensive Course Coverage


Expert Guidance for Efficient Learning
Hands-on Experience with Real-world System Design Project
Proven Track Record with 100,000+ Successful Enthusiasts

1 Suggest improvement

Previous Next

Web Server, Proxies and their role in Designing Instagram | System Design
Designing Systems
Skip to content
Share your thoughts in the comments Add Your Comment

Similar Reads
Cache Eviction vs. Expiration in System Is LRU the best Cache Eviction Policy?
Design

How Cache Locks can be used to overcome Cache Invalidation and the Methods to
Cache Stampede Problem? Invalidate Cache

Cache Stampede or Dogpile Problem in Design a system that counts the number of
System Design clicks on YouTube videos | System Design

Design Restaurant Management System | Design a data structure for LRU Cache
System Design

System Design - Design Google Calendar Different types of Low Level Design in
System Design

J janardan3…

Article Tags : System Design


Geeksforgeeks.org asks for your consent to use
your personal data to:
Personalised advertising and content, advertising and content
perm_identity measurement, audience research and services development

devices Store and/or access information on a device

Your personal data will be processed and information from your device
(cookies, unique identifiers, and other device data) may be stored by,
accessed by and shared with 544 TCF vendor(s) and 64 ad partner(s), or
used specifically by this site or app.
Some vendors may process your personal data on the basis of legitimate
interest, which you can object to by managing your options below. Look
for a link at the bottom of this page or in our privacy policy where you can
withdraw consent.
Company Explore Languages DSA Data Science & HTML & CSS
A-143, 9th Floor, Sovereign Corporate About Us Hack-A-Thons Python Data Structures ML HTML
Tower, Sector-136, Noida, Uttar Pradesh -
201305 Legal GfG Weekly Contest Java Algorithms Data Science With CSS
Careers DSA in JAVA/C++ C++ DSA for Beginners Python Web Templates
In Media Master System Design PHP Basic DSA Problems Data Science For CSS Frameworks
Beginner
Contact Us Master CP GoLang DSA Roadmap Bootstrap
Machine Learning
Advertise with us GeeksforGeeks Videos SQL Top 100 DSA Tailwind CSS
Tutorial
GFG Corporate Geeks Community R Language Interview Problems SASS
ML Maths
Solution Android Tutorial DSA Roadmap by LESS
Sandeep Jain Data Visualisation
Placement Training Tutorials Archive Web Design
Tutorial
Program All Cheat Sheets
Pandas Tutorial Django Tutorial
NumPy Tutorial
NLP Tutorial
Deep Learning
Tutorial

Python Tutorial Computer DevOps Competitive System Design JavaScript


Python Programming Science Git Programming High Level Design JavaScript Examples
Examples Operating Systems AWS Top DS or Algo for CP Low Level Design TypeScript
Python Projects Computer Network Docker Top 50 Tree UML Diagrams ReactJS
Python Tkinter Database Kubernetes Top 50 Graph Interview Guide NextJS
Web Scraping Management System Azure Top 50 Array Design Patterns AngularJS
OpenCV Tutorial Software Engineering GCP Top 50 String OOAD NodeJS
Digital Logic Design DevOps Roadmap Top 50 DP Lodash
Skip to content
Python Interview Engineering Maths Top 15 Websites for System Design Web Browser
Question CP Bootcamp
Interview Questions

Preparation School Subjects Management & Free Online Tools More Tutorials GeeksforGeeks
Corner Mathematics Finance Typing Test Software Videos
Company-Wise Physics Management Image Editor Development DSA
Recruitment Process Chemistry HR Management Code Formatters Software Testing Python
Resume Templates Biology Finance Code Converters Product Management Java
Aptitude Preparation Social Science Income Tax Currency Converter SAP C++
Puzzles English Grammar Organisational Random Number SEO - Search Engine Data Science
Company-Wise Behaviour Generator Optimization
World GK CS Subjects
Preparation Marketing Random Password Linux
Generator Excel

@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved

Geeksforgeeks.org asks for your consent to use


your personal data to:
Personalised advertising and content, advertising and content
perm_identity measurement, audience research and services development

devices Store and/or access information on a device

Your personal data will be processed and information from your device
(cookies, unique identifiers, and other device data) may be stored by,
accessed by and shared with 544 TCF vendor(s) and 64 ad partner(s), or
used specifically by this site or app.
Some vendors may process your personal data on the basis of legitimate
interest, which you can object to by managing your options below. Look
for a link at the bottom of this page or in our privacy policy where you can
withdraw consent.

You might also like