Caching
What is caching?
- Caching is the process of storing data in a cache.
- A cache is a temporary storage area.
- Caching is used to speed up data access.
- Caching is used to reduce load on underlying resources.
Real world example
- I have used caching in my project to reduce the load on the database.
- website link :FMC Weekend
- I have used caching in the following way:
- I have used caching to store the data of the users who have registered for the event.
- Their Profile data is stored in the cache.
- When the user logs in, the data is fetched from the cache.
- If the data is not present in the cache, then the data is fetched from the database and stored in the
cache.
- This reduces the load on the database.
Types of caching
- There are two types of caching:
- In Memory/Local Cache
- Distributed Cache/Internal Cache
In Memory/Local Cache
- In Memory/Local Cache is a cache that is stored in the memory of the server.
- Example:
Distributed Cache/Internal Cache
- Distributed Cache/Internal Cache is a cache that is stored in a shared network/memory.
- Example:
When to use caching?
- Caching should be used when:
- The data is not updated frequently.
- The data is not sensitive.
- The data is not large.
- static content (images, videos,html,css etc.)
- Example :
- Read Intensive in Twitter
- Read Intensive in Facebook
- Read Intensive in Instagram
Types of caching we are storing in our
project
- Application Server Cache
- CDN
Cache Eviction
- Cache eviction is the process of removing an entry from the cache on a definite time or on a condition.
Cache Eviction Techniques
- How we are deleting the data from the cache?
- There are following types of cache eviction techniques:
- LRU (Least Recently Used) Eviction
- LFU (Least Frequently Used) Eviction
- FIFO (First In First Out) Eviction
- MRU(Most Recently Used) Eviction
- LFU (Least Frequently Used) Eviction
- LIFO (Last In First Out) Eviction
- RR (Random Replacement) Eviction
LRU (Least Recently Used) Eviction
- In this technique, the entry which is least recently used is removed from the cache.
- Example:
- If the cache size is 5 and the cache is full.
- If we want to add a new entry in the cache, then the entry which is least recently used is
removed from the cache.
LFU (Least Frequently Used) Eviction
- In this technique, the entry which is least frequently used is removed from the cache.
- Example:
- If the cache size is 5 and the cache is full.
- If we want to add a new entry in the cache, then the entry which is least frequently used is
removed from the cache.
FIFO (First In First Out) Eviction
- In this technique, the entry which is first inserted in the cache is removed from the cache.
- Example:
- If the cache size is 5 and the cache is full.
- If we want to add a new entry in the cache, then the entry which is first inserted in the cache
is removed from the cache.
MRU(Most Recently Used) Eviction
- In this technique, the entry which is most recently used is removed from the cache.
- Example:
- If the cache size is 5 and the cache is full.
- If we want to add a new entry in the cache, then the entry which is most recently used is
removed from the cache.
LFU (Least Frequently Used) Eviction
- In this technique, the entry which is least frequently used is removed from the cache.
- Example:
- If the cache size is 5 and the cache is full.
- If we want to add a new entry in the cache, then the entry which is least frequently used is
removed from the cache.
LIFO (Last In First Out) Eviction
- In this technique, the entry which is last inserted in the cache is removed from the cache.
- Example:
- If the cache size is 5 and the cache is full.
- If we want to add a new entry in the cache, then the entry which is last inserted in the cache
is removed from the cache.
RR (Random Replacement) Eviction
- In this technique, the entry which is randomly selected is removed from the cache.
- Example:
- If the cache size is 5 and the cache is full.
- If we want to add a new entry in the cache, then the entry which is randomly selected is removed
from the cache.
Cache Invalidation
- Cache Invalidation is the process of removing an entry from the cache on a definite time or on a
condition.
Cache Invalidation Techniques
- How we are deleting the data from the cache?
- There are following types of cache invalidation techniques:
- Time To Live (TTL)
- Write Through
- Write Back
Time To Live (TTL)
- In this technique, the entry is removed from the cache after a definite time.
- Example:
- If the cache size is 5 and the cache is full.
- If we want to add a new entry in the cache, then the entry which is least recently used is
removed from the cache.
Write Through
- In this technique, the entry is removed from the cache after a definite time.
- Example:
- If the cache size is 5 and the cache is full.
- If we want to add a new entry in the cache, then the entry which is least recently used is
removed from the cache.
Write Back
- In this technique, the entry is removed from the cache after a definite time.
- Example:
- If the cache size is 5 and the cache is full.
- If we want to add a new entry in the cache, then the entry which is least recently used is
removed from the cache.
Cache Consistency
- Cache Consistency is the process of maintaining the consistency of the data in the cache.
Cache Consistency Techniques
- How we are maintaining the consistency of the data in the cache?
- There are following types of cache consistency techniques:
Write Through
- In this technique, the entry is removed from the cache after a definite time.
- Example:
- If the cache size is 5 and the cache is full.
- If we want to add a new entry in the cache, then the entry which is least recently used is
removed from the cache.
Write Back
- In this technique, the entry is removed from the cache after a definite time.
- Example:
- If the cache size is 5 and the cache is full.
- If we want to add a new entry in the cache, then the entry which is least recently used is
removed from the cache.
File Based Storage System
What is File Based Storage System?
- File Based Storage System is a system in which the data is stored in the form of files.
- Example:
When to use File Based Storage System?
- File Based Storage System should be used when:
- The data is not updated frequently.
- The data is not sensitive.
- The data is not large.
- Example :
- Read Intensive in Twitter
- Read Intensive in Facebook
- Read Intensive in Instagram
Types of File Based Storage System
- There are two types of File Based Storage System:
- Local File System
- Distributed File System
Local File System
- Local File System is a file system that is stored in the memory of the server.
- Example:
Distributed File System
- Distributed File System is a file system that is stored in a shared network/memory.
- Example:
Challenges in File Based Storage System
- Data Redundancy
- Data Consistency
- Data Security
- Slow Data Access
Database
RDBMS
- RDBMS stands for Relational Database Management System.
- RDBMS is a database management system that is based on the relational model.
- Stored data in the form of tables.
- Connected data with the help of foreign keys.
- Example:
- MySQL
- PostgreSQL
- Oracle
- Microsoft SQL Server
Advantages of RDBMS
- Data Redundancy is reduced.
- Data Consistency is maintained.
- Data Security is maintained.
- Data Access is fast.
- Data Integrity is maintained.
Disadvantages of RDBMS
- Rigid Schema
- Scaling is difficult(Horizontal Scaling)
- High Cost
- Not suitable for unstructured data
- Vertical Scaling is possible but it is expensive and limited.
NoSQL
- NoSQL stands for Not Only SQL.
- NoSQL is a database management system that is not based on the relational model.
Types of NoSQL
- There are four types of NoSQL:
- Key Value Store
- Generrally used for caching.
- Example:
- Document Store
- Brimgs both RDMS and NoSQL together.
- Used for storing unstructured data.
- Example:
- Column Store
- Used for storing large amount of data.
- For Data Management during machine learning.
- Column Store is a type of NoSQL database that stores data in columns rather than rows.
- Example:
- Graph Database
- Used for storing data in the form of nodes and edges.
- Used for storing data in the form of graphs.
- Google Maps
- Social Media LinkedIn connection graph
- Example:
Examples
- Google Maps - Graph DB
- LinkedIn - Graph DB
- Amazon Cart- Key Valued DB :Redis
- Payment :We won't use NoSQL , We use SQL DB for consistency
Polyglot Persistance
- Multiple Databases are used as per requirement in the system
graph TD;
X("<img src='https://raw.githubusercontent.com/shubham21155102/Archieve/main/System_Design/psql.png'; width='50' />"<br><br>Postgre SQL)
Y("<img src='https://raw.githubusercontent.com/shubham21155102/Archieve/main/System_Design/cassandral.png'; width='75' />"<br><br>Cassendra)
Z("<img src='https://raw.githubusercontent.com/shubham21155102/Archieve/main/System_Design/mong.webp'; width='70' />"<br><br>Mongo DB)
W("<img src='https://raw.githubusercontent.com/shubham21155102/Archieve/main/System_Design/redis.png'; width='70' />" <br><br>Redis DB)
graph TD;
A[Ecommerce Platform]-->B[Shopping Cart<br> & <br> Session Data]-->C[Key Valued DB]-->W("<img src='https://raw.githubusercontent.com/shubham21155102/Archieve/main/System_Design/redis.png'; width='70' />"<br><br>Redis DB)
A-->D[Completed Order]-->E[Document DB]-->Z("<img src='https://raw.githubusercontent.com/shubham21155102/Archieve/main/System_Design/mong.webp'; width='70' />"<br><br>Mongo DB)
A-->F[Inventory <br>&<br>Item Price]-->G[RDBMS<br>Legacy DB]-->X("<img src='https://raw.githubusercontent.com/shubham21155102/Archieve/main/System_Design/psql.png'; width='50' />"<br><br>PostgreSQL)
A-->H[Customer <br>Social Graph]-->I[Graph DB]-->CC("<img src='https://raw.githubusercontent.com/shubham21155102/Archieve/main/System_Design/neo4j.svg'; width='80' />"<br><br>Neo4j Graph DB)