Caching with Redis
Think of a busy café. The menu board on the wall shows the prices, so the cashier does not walk to the office to read the price list for every order. The board is fast to read. But when the owner changes a price in the office, someone must also change the board. If nobody does, customers see the old price. A cache is that menu board, and Redis is a popular tool to build it.
6 min read6 মিনিটে পড়া যায় · 6 sections6টি সেকশন · 7 sources7টি সোর্স · updated Nov 2026আপডেট: Nov 2026
Cache-aside: hit or missCache-aside: hit নাকি miss
Follow one read through Redis, then move the hit rate and watch the average read time.একটি read-এর পথ Redis দিয়ে অনুসরণ করুন, তারপর hit rate বদলে গড় read time দেখুন।
You’ll learnআপনি শিখবেন
- The app asks the cache firstApp আগে cache-কে জিজ্ঞেস করে✓
- A hit skips the databaseHit হলে database লাগে না
- A miss reads the database and saves a copyMiss হলে database পড়ে copy রাখে
- A higher hit rate lowers the average readHit rate বেশি হলে গড় read কমে
- A write must invalidate the old copyWrite-এ পুরোনো copy invalidate করতে হয়
In plain words
A cache is a fast storage layer that keeps a part of your data, so the next request for that data is faster than a trip to the main store Source 1সোর্স 1. The data is usually kept in fast memory (RAM) Source 1সোর্স 1. The main store is often a database, but a cache can also keep the results of API requests Source 1সোর্স 1.
Redis is an in-memory data store. Many teams use it as a cache Source 2সোর্স 2.
Two words describe each lookup:
- Cache hit: the data is in the cache Source 1সোর্স 1. The app returns it from the cache, with no trip to the main store.
- Cache miss: the data is not in the cache Source 1সোর্স 1. The app must get it from the database or the source API.
A common way to use a cache is the cache-aside pattern. The app loads data into the cache on demand, when somebody asks for it Source 6সোর্স 6:
Read: app --> Redis --hit--> return data
|
miss
v
database --> app saves a copy in Redis --> return data
Write: app --> database (save the change)
app --> Redis (delete the old copy)When the app changes data, it writes to the database first. Then it invalidates the cache item, which means it removes or updates the old copy. The next read loads the new data and caches it again Source 6সোর্স 6.
TTL (time to live) is the expiry time of a cached item Source 1সোর্স 1. In Redis, the EXPIRE command sets a timeout on a key. When the timeout ends, Redis deletes the key automatically Source 3সোর্স 3. The TTL command shows how much time a key has left Source 4সোর্স 4. It returns -1 if the key has no expiry, and -2 if the key does not exist Source 4সোর্স 4.
Memory is limited. If you set an eviction policy, Redis removes keys by itself when the cache passes a memory limit. This is called eviction Source 5সোর্স 5. For example, the allkeys-lru policy removes the keys that were used least recently Source 5সোর্স 5. Eviction is usually safe, because cache entries are only copies of data that is stored elsewhere Source 5সোর্স 5.
Open the image in full sizeছবিটি পূর্ণ আকারে খুলুনসহজ English
A cache is a fast storage layer. It keeps a part of your data, so the next request for that data is faster than a trip to the main store Source 1সোর্স 1. Redis is an in-memory data store that many teams use as a cache Source 2সোর্স 2.
A cache hit means that the data is in the cache Source 1সোর্স 1. The app returns it from the cache, with no trip to the main store. A cache miss means that the data is not in the cache Source 1সোর্স 1. Then the app gets it from the database or the source API.
In the cache-aside pattern, the app loads data into the cache on demand. When the app changes data, it writes to the database first. Then it removes or updates the old copy in the cache Source 6সোর্স 6.
TTL (time to live) is the expiry time of a cached item Source 1সোর্স 1. In Redis, EXPIRE sets a timeout on a key. When the timeout ends, Redis deletes the key Source 3সোর্স 3. TTL returns -1 for a key with no expiry and -2 for a key that does not exist Source 4সোর্স 4.
বাংলা ব্যাখ্যা
Cache হলো একটি দ্রুত storage layer। এটি আপনার data-র একটি অংশ রেখে দেয়, যাতে সেই data-র পরের request মূল store-এ যাওয়ার চেয়ে দ্রুত হয় Source 1সোর্স 1। Data সাধারণত দ্রুত memory-তে (RAM) রাখা হয় Source 1সোর্স 1। মূল store প্রায়ই একটি database, তবে cache API request-এর ফলাফলও রাখতে পারে Source 1সোর্স 1। Redis হলো একটি in-memory data store, আর অনেক team এটিকে cache হিসেবে ব্যবহার করে Source 2সোর্স 2।
Data cache-এ থাকলে তাকে cache hit বলে Source 1সোর্স 1। App মূল store-এ না গিয়ে cache থেকেই data ফেরত দেয়। Data cache-এ না থাকলে তাকে cache miss বলে Source 1সোর্স 1। তখন app-কে database বা source API থেকে data আনতে হয়।
Cache-aside pattern-এ কেউ data চাইলে তখনই app সেটি cache-এ load করে। App data বদলালে আগে database-এ লেখে। তারপর cache item-টি invalidate করে, অর্থাৎ পুরোনো copy মুছে ফেলে বা update করে। পরের read নতুন data load করে আবার cache করে Source 6সোর্স 6।
TTL (time to live) হলো একটি cached item-এর মেয়াদ Source 1সোর্স 1। Redis-এ EXPIRE command একটি key-তে timeout ঠিক করে, আর timeout শেষ হলে Redis নিজে থেকেই key-টি মুছে দেয় Source 3সোর্স 3। TTL command দেখায় একটি key-র আর কত সময় বাকি। Key-র কোনো expiry না থাকলে এটি -1 দেয়, আর key না থাকলে -2 দেয় Source 4সোর্স 4।
Memory সীমিত। আপনি একটি eviction policy ঠিক করলে, cache একটি memory limit পার হলে Redis নিজেই key সরিয়ে দেয় Source 5সোর্স 5। যেমন, allkeys-lru policy সবচেয়ে কম সাম্প্রতিক সময়ে ব্যবহৃত key-গুলো সরায় Source 5সোর্স 5।
When you need it
- The same data is read often.
- The database load is high.
- Response time must improve.
- The data does not change very often. Caching works best for relatively static data or data that is read often Source 6সোর্স 6. It is also best if the data can be a little old (tutorial advice).
- External API calls are expensive or slow.
সহজ English
A cache helps in these cases:
- The app reads the same data often.
- The database has a high load.
- The response time must be better.
- The data does not change very often. Caching works best for relatively static data or data that the app reads often Source 6সোর্স 6. It is also best if the data can be a little old (tutorial advice).
- Calls to external APIs are expensive or slow.
বাংলা ব্যাখ্যা
নিচের অবস্থাগুলোতে cache কাজে আসে:
- একই data বারবার পড়া হয়।
- Database-এর load বেশি।
- Response time উন্নত করা দরকার।
- Data খুব বেশি বদলায় না। যে data তুলনামূলক static বা প্রায়ই পড়া হয়, সেই data-র জন্য caching সবচেয়ে ভালো কাজ করে Source 6সোর্স 6। Data একটু পুরোনো হলেও চললে আরও ভালো (এটি tutorial-এর নিজস্ব পরামর্শ)।
- External API call ব্যয়বহুল বা ধীর।
A real-life example
In an LMS (learning management system), students open the course list thousands of times each day. But the course information changes only sometimes.
So the app caches the course list in Redis for 10 minutes. Most requests then never touch the database.
Then an admin changes the price of a course. If the app does not invalidate the related cache key, students see the old price for up to 10 minutes. Worse, the checkout can show a price that is different from the list. So the update code must also delete the course list key and the key of that course.
সহজ English
In an LMS (learning management system), students open the course list thousands of times each day. The course information changes only sometimes.
So the app keeps the course list in Redis for 10 minutes. Most requests then do not go to the database.
Then an admin changes the price of a course. If the app does not invalidate the related cache key, students see the old price for up to 10 minutes. Also, the checkout can show a price that is not the same as the price in the list. So the update code must also delete the course list key and the key of that course.
বাংলা ব্যাখ্যা
একটি LMS-এ (learning management system) শিক্ষার্থীরা প্রতিদিন হাজার হাজার বার course list খোলেন। কিন্তু course-এর তথ্য মাঝে মাঝে বদলায়।
তাই app course list-টি 10 মিনিটের জন্য Redis-এ cache করে রাখে। এতে বেশিরভাগ request database পর্যন্ত যায়ই না।
এরপর একজন admin একটি course-এর দাম বদলান। App যদি সংশ্লিষ্ট cache key invalidate না করে, তাহলে শিক্ষার্থীরা সর্বোচ্চ 10 মিনিট পর্যন্ত পুরোনো দাম দেখেন। আরও খারাপ হলো, checkout-এ list-এর চেয়ে আলাদা দাম দেখাতে পারে। তাই update code-কে course list key এবং সেই course-এর key-ও মুছে ফেলতে হবে।
Common mistakes
- Caching user data with the wrong key. If the key is
dashboardand notdashboard:user:42, user B can get user A's dashboard. Use clear, structured keys, such ascustomer:100Source 7সোর্স 7. Also protect the cache itself from unauthorized access Source 7সোর্স 7. - Using a very long TTL. If the expiry is too long, the cached data becomes stale. If it is too short, the app keeps going back to the database Source 6সোর্স 6.
- No invalidation plan. Cache-aside does not guarantee that the cache and the database agree Source 6সোর্স 6. Decide which change deletes which keys.
- Making the whole app fail when Redis is down. The app should keep working if the cache is not available Source 7সোর্স 7. It should fall back to the database Source 7সোর্স 7. But plan for the extra load: while the cache recovers, the database can get too many requests and time out Source 7সোর্স 7.
- Confusing speed with correctness. A fast answer is not always a correct answer. The cache and the database can disagree for a while. A change made outside the app does not show in the cache until the item loads again Source 6সোর্স 6. Do not use the cache as the only store of critical data. Save every important change to a persistent store Source 7সোর্স 7.
সহজ English
- Caching user data with the wrong key. If the key is
dashboardand notdashboard:user:42, user B can get the dashboard of user A. Use clear, structured keys, such ascustomer:100Source 7সোর্স 7. Also protect the cache from unauthorized access Source 7সোর্স 7. - Using a very long TTL. If the expiry is too long, the cached data becomes old (stale). If it is too short, the app goes to the database too often Source 6সোর্স 6.
- No invalidation plan. Cache-aside does not make sure that the cache and the database agree Source 6সোর্স 6. Decide which change deletes which keys.
- The whole app fails when Redis is down. When the cache is not available, the app should continue to work and use the database Source 7সোর্স 7. But while the cache recovers, the database can get too many requests and time out Source 7সোর্স 7.
- Fast is not the same as correct. The cache and the database can disagree for a while. A change made outside the app does not show in the cache until the item loads again Source 6সোর্স 6. Do not keep critical data only in the cache. Save each important change to a persistent store Source 7সোর্স 7.
বাংলা ব্যাখ্যা
এই সাধারণ ভুলগুলোর দিকে খেয়াল রাখুন:
- ভুল key দিয়ে user data cache করা। Key যদি
dashboard:user:42না হয়েdashboardহয়, তাহলে user B, user A-র dashboard পেয়ে যেতে পারেন।customer:100-এর মতো স্পষ্ট, গোছানো key ব্যবহার করুন Source 7সোর্স 7। Cache-কেও unauthorized access থেকে রক্ষা করুন Source 7সোর্স 7। - খুব লম্বা TTL ব্যবহার করা। Expiry খুব লম্বা হলে cached data stale, অর্থাৎ পুরোনো, হয়ে যায়। খুব ছোট হলে app বারবার database-এ ফিরে যায় Source 6সোর্স 6।
- Invalidation-এর কোনো পরিকল্পনা না থাকা। Cache-aside নিশ্চিত করে না যে cache আর database একই data রাখবে Source 6সোর্স 6। কোন পরিবর্তনে কোন key মুছবে, তা আগে ঠিক করুন।
- Redis down হলে পুরো app fail করা। Cache না পাওয়া গেলেও app-কে কাজ চালিয়ে যেতে হবে এবং database-এ fall back করতে হবে Source 7সোর্স 7। তবে বাড়তি load-এর জন্য পরিকল্পনা রাখুন: cache recover করার সময় database-এ অনেক বেশি request এসে time out হতে পারে Source 7সোর্স 7।
- গতি আর সঠিকতাকে গুলিয়ে ফেলা। দ্রুত উত্তর মানেই সবসময় সঠিক উত্তর নয়। Cache আর database কিছু সময় একমত নাও থাকতে পারে। App-এর বাইরে করা কোনো পরিবর্তন item আবার load না হওয়া পর্যন্ত cache-এ দেখা যায় না Source 6সোর্স 6। গুরুত্বপূর্ণ data শুধু cache-এ রাখবেন না। প্রতিটি গুরুত্বপূর্ণ পরিবর্তন একটি persistent store-এ save করুন Source 7সোর্স 7।
What a tester can check
Tutorial advice
These test ideas are the tutorial's own advice.
- Update, then read. Change a course price as an admin. Open the course list and the checkout at once. Both must show the new price.
- Two users. Log in as user A, then as user B, and open the same personal page. User B must never see user A's data from the cache.
- Check the TTL. In a test environment, run
TTLon a cached key. A key with-1never expires. Ask if that is on purpose. - Redis down. Stop Redis in a test environment. The app must still answer from the database, maybe slower, with no error page.
- Cold cache. Clear the cache and send a burst of traffic. Watch the database load and the response times while the cache fills again.
সহজ English
These test ideas are the tutorial's own advice.
- Update, then read. As an admin, change the price of a course. Open the course list and the checkout at the same time. Both must show the new price.
- Two users. Log in as user A, then as user B, and open the same personal page. User B must never see the data of user A from the cache.
- Check the TTL. In a test environment, run
TTLon a cached key. A key with-1never expires. Ask if this is on purpose. - Redis down. Stop Redis in a test environment. The app must still answer from the database, maybe more slowly, and show no error page.
- Cold cache. Clear the cache and send a burst of traffic. Watch the database load and the response times while the cache fills again.
বাংলা ব্যাখ্যা
নিচের test ধারণাগুলো এই tutorial-এর নিজস্ব পরামর্শ।
- আগে update, তারপর read। Admin হিসেবে একটি course-এর দাম বদলান। একই সময়ে course list আর checkout খুলুন। দুই জায়গাতেই নতুন দাম দেখাতে হবে।
- দুজন user। প্রথমে user A, তারপর user B হিসেবে log in করুন এবং একই personal page খুলুন। User B যেন কখনোই cache থেকে user A-র data না দেখেন।
- TTL পরীক্ষা করুন। একটি test environment-এ একটি cached key-তে
TTLচালান।-1থাকা key কখনো expire হয় না। এটি ইচ্ছাকৃত কি না, জিজ্ঞেস করুন। - Redis down। একটি test environment-এ Redis বন্ধ করুন। App-কে তবুও database থেকে উত্তর দিতে হবে, হয়তো একটু ধীরে, কিন্তু কোনো error page ছাড়া।
- Cold cache। Cache খালি করে একসাথে অনেক traffic পাঠান। Cache আবার ভরে ওঠার সময় database load আর response time খেয়াল করুন।
Key words
| Word | Meaning |
|---|---|
| Cache | Fast temporary storage that keeps copies of often-used data |
| Redis | An in-memory data store, often used as a cache |
| Cache hit | The data is found in the cache |
| Cache miss | The data is not in the cache, so the app reads the main store |
| TTL | Time to live: how long a cached item stays before it expires |
| Invalidation | Removing or updating an old item in the cache |
| Cache-aside | A pattern where the app reads the cache first and fills it on a miss |
| Eviction | Removing items automatically when the cache is full |
| Stale data | Old data that no longer matches the main store |