0% found this document useful (0 votes)
109 views21 pages

Caching Strategy in Relational Database and Redis

The document provides an overview of caching strategies in relational databases and Redis. It discusses common caching strategies like cache-aside, read-through, write-through, etc. It then introduces Redis as an open-source in-memory data structure store that can be used as a database, cache, and message broker. The document outlines how to install and configure Redis, and demonstrates how to implement caching in a .NET application using the ServiceStack Redis client. It also compares Redis to Memcached and highlights some key differences between the two caching technologies.

Uploaded by

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

Caching Strategy in Relational Database and Redis

The document provides an overview of caching strategies in relational databases and Redis. It discusses common caching strategies like cache-aside, read-through, write-through, etc. It then introduces Redis as an open-source in-memory data structure store that can be used as a database, cache, and message broker. The document outlines how to install and configure Redis, and demonstrates how to implement caching in a .NET application using the ServiceStack Redis client. It also compares Redis to Memcached and highlights some key differences between the two caching technologies.

Uploaded by

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

Caching Strategy in Relational Database and Redis

Supervised By: Presented By:


Mr. Ram Krishna Dahal Sir Nasip
Shrestha
Roll No: 25/074
Table Of Contents

 Introduction to Caching
 Caching Strategies in Database
 Introduction to Redis
 Installation and Configuration
 Redis Implementation
 Redis vs Memcached
 Conclusion
 References
Introduction to Caching

 Caching is a practice of storing data in and retrieving data from a high-


performance store (usually memory) either explicitly or implicitly.
 Caching is a technique to store frequently used data in memory to prevent future
access to the source of that data.
 Caching is a strategy used to improve the performance of an application by
reducing the response time for a request.
 Caching in databases is primarily concerned with storing data that has been
queried for so future request can be server from cache.
Caching Strategies in Database
 The cache strategy to be implemented depends upon the data and the access
patterns to the data.
 Following are the types of caching strategies commonly used.
1. Cache Aside
2. Read Through
3. Write Through
4. Write Around
5. Write Back
Cache Aside
 Suitable for data heavy applications.
 Application first checks for data in cache and only accesses database only when
data is not retrieved from cache.
Read Through
 Cache sits in line with database.
 Loads data from database when data is not present in cache.
 Similar to cache aside but the logic to store data in cache is performed by cache
server and not application and data model in cache cannot differ from database.
Write Through
 Cache sits in line with database.
 Data is first written to cache and then to database.
 Whenever data is written to database, it adds or updates data in cache.
Write Around
 Data is written directly to the database and only the data that is read makes its
way into the cache.
 Can be combined with read through cache for better performance.
Write Back
 The application writes data to the cache which acknowledges immediately and
after some delay, it writes the data back to the database.
 It is designed to improve or reduce write operations from and to a cache and its
data source.
 Provides better write performance than write through caches as it reduces the
number of write operations to the main memory
Introduction to Redis

 Redis is an open source in-memory data structure store, used as a database,


cache and message broker.
 Supports data structures like strings, hashes, lists, sets and sorted sets.
 Redis works with an in-memory dataset in order to achieve outstanding
performance.
 Data can be persisted by saving it in a disk memory.
 Redis is based on cache aside caching strategy.
Installation and Configuration

 To install Redis, we just need to download redis installer and extract it.
 Once installed, Redis should be available by default under http://localhost:6379/.
 To get Redis up and running in .NET application we need to add
ServiceStack.Redis library from Nuget Package Manager in our .NET project.
 Once installed we can connect to redis server by using ‘RedisManagerPool’ class
provided by ServiceStack.Redis package and also specify the password required
to access redis server as shown below:

var password = HttpUtility.UrlEncode(“somepassword”);


using (var redisManager = new RedisManagerPool(password + “redisUrl”);
using (var redis = redisManager.GetClient()){
….statements;
}
Once we have redis server configured in our system and our .NET application, a
unique key has to be specified that uniquely identifies each data in redis server. Once
we have the key figured out, it can now be used to perform requests to redis server
as shown below:
var cacheKey = string.Format(“request:{0}”, uniquelyIdentifiableKey);
var dataToStore = new ObejctOfModelClass();
try{
var password = HttpUtility.UrlEncode(“somepassword”);
using (var redisManager = new RedisManagerPool(password +
“redisUrl”);
using (var redis = redisManager.GetClient()){
dataToStore = redis.Get<ObjectOfModelClass>(cacheKey);
if(dataToStore != null) return dataToStore;
}
}
catch{
}
 The code above shows how redis cache behaves when cache hit occurs. During
cache hit, required data is present in the redis cache server itself and data is
provided by the cache server.
 But data for a key might not always be present in the redis server. In this case,
we have a cache miss. During cache miss, the data is now retrieved from
database and the redis cache server is updated with the data and the key that
identifies that data. The code on occasions of cache miss is provided below:
var cacheKey = string.Format(“request:{0}”, uniquelyIdentifiableKey);
var dataToStore = new ObejctOfModelClass();
try{
var password = HttpUtility.UrlEncode(“somepassword”);
using (var redisManager = new RedisManagerPool(password +
“redisUrl”);
using (var redis = redisManager.GetClient()){
dataToStore = redis.Get<ObjectOfModelClass>(cacheKey);
if(dataToStore != null){
return dataToStore;
}
else{
dataToStore = dataRepo.GetData();
redis.Set(cacheKey,dataToStore);
return dataToStore;
}
}
}
catch{
}

The ‘Set’ function saves the key value pair of key and data in redis cache server.
Redis Cache Vs Memcached
 Memcached only supports a key value data structure which are mostly strings.
Redis however supports different data structures like string, hash, list, set and
sorted sets.
 Memcached stores cached data in memory only and does not make use of disk
for storage but Redis makes use of disk memory when the storage in primary
memory is full.
 Memcached performs better than Redis when the data to cache is static and
volume is low. But when the data starts to grow redis cache outperforms
Memcached.
 Redis supports in-memory data persistence but Memcached doest not support
any form of data persistence.
Conclusion
Redis cache server improves the performance of a data heavy application where data
are frequently required to be fetched from database. Redis also helps us understand
how caching can help us provide fast responses for data that have been accessed
before.
Maintaining data consistency between data in cache and data in database is a
concern for redis cache. It can be mitigated by combining it with one of three write
cache strategies.
References
 Cache -Aside pattern. (2018, January). Retrieved from Microsoft Azure:
https://docs.microsoft.com/en-us/azure/architecture/patterns/cache-aside
 Caching Strategies. (n.d.). Retrieved from AWS amazon :
https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/Strategies.html?
fbclid=IwAR1Jkps1w2DA3telsZAD4RIK_PWMwBvdrxP-
b2JgnduU1thnwMXbrePhWFw
 Caching Strategies and How to Choose the Right One. (2017, August). Retrieved
from Codeahoy: https://codeahoy.com/2017/08/11/caching-strategies-and-how-
to-choose-the-right-one/
 Introduction to Redis. (n.d.). Retrieved from redis:
https://redis.io/topics/introduction?fbclid=IwAR2imiZ-
ErCj4vWe0SBEiLY9zK2PiI3f1cKRFy95YuuJKUxRgFx1PCml58A
 Redis or Memcached - How to select the Caching Strategy? (2019, March 7).
Retrieved from Eduonix: https://blog.eduonix.com/web-programming-
tutorials/redis-memcached-select-caching-strategy/
 Simple and Fast .Net Web Services Framework. (n.d.). Retrieved from
ServiceStack: https://servicestack.net/redis?fbclid=IwAR2zsus_N8-
2FvqQbVTBNn9AYhb7H4m0mhg1Bk6fBKvpNBpKyNus67vLTjM
Any Queries?
Thank You

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy