Good question, Here is the detailed answer of your question on How to ensure Redis cache has most updated data 1. Cache Invalidation on Update Whenever data is updated in the database, immediately invalidate or update the corresponding cache entry. Implementation Steps: Update the Database: Perform the update operation on the database. Invalidate Cache: Remove or update the relevant key in Redis. Example in Node.js: const redisClient = require('./redisClient'); // assuming you've set up your Redis client const db = require('./databaseClient'); // your database client async function updateData(id, newData) { // Update the database await db.update(id, newData); // Invalidate the cache await redisClient.del(`data:${id}`); } async function getData(id) { // Check the cache first let cachedData = await redisClient.get(`data:${id}`); if (cachedData) { return JSON.parse(cachedData); } // If not in cache, fetch from the database let data = await db.get(id); // Store in cache for future requests await redisClient.set(`data:${id}`, JSON.stringify(data)); return data; } 2. Write-Through Cache When updating data, write the new data to both the database and the cache simultaneously. Example in Node.js: async function updateData(id, newData) { // Update the database await db.update(id, newData); // Update the cache await redisClient.set(`data:${id}`, JSON.stringify(newData)); } 3. Cache with Expiry (TTL) Set a time-to-live (TTL) for cache entries. This ensures that even if cache invalidation is missed, the stale data will eventually expire and be refreshed from the database. Example in Node.js: async function getData(id) { let cachedData = await redisClient.get(`data:${id}`); if (cachedData) { return JSON.parse(cachedData); } let data = await db.get(id); // Store data with an expiry time (e.g., 60 seconds) await redisClient.setex(`data:${id}`, 60, JSON.stringify(data)); return data; } 4. Event-Driven Cache Invalidation Use events to trigger cache invalidation. Whenever the database updates, it emits an event that listeners (like the cache layer) can act upon. Example with Event Emitters: const EventEmitter = require('events'); const cacheInvalidationEmitter = new EventEmitter(); cacheInvalidationEmitter.on('dataUpdated', async (id) => { await redisClient.del(`data:${id}`); }); async function updateData(id, newData) { await db.update(id, newData); cacheInvalidationEmitter.emit('dataUpdated', id); } By using one or a combination of these strategies, you can ensure that your Redis cache reflects the latest data from your database. Hope this answer will resolve your query, if not? feel free to ask here again and I will make a proper practical video on your boubt and will ensure you will get a proper answer
Good question, Here is the detailed answer of your question on How to ensure Redis cache has most updated data 1. Cache Invalidation on Update Whenever data is updated in the database, immediately invalidate or update the corresponding cache entry. Implementation Steps: Update the Database: Perform the update operation on the database. Invalidate Cache: Remove or update the relevant key in Redis. Example in Node.js: const redisClient = require('./redisClient'); // assuming you've set up your Redis client const db = require('./databaseClient'); // your database client async function updateData(id, newData) { // Update the database await db.update(id, newData); // Invalidate the cache await redisClient.del(`data:${id}`); } async function getData(id) { // Check the cache first let cachedData = await redisClient.get(`data:${id}`); if (cachedData) { return JSON.parse(cachedData); } // If not in cache, fetch from the database let data = await db.get(id); // Store in cache for future requests await redisClient.set(`data:${id}`, JSON.stringify(data)); return data; } 2. Write-Through Cache When updating data, write the new data to both the database and the cache simultaneously. Example in Node.js: async function updateData(id, newData) { // Update the database await db.update(id, newData); // Update the cache await redisClient.set(`data:${id}`, JSON.stringify(newData)); } 3. Cache with Expiry (TTL) Set a time-to-live (TTL) for cache entries. This ensures that even if cache invalidation is missed, the stale data will eventually expire and be refreshed from the database. Example in Node.js: async function getData(id) { let cachedData = await redisClient.get(`data:${id}`); if (cachedData) { return JSON.parse(cachedData); } let data = await db.get(id); // Store data with an expiry time (e.g., 60 seconds) await redisClient.setex(`data:${id}`, 60, JSON.stringify(data)); return data; } 4. Event-Driven Cache Invalidation Use events to trigger cache invalidation. Whenever the database updates, it emits an event that listeners (like the cache layer) can act upon. Example with Event Emitters: const EventEmitter = require('events'); const cacheInvalidationEmitter = new EventEmitter(); cacheInvalidationEmitter.on('dataUpdated', async (id) => { await redisClient.del(`data:${id}`); }); async function updateData(id, newData) { await db.update(id, newData); cacheInvalidationEmitter.emit('dataUpdated', id); } By using one or a combination of these strategies, you can ensure that your Redis cache reflects the latest data from your database. Hope this answer will resolve your query, if not? feel free to ask here again and I will make a proper practical video on your boubt and will ensure you will get a proper answer😊
there is TTL in Redis which will invalidate the data after fixed time which we set, But there are some other really great approaches in Redis like: 1. Event Driven approach 2. Polling 3. Invalidation on updation of data 4. Hybrid approaches
I'm really glad that you have liked this video. One of the best comment on my channel ever😅, Also comment down on what topics you want the next videos on☺️
Hello Sir, Could you please upload a video tutorial where you create an eCommerce REST API similar to Walmart using Node.js, Redis, and Express.js? The tutorial should cover authentication and all other key points typically followed by the industry.
In production, There are various ways, like you can use Redis cloud, where Redis is hosted on the cloud or you can host Redis on your server as well, for example on AWS EC2 using the same docker image, and also AWS, Azure, etc. (famous cloud providers) provides Redis cloud in their services which are usually used in production. Hope this answer has resolved your query if not? feel free to ask in the comments section
Really amazing video bro , but I have a question, how can I ensure that when data is updated in the database, subsequent requests to the same route do not return stale data from Redis cache but instead fetch the updated data from the database?
Good question, Here is the detailed answer on how you can ensure Redis always have updated data from database: 👇👇 There are various techniques like: 1. Cache Invalidation on Update Whenever data is updated in the database, immediately invalidate or update the corresponding cache entry. Implementation Steps: Update the Database: Perform the update operation on the database. Invalidate Cache: Remove or update the relevant key in Redis. Example in Node.js: javascript const redisClient = require('./redisClient'); // assuming you've set up your Redis client const db = require('./databaseClient'); // your database client async function updateData(id, newData) { // Update the database await db.update(id, newData); // Invalidate the cache await redisClient.del(`data:${id}`); } async function getData(id) { // Check the cache first let cachedData = await redisClient.get(`data:${id}`); if (cachedData) { return JSON.parse(cachedData); } // If not in cache, fetch from the database let data = await db.get(id); // Store in cache for future requests await redisClient.set(`data:${id}`, JSON.stringify(data)); return data; } 2. Write-Through Cache When updating data, write the new data to both the database and the cache simultaneously. Example in Node.js: async function updateData(id, newData) { // Update the database await db.update(id, newData); // Update the cache await redisClient.set(`data:${id}`, JSON.stringify(newData)); } 3. Cache with Expiry (TTL) Set a time-to-live (TTL) for cache entries. This ensures that even if cache invalidation is missed, the stale data will eventually expire and be refreshed from the database. Example in Node.js: async function getData(id) { let cachedData = await redisClient.get(`data:${id}`); if (cachedData) { return JSON.parse(cachedData); } let data = await db.get(id); // Store data with an expiry time (e.g., 60 seconds) await redisClient.setex(`data:${id}`, 60, JSON.stringify(data)); return data; } 4. Event-Driven Cache Invalidation Use events to trigger cache invalidation. Whenever the database updates, it emits an event that listeners (like the cache layer) can act upon. Example with Event Emitters: const EventEmitter = require('events'); const cacheInvalidationEmitter = new EventEmitter(); cacheInvalidationEmitter.on('dataUpdated', async (id) => { await redisClient.del(`data:${id}`); }); async function updateData(id, newData) { await db.update(id, newData); cacheInvalidationEmitter.emit('dataUpdated', id); } By using one or a combination of these strategies, you can ensure that your Redis cache reflects the latest data from your database. I hope this answer will resolve your query, if not? feel free to ask in comments section and I will make dedicated video on this topic and practically explain you how you can ensure Redis cache have most updated data
Good question, Here is the detailed answer of your question on How to ensure Redis cache has most updated data 1. Cache Invalidation on Update Whenever data is updated in the database, immediately invalidate or update the corresponding cache entry. Implementation Steps: Update the Database: Perform the update operation on the database. Invalidate Cache: Remove or update the relevant key in Redis. Example in Node.js: const redisClient = require('./redisClient'); // assuming you've set up your Redis client const db = require('./databaseClient'); // your database client async function updateData(id, newData) { // Update the database await db.update(id, newData); // Invalidate the cache await redisClient.del(`data:${id}`); } async function getData(id) { // Check the cache first let cachedData = await redisClient.get(`data:${id}`); if (cachedData) { return JSON.parse(cachedData); } // If not in cache, fetch from the database let data = await db.get(id); // Store in cache for future requests await redisClient.set(`data:${id}`, JSON.stringify(data)); return data; } 2. Write-Through Cache When updating data, write the new data to both the database and the cache simultaneously. Example in Node.js: async function updateData(id, newData) { // Update the database await db.update(id, newData); // Update the cache await redisClient.set(`data:${id}`, JSON.stringify(newData)); } 3. Cache with Expiry (TTL) Set a time-to-live (TTL) for cache entries. This ensures that even if cache invalidation is missed, the stale data will eventually expire and be refreshed from the database. Example in Node.js: async function getData(id) { let cachedData = await redisClient.get(`data:${id}`); if (cachedData) { return JSON.parse(cachedData); } let data = await db.get(id); // Store data with an expiry time (e.g., 60 seconds) await redisClient.setex(`data:${id}`, 60, JSON.stringify(data)); return data; } 4. Event-Driven Cache Invalidation Use events to trigger cache invalidation. Whenever the database updates, it emits an event that listeners (like the cache layer) can act upon. Example with Event Emitters: const EventEmitter = require('events'); const cacheInvalidationEmitter = new EventEmitter(); cacheInvalidationEmitter.on('dataUpdated', async (id) => { await redisClient.del(`data:${id}`); }); async function updateData(id, newData) { await db.update(id, newData); cacheInvalidationEmitter.emit('dataUpdated', id); } By using one or a combination of these strategies, you can ensure that your Redis cache reflects the latest data from your database. Hope this answer will resolve your query, if not? feel free to ask here again and I will make a proper practical video on your boubt and will ensure you will get a proper answer😊
If your data changes continuously, you need a robust caching strategy to ensure data consistency while minimizing the performance impact. Here are some approaches: 1. Event-Driven Updates Emit events on data changes and have the cache update itself based on those events. Example with Event Emitters: const EventEmitter = require('events'); const cacheUpdateEmitter = new EventEmitter(); cacheUpdateEmitter.on('dataUpdated', async (id, newData) => { await redisClient.set(`data:${id}`, JSON.stringify(newData)); }); async function updateData(id, newData) { await db.update(id, newData); cacheUpdateEmitter.emit('dataUpdated', id, newData); } 2. Polling Periodically poll the database for changes and update the cache accordingly. Example: async function pollAndUpdateCache(id) { setInterval(async () => { let data = await db.get(id); await redisClient.set(`data:${id}`, JSON.stringify(data)); }, 5000); // Poll every 5 seconds } 5. Hybrid Approach Combine several strategies for optimal performance and consistency. By implementing one or a combination of these strategies, you can handle continuously changing data effectively while maintaining cache consistency.
Sorry bro for late replying, thanks for waiting, here I have given the answer, but if you still have more questions, want more clarity upon answers, feel free to comment here, I will make a dedicated practical video for resolving your queries😊
Please tell me how can I write like this and you know every linkedin post they are posting like this and I am unable to write like this and I search a lot and last I am unable to find how can I write content on post plz make a detailed video and I will share lot of my friends and please explain in detail, please make video ASAP
Hey bro, first of all, thanks for commenting, but sorry I didn't get your query, Can you explain to me a bit more and with clarity what you are exactly asking?
When using Redis, it is crucial to follow best practices to ensure there are no memory leaks: 1. Use the LRU eviction policy to automatically remove the least recently used entries when memory limits are reached. 2. Set TTL (Time-to-Live) for each cache entry to automatically remove stale data. 3. Set memory limits to prevent Redis from consuming excessive memory. 4. Implement cache segmentation to manage memory usage more effectively by using distinct keys for different APIs. 5. And make sure to use effective data structure according to your system/data need, because Redis supports a variety of data structures to manage data effectively.
This video got me to subscribed, amazing content and I have a question how much data I can store on redis? and also Instagram where storing cashed data
Hey vammotv, first of all thanks for subscribing and joining our valuable community. Here is the short answer of your questions: How Much Data Can You Store in Redis? The amount of data you can store in Redis is limited by the available RAM on your server. For larger datasets, Redis Cluster allows sharding across multiple nodes to increase capacity. Also Redis provides various data structure which can be used for efficient data storing Instagram's Caching Strategy Although Instagram's real architecture isn't public fully, But through my research I got top know Instagram uses in-memory caches like Redis and Memcached for frequently accessed data, and CDNs for static assets. They also optimize databases and use a microservices architecture to handle large-scale data efficiently. Hope these answers were able to resolve your queries, if not? feel free to ask here again and I will try my best to make video on your questions and answer them propely and in-depth😊
@@sandeepdev0 Please tell me how can I write like this and you know every linkedin post they are posting like this and I am unable to write like this and I search a lot and last I am unable to find how can I write content on post plz make a detailed video and I will share lot of my friends and please explain in detail, please make video ASAP
yes you are right, but I also said that this is just an example and its not the actual and real architecture of Instagram. That was just a simple example to make everyone understand the problem solved by Redis.
Agar data me koi change hua hoga to.. then still redis se old data ayega fir tab kya karoge
Good question,
Here is the detailed answer of your question on How to ensure Redis cache has most updated data
1. Cache Invalidation on Update
Whenever data is updated in the database, immediately invalidate or update the corresponding cache entry.
Implementation Steps:
Update the Database: Perform the update operation on the database.
Invalidate Cache: Remove or update the relevant key in Redis.
Example in Node.js:
const redisClient = require('./redisClient'); // assuming you've set up your Redis client
const db = require('./databaseClient'); // your database client
async function updateData(id, newData) {
// Update the database
await db.update(id, newData);
// Invalidate the cache
await redisClient.del(`data:${id}`);
}
async function getData(id) {
// Check the cache first
let cachedData = await redisClient.get(`data:${id}`);
if (cachedData) {
return JSON.parse(cachedData);
}
// If not in cache, fetch from the database
let data = await db.get(id);
// Store in cache for future requests
await redisClient.set(`data:${id}`, JSON.stringify(data));
return data;
}
2. Write-Through Cache
When updating data, write the new data to both the database and the cache simultaneously.
Example in Node.js:
async function updateData(id, newData) {
// Update the database
await db.update(id, newData);
// Update the cache
await redisClient.set(`data:${id}`, JSON.stringify(newData));
}
3. Cache with Expiry (TTL)
Set a time-to-live (TTL) for cache entries. This ensures that even if cache invalidation is missed, the stale data will eventually expire and be refreshed from the database.
Example in Node.js:
async function getData(id) {
let cachedData = await redisClient.get(`data:${id}`);
if (cachedData) {
return JSON.parse(cachedData);
}
let data = await db.get(id);
// Store data with an expiry time (e.g., 60 seconds)
await redisClient.setex(`data:${id}`, 60, JSON.stringify(data));
return data;
}
4. Event-Driven Cache Invalidation
Use events to trigger cache invalidation. Whenever the database updates, it emits an event that listeners (like the cache layer) can act upon.
Example with Event Emitters:
const EventEmitter = require('events');
const cacheInvalidationEmitter = new EventEmitter();
cacheInvalidationEmitter.on('dataUpdated', async (id) => {
await redisClient.del(`data:${id}`);
});
async function updateData(id, newData) {
await db.update(id, newData);
cacheInvalidationEmitter.emit('dataUpdated', id);
}
By using one or a combination of these strategies, you can ensure that your Redis cache reflects the latest data from your database.
Hope this answer will resolve your query, if not? feel free to ask here again and I will make a proper practical video on your boubt and will ensure you will get a proper answer
@@sandeepdev0 👍🏻
best video I ever saw
Thanks for this knowledge
Bhai apka padhane ka style mast aur simple hai
redis client creation time time pr configure nhi kiya ?
Impressive content and teaching style.
Can you make some videos related to Kafka with real world example.
Thank you and your suggestion Noted✅
Really amazing video on docker and redis please if possible make more on docker and redis please bhaiya . Your explanation is very good 🤩
noted✅, content coming soon
which extention did you use for autocomplete code in VS code?
Github co-pilot, but nowadays I am using cursor
Thanks much needed videos like these
More to come! 😊
Kon kon drop servicing wali didi ka ad dekha hai is video ko dekhne se pehle like here 😂😂
one qustion well i can literally do that redies query but how will system know that user has updated query
With the help of cdc
Good question,
Here is the detailed answer of your question on How to ensure Redis cache has most updated data
1. Cache Invalidation on Update
Whenever data is updated in the database, immediately invalidate or update the corresponding cache entry.
Implementation Steps:
Update the Database: Perform the update operation on the database.
Invalidate Cache: Remove or update the relevant key in Redis.
Example in Node.js:
const redisClient = require('./redisClient'); // assuming you've set up your Redis client
const db = require('./databaseClient'); // your database client
async function updateData(id, newData) {
// Update the database
await db.update(id, newData);
// Invalidate the cache
await redisClient.del(`data:${id}`);
}
async function getData(id) {
// Check the cache first
let cachedData = await redisClient.get(`data:${id}`);
if (cachedData) {
return JSON.parse(cachedData);
}
// If not in cache, fetch from the database
let data = await db.get(id);
// Store in cache for future requests
await redisClient.set(`data:${id}`, JSON.stringify(data));
return data;
}
2. Write-Through Cache
When updating data, write the new data to both the database and the cache simultaneously.
Example in Node.js:
async function updateData(id, newData) {
// Update the database
await db.update(id, newData);
// Update the cache
await redisClient.set(`data:${id}`, JSON.stringify(newData));
}
3. Cache with Expiry (TTL)
Set a time-to-live (TTL) for cache entries. This ensures that even if cache invalidation is missed, the stale data will eventually expire and be refreshed from the database.
Example in Node.js:
async function getData(id) {
let cachedData = await redisClient.get(`data:${id}`);
if (cachedData) {
return JSON.parse(cachedData);
}
let data = await db.get(id);
// Store data with an expiry time (e.g., 60 seconds)
await redisClient.setex(`data:${id}`, 60, JSON.stringify(data));
return data;
}
4. Event-Driven Cache Invalidation
Use events to trigger cache invalidation. Whenever the database updates, it emits an event that listeners (like the cache layer) can act upon.
Example with Event Emitters:
const EventEmitter = require('events');
const cacheInvalidationEmitter = new EventEmitter();
cacheInvalidationEmitter.on('dataUpdated', async (id) => {
await redisClient.del(`data:${id}`);
});
async function updateData(id, newData) {
await db.update(id, newData);
cacheInvalidationEmitter.emit('dataUpdated', id);
}
By using one or a combination of these strategies, you can ensure that your Redis cache reflects the latest data from your database.
Hope this answer will resolve your query, if not? feel free to ask here again and I will make a proper practical video on your boubt and will ensure you will get a proper answer😊
@@sandeepdev0 Bhai mera be same he question tha. Ap es pher ek video he bana do Part2.
Shukriya
Need video
@@sandeepdev0 nice 🙂 very soon I will be asking some more but currently little busy at ts learning
Amazing video.
Please make a video on the payment gateway with node js please
Noted✅, video will come soon on this topic
I think client side caching is better using next js or react query
Recently i have found you. your all videos are amazing sir, please bring a microservice video on node js with aws it will be so helpful. thanks.
Noted✅, will bring this content very soon
bro you are god really i love it ...best explanation my friend, made by day
But i guess there has to be some mechanism to invalidate the data after sometime manually
there is TTL in Redis which will invalidate the data after fixed time which we set,
But there are some other really great approaches in Redis like:
1. Event Driven approach
2. Polling
3. Invalidation on updation of data
4. Hybrid approaches
Razorpay ka use krke nodejs me subscription api bano. 🙏
Working on it bro✅, Next video will be on this and coming in just couple of days
Thank you very much😭 bro i really need this 😘
Glad I could help😊 , it made my day
Thanks bro learnt alot
guruji kha the ap apkw charan kha hai mai kitna darta tha ye soch k kya hai ye aj smjh aya
I'm really glad that you have liked this video. One of the best comment on my channel ever😅,
Also comment down on what topics you want the next videos on☺️
Hello Sir,
Could you please upload a video tutorial where you create an eCommerce REST API similar to Walmart using Node.js, Redis, and Express.js? The tutorial should cover authentication and all other key points typically followed by the industry.
Actually good Idea, Will do work on this soon
New sub❤👍
welcome in the community bro💓
In production how its going to work . When i stop docker , redis will not work .
In production, There are various ways, like you can use Redis cloud, where Redis is hosted on the cloud or you can host Redis on your server as well, for example on AWS EC2 using the same docker image, and also AWS, Azure, etc. (famous cloud providers) provides Redis cloud in their services which are usually used in production.
Hope this answer has resolved your query if not? feel free to ask in the comments section
How about upstash . For production
@@sandeepdev0 How about upstash for production
Yes, upstash also can be used in production. Its also great👌
@@sandeepdev0 thanks 👍
How about use redis on cpanel?
nice video , btw which vs code extension u use that autocompletes ur code ??
codium ai
Actually I use github copilot, but if you want same or better for absolutely free you can go for codium ai
great work bro 🤟
Really amazing video bro , but I have a question, how can I ensure that when data is updated in the database, subsequent requests to the same route do not return stale data from Redis cache but instead fetch the updated data from the database?
Good question,
Here is the detailed answer on how you can ensure Redis always have updated data from database:
👇👇
There are various techniques like:
1. Cache Invalidation on Update
Whenever data is updated in the database, immediately invalidate or update the corresponding cache entry.
Implementation Steps:
Update the Database: Perform the update operation on the database.
Invalidate Cache: Remove or update the relevant key in Redis.
Example in Node.js:
javascript
const redisClient = require('./redisClient'); // assuming you've set up your Redis client
const db = require('./databaseClient'); // your database client
async function updateData(id, newData) {
// Update the database
await db.update(id, newData);
// Invalidate the cache
await redisClient.del(`data:${id}`);
}
async function getData(id) {
// Check the cache first
let cachedData = await redisClient.get(`data:${id}`);
if (cachedData) {
return JSON.parse(cachedData);
}
// If not in cache, fetch from the database
let data = await db.get(id);
// Store in cache for future requests
await redisClient.set(`data:${id}`, JSON.stringify(data));
return data;
}
2. Write-Through Cache
When updating data, write the new data to both the database and the cache simultaneously.
Example in Node.js:
async function updateData(id, newData) {
// Update the database
await db.update(id, newData);
// Update the cache
await redisClient.set(`data:${id}`, JSON.stringify(newData));
}
3. Cache with Expiry (TTL)
Set a time-to-live (TTL) for cache entries. This ensures that even if cache invalidation is missed, the stale data will eventually expire and be refreshed from the database.
Example in Node.js:
async function getData(id) {
let cachedData = await redisClient.get(`data:${id}`);
if (cachedData) {
return JSON.parse(cachedData);
}
let data = await db.get(id);
// Store data with an expiry time (e.g., 60 seconds)
await redisClient.setex(`data:${id}`, 60, JSON.stringify(data));
return data;
}
4. Event-Driven Cache Invalidation
Use events to trigger cache invalidation. Whenever the database updates, it emits an event that listeners (like the cache layer) can act upon.
Example with Event Emitters:
const EventEmitter = require('events');
const cacheInvalidationEmitter = new EventEmitter();
cacheInvalidationEmitter.on('dataUpdated', async (id) => {
await redisClient.del(`data:${id}`);
});
async function updateData(id, newData) {
await db.update(id, newData);
cacheInvalidationEmitter.emit('dataUpdated', id);
}
By using one or a combination of these strategies, you can ensure that your Redis cache reflects the latest data from your database.
I hope this answer will resolve your query, if not? feel free to ask in comments section and I will make dedicated video on this topic and practically explain you how you can ensure Redis cache have most updated data
Good question,
Here is the detailed answer of your question on How to ensure Redis cache has most updated data
1. Cache Invalidation on Update
Whenever data is updated in the database, immediately invalidate or update the corresponding cache entry.
Implementation Steps:
Update the Database: Perform the update operation on the database.
Invalidate Cache: Remove or update the relevant key in Redis.
Example in Node.js:
const redisClient = require('./redisClient'); // assuming you've set up your Redis client
const db = require('./databaseClient'); // your database client
async function updateData(id, newData) {
// Update the database
await db.update(id, newData);
// Invalidate the cache
await redisClient.del(`data:${id}`);
}
async function getData(id) {
// Check the cache first
let cachedData = await redisClient.get(`data:${id}`);
if (cachedData) {
return JSON.parse(cachedData);
}
// If not in cache, fetch from the database
let data = await db.get(id);
// Store in cache for future requests
await redisClient.set(`data:${id}`, JSON.stringify(data));
return data;
}
2. Write-Through Cache
When updating data, write the new data to both the database and the cache simultaneously.
Example in Node.js:
async function updateData(id, newData) {
// Update the database
await db.update(id, newData);
// Update the cache
await redisClient.set(`data:${id}`, JSON.stringify(newData));
}
3. Cache with Expiry (TTL)
Set a time-to-live (TTL) for cache entries. This ensures that even if cache invalidation is missed, the stale data will eventually expire and be refreshed from the database.
Example in Node.js:
async function getData(id) {
let cachedData = await redisClient.get(`data:${id}`);
if (cachedData) {
return JSON.parse(cachedData);
}
let data = await db.get(id);
// Store data with an expiry time (e.g., 60 seconds)
await redisClient.setex(`data:${id}`, 60, JSON.stringify(data));
return data;
}
4. Event-Driven Cache Invalidation
Use events to trigger cache invalidation. Whenever the database updates, it emits an event that listeners (like the cache layer) can act upon.
Example with Event Emitters:
const EventEmitter = require('events');
const cacheInvalidationEmitter = new EventEmitter();
cacheInvalidationEmitter.on('dataUpdated', async (id) => {
await redisClient.del(`data:${id}`);
});
async function updateData(id, newData) {
await db.update(id, newData);
cacheInvalidationEmitter.emit('dataUpdated', id);
}
By using one or a combination of these strategies, you can ensure that your Redis cache reflects the latest data from your database.
Hope this answer will resolve your query, if not? feel free to ask here again and I will make a proper practical video on your boubt and will ensure you will get a proper answer😊
@@sandeepdev0 Thank you 😁
how to check if data is changed
There are several techniques to do this, Hard to explain here... I am making a proper video on it
@@sandeepdev0 please make a video its important if i am use radis in my project
I like your video but I'm looking for advanced projects
For example - project-based on node.js typescript mongodb redis aws
Advanced Full stack project tutorials - 1 shot videos coming soon
What if my data comes from database and it changes continuously?
Atleast reply don't just like the comments
If your data changes continuously, you need a robust caching strategy to ensure data consistency while minimizing the performance impact. Here are some approaches:
1. Event-Driven Updates
Emit events on data changes and have the cache update itself based on those events.
Example with Event Emitters:
const EventEmitter = require('events');
const cacheUpdateEmitter = new EventEmitter();
cacheUpdateEmitter.on('dataUpdated', async (id, newData) => {
await redisClient.set(`data:${id}`, JSON.stringify(newData));
});
async function updateData(id, newData) {
await db.update(id, newData);
cacheUpdateEmitter.emit('dataUpdated', id, newData);
}
2. Polling
Periodically poll the database for changes and update the cache accordingly.
Example:
async function pollAndUpdateCache(id) {
setInterval(async () => {
let data = await db.get(id);
await redisClient.set(`data:${id}`, JSON.stringify(data));
}, 5000); // Poll every 5 seconds
}
5. Hybrid Approach
Combine several strategies for optimal performance and consistency.
By implementing one or a combination of these strategies, you can handle continuously changing data effectively while maintaining cache consistency.
In some cases like Chat Apps where messages are sent in realtime , you can utilize Redis Pub Sub for optimize the performance
Sorry bro for late replying, thanks for waiting, here I have given the answer, but if you still have more questions, want more clarity upon answers, feel free to comment here, I will make a dedicated practical video for resolving your queries😊
@@sandeepdev0 thanks, will try to implement once, then I will get back to you
I am using upstash with reddis, my backend in node.js is deployed on vercel, but speed not increased
make sure your logic for data fetching from Redis and data saving in Redis is correct
great video!!
Please tell me how can I write like this and you know every linkedin post they are posting like this and I am unable to write like this and I search a lot and last I am unable to find how can I write content on post plz make a detailed video and I will share lot of my friends and please explain in detail, please make video ASAP
Hey bro, first of all, thanks for commenting, but sorry I didn't get your query, Can you explain to me a bit more and with clarity what you are exactly asking?
What about accounting software there data needs to be real-time
Redis Pub/Sub can be used in these types of softwares
Different APIs k liye different caching Karne se Radis ki wajah se memory leak nahi hogi ?
When using Redis, it is crucial to follow best practices to ensure there are no memory leaks:
1. Use the LRU eviction policy to automatically remove the least recently used entries when memory limits are reached.
2. Set TTL (Time-to-Live) for each cache entry to automatically remove stale data.
3. Set memory limits to prevent Redis from consuming excessive memory.
4. Implement cache segmentation to manage memory usage more effectively by using distinct keys for different APIs.
5. And make sure to use effective data structure according to your system/data need, because Redis supports a variety of data structures to manage data effectively.
isn't similar to memoisation concept ?
Yes Ayush, you are absolutely right ✅
But if we want real-time data then?
REDIS PUB SUB can be used in this case
@@sandeepdev0 can you create video on that
@@sandeepdev0 thanks
And if there is any change on data in that case ?
@@sandeepdev0What about sockets
thank you
Keep it up bro ❤..
super brooo
This video got me to subscribed, amazing content and I have a question how much data I can store on redis? and also Instagram where storing cashed data
Hey vammotv, first of all thanks for subscribing and joining our valuable community. Here is the short answer of your questions:
How Much Data Can You Store in Redis?
The amount of data you can store in Redis is limited by the available RAM on your server. For larger datasets, Redis Cluster allows sharding across multiple nodes to increase capacity.
Also Redis provides various data structure which can be used for efficient data storing
Instagram's Caching Strategy
Although Instagram's real architecture isn't public fully, But through my research I got top know Instagram uses in-memory caches like Redis and Memcached for frequently accessed data, and CDNs for static assets. They also optimize databases and use a microservices architecture to handle large-scale data efficiently.
Hope these answers were able to resolve your queries, if not? feel free to ask here again and I will try my best to make video on your questions and answer them propely and in-depth😊
@@sandeepdev0 Please tell me how can I write like this and you know every linkedin post they are posting like this and I am unable to write like this and I search a lot and last I am unable to find how can I write content on post plz make a detailed video and I will share lot of my friends and please explain in detail, please make video ASAP
Schema to diffrence hogi jab ki microservice pe kam karta he instagram to db bhi alg he
yes you are right, but I also said that this is just an example and its not the actual and real architecture of Instagram. That was just a simple example to make everyone understand the problem solved by Redis.
But thanks for commenting on the video☺️
@sandeepdev0 thank you sir...
Bhai ye thoda music dhang ka use kiya kr, baki sb theek hai
Got it bro, I will be careful next time☺️
Bro, see my comments you can understand what I am telling