0% found this document useful (0 votes)
8 views

Caching Strategies

Caching Strategies 1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Caching Strategies

Caching Strategies 1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Caching

Strategies

LAHIRU
LIYANAPATHIRANA
Page 2 of 22

Introduction
Caching is the process of storing frequently accessed
data in a temporary, high-speed storage layer—known
as a cache—to enable faster retrieval and reduce the
need to repeatedly fetch data from the original, slower
data source.

Rather than accessing the underlying storage every time


a request is made, the system retrieves the data directly
from the cache.

This approach improves overall system performance,


enhances the user experience, significantly reduces
latency, and alleviates the load on backend resources.

The data stored in a cache is a subset of the main data


repository.

It is transient, meaning it is not intended for permanent


storage and may be updated or evicted based on
access patterns or storage limits.

LAHIRU
LIYANAPATHIRANA
Page 3 of 22

Key Benefits of Caching


Reduced Latency: Serving data from a cache
instead of a remote database or disk reduces
response times significantly.

Lower Back-End Load: Fewer direct requests to


databases or APIs decrease load and operational
costs.

Improved Scalability: Systems can handle more


concurrent users when caching reduces the load on
backend resources.

Higher Availability: Caching enhances system


availability by reducing dependency on primary data
sources.

Enhanced User Experience: Faster page loads and


interactions boost user satisfaction and
engagement

Cost Efficiency: Reducing computational and


bandwidth requirements can lead to significant cost
savings, especially in cloud environments.

LAHIRU
LIYANAPATHIRANA
Page 4 of 22

Considerations For Caching


Implementing caching requires careful planning.

Key factors to consider include:

Data Volatility and Consistency: Data that is read


often but changes infrequently is ideal for caching.
Highly volatile data can lead to stale cache entries if
not properly managed.

Access Patterns: Analyzing data access frequency,


volume, and patterns helps design a strategy aligned
with real usage, boosting performance and efficiency.

Cache Size and Memory Management: The cache


must be sized correctly to store frequently accessed
data without overusing system resources. Since caches
are smaller than primary storage, eviction policies must
decide which data to remove when full.

Security and Privacy: When dealing with sensitive


data, appropriate security measures must be in place to
protect cached information. This includes encrypting
cached data and enforcing strict access controls.

LAHIRU
LIYANAPATHIRANA
Page 5 of 22

Considerations For Caching


Monitoring and Performance: Monitor cache hit and
eviction rates regularly. High hit rates signal
effectiveness; high eviction rates may indicate an
undersized cache or expiration policies require tuning.

Cache Warming: Depending on application needs, the


cache can be pre-populated (warmed) with data at
startup or allowed to build up gradually as requests are
served.

Integration Requirements: The cache should integrate


smoothly with existing systems and support the
application’s data sources and architecture.

Cost Implications: Consider the trade-offs between


performance gains and added costs like storage,
infrastructure, and complexity.

LAHIRU
LIYANAPATHIRANA
Page 6 of 22

Caching Strategies
Caching strategies determine how data is stored in and
retrieved from a cache.

The following are common caching strategies:

Cache-Aside (Lazy Loading)

Read-Through

Write-Through

Write-Back (Write-Behind)

Write-Around

LAHIRU
LIYANAPATHIRANA
Page 7 of 22

Cache-Aside (Lazy Loading)


In this strategy, the application is responsible for
managing the cache.

When the application needs data, it first queries the


cache. If the data is found (a cache hit), it is returned
directly.

If the data is not in the cache (a cache miss), the


application retrieves it from the primary data source,
stores a copy in the cache for future requests, and then
returns the data.

LAHIRU
LIYANAPATHIRANA
Page 8 of 22

Cache-Aside (Lazy Loading)


Use Cases
Suitable for general-purpose applications.

Ideal for read-heavy workloads with unpredictable


access patterns.

Applicable in scenarios where resilience to cache


failures is important.
Advantages
Flexible and straightforward to implement.

Only requested data is cached, optimizing memory


usage.

Resilient to cache failures—if the cache is


unavailable, the application can still function by
querying the primary database.

LAHIRU
LIYANAPATHIRANA
Page 9 of 22

Cache-Aside (Lazy Loading)


Limitations
Cache misses introduce additional latency, as data
must be fetched from the underlying source.

Risk of data inconsistency if the database is


updated directly without invalidating or updating
the cache.

The application assumes greater responsibility for


managing the cache lifecycle and consistency.

LAHIRU
LIYANAPATHIRANA
Page 10 of 22

Read-Through
In this strategy, the cache acts as an intermediary
between the application and the primary data source.

All read requests go through the cache. If the data is


present (cache hit), it’s returned immediately.

If not (cache miss), the cache fetches it from the data


source, stores it, and then returns it to the application.

This pattern offloads data-fetching logic from the


application and populates the cache transparently.

LAHIRU
LIYANAPATHIRANA
Page 11 of 22

Read-Through
Use Cases
Well-suited for read-heavy workloads where the
application should be decoupled from direct data
access.

Ideal for systems with integrated caching layers,


such as distributed databases.

Common in applications with high read-to-write


ratios, such as content delivery networks (CDNs),
dashboards, and news websites.

Best when the cache can transparently handle data


loading on behalf of the application.
Advantages
Simplifies application logic by centralizing data
retrieval and cache population within the cache.

Decouples caching logic from application code,


reducing complexity while improving efficiency
through intelligent data loading and eviction.

LAHIRU
LIYANAPATHIRANA
Page 12 of 22

Read-Through
Limitations
Cache misses may introduce additional latency, as
the cache must fetch and store the data before
returning it.

Risk of stale data if cache invalidation or write


strategies are not properly implemented.

Requires a caching solution that supports the read-


through pattern, which may not be available in all
cache systems.

LAHIRU
LIYANAPATHIRANA
Page 13 of 22

Write-Through
In this strategy, data is written to both the cache and
the underlying database simultaneously.

This ensures that the cache always holds the most


recent version of the data, maintaining strong
consistency between the cache and the database.

Write-through caching is typically implemented using


caching solutions that support automatic propagation
of write operations.

LAHIRU
LIYANAPATHIRANA
Page 14 of 22

Write-Through
Use Cases
Ideal for applications that require strong
consistency between the cache and the database.

Suitable for read-heavy workloads with infrequent


writes, such as user profile storage or configuration
data.

Common in financial systems or other domains


where data accuracy and consistency are critical.
Advantages
Strong consistency between cache and data store,
as both are updated in real time.

Simplifies cache expiration logic, since newly


written data is always cached immediately.

Improved recovery in case of cache failure—since


the cache is a mirror of the data store.

Efficient reads, as most recently written data are


always available in the cache.
LAHIRU
LIYANAPATHIRANA
Page 15 of 22

Write-Through
Limitations
Higher write latency, as every write operation must
update both the cache and the database. This can
impact performance under heavy write loads.

Increased write traffic to the backing store, which


may strain the database under frequent write
conditions.

Not ideal for write-heavy workloads, where the


benefits of caching may be diminished by constant
cache churn.

Cache may store rarely accessed data, consuming


space without proportional read benefit.

LAHIRU
LIYANAPATHIRANA
Page 16 of 22

Write-Back (Write-Behind)
In this strategy, data is written to the cache first, and
the update to the primary data source is delayed and
performed asynchronously.

This decouples the write operation from the underlying


data source, improving write performance but
introducing a delay in persistence.

LAHIRU
LIYANAPATHIRANA
Page 17 of 22

Write-Back (Write-Behind)
Use Cases
Ideal for write-heavy workloads where throughput
and performance are prioritized over immediate
consistency.

Common in systems such as logging platforms, real-


time analytics, metrics collection, and caching
layers for data ingestion pipelines.

Suitable when eventual consistency is acceptable


and durability can be managed carefully.
Advantages
Low write latency, since writes complete as soon as
data is stored in the cache.

Improves overall throughput, especially in high-write


scenarios.

Optimizes backend writes, allowing for bulk


operations instead of frequent small writes.

LAHIRU
LIYANAPATHIRANA
Page 18 of 22

Write-Back (Write-Behind)
Limitations
Risk of data loss if the cache fails before pending
writes are flushed to persistent storage.

Potential for data inconsistency, as the data store


may lag behind the cache.

Increased complexity, due to the need for a robust


asynchronous write mechanism and error handling.

Challenging failure recovery, as ensuring data


integrity and eventual consistency requires careful
design and monitoring.

LAHIRU
LIYANAPATHIRANA
Page 19 of 22

Write-Around
In the write-around caching strategy, write operations
bypass the cache and are written directly to the primary
data store.

The cache is updated only when the data is read later,


typically as part of a cache-aside or read-through
pattern.

This approach is designed to avoid populating the


cache with data that may not be read again.

LAHIRU
LIYANAPATHIRANA
Page 20 of 22

Write-Around
Use Cases
Suitable for scenarios where data is written once
and read infrequently or not at all.

Common in log ingestion systems, event recording,


or background job tracking where immediate reads
are unlikely.

Helpful when cache space is limited and should be


reserved for frequently accessed data.
Advantages
Avoids unnecessary cache population, ensuring only
frequently accessed data occupies cache memory.

Write operations are fast, as they skip the additional


step of updating the cache.

Reduces cache churn, preventing rarely read data


from evicting hot data.

LAHIRU
LIYANAPATHIRANA
Page 21 of 22

Write-Around
Use Cases
Higher latency on reads immediately after writes, as
the data won’t be in the cache and must be fetched
from the database.

No reduction in database load for write operations,


since all writes go directly to the backing store.

LAHIRU
LIYANAPATHIRANA
Did You Find This
Post Useful?

Stay Tuned for


More Posts
Like This

LAHIRU
LIYANAPATHIRANA

You might also like