Distributed Cache
Think of an office where each person keeps notes on their own sticky pads. When a fact changes, some pads get the update and some do not. Now put one whiteboard on the wall that everyone reads and writes. Everyone sees the same fact. A distributed cache is that whiteboard for your app servers: one fast, shared store that all of them use.
6 min read6 মিনিটে পড়া যায় · 7 sections7টি সেকশন · 8 sources8টি সোর্স · updated Oct 2026আপডেট: Oct 2026
Sticky pads versus one whiteboardস্টিকি প্যাড বনাম একটাই হোয়াইটবোর্ড
Check an OTP on three app servers with local caches, then with one shared Redis.তিনটা অ্যাপ সার্ভারে লোকাল ক্যাশ দিয়ে একটা OTP যাচাই করুন, তারপর একটাই শেয়ার্ড Redis দিয়ে।
You’ll learnআপনি শিখবেন
- Why a local cache differs per serverলোকাল ক্যাশ কেন সার্ভারে সার্ভারে আলাদা
- How one shared cache gives one answerএকটাই শেয়ার্ড ক্যাশ কীভাবে একই উত্তর দেয়
- What a TTL does to an OTPTTL একটা OTP-র সাথে কী করে
- What to test when Redis is downRedis বন্ধ হলে কী পরীক্ষা করবেন
In plain words
A cache keeps copies of data in fast memory, so the app does not ask the database every time. There are two kinds. A private cache, or local cache, keeps the data on the computer that runs the app Source 1সোর্স 1. A shared cache is one common store that many processes and machines can use Source 1সোর্স 1. A distributed cache is a shared cache.
The problem with a local cache starts when you run more than one app server. Each server then has its own separate copy of the data Source 1সোর্স 1. If the data changes between two reads, two servers can hold two different answers Source 1সোর্স 1. With a shared cache, all servers see the same cached data Source 1সোর্স 1. The price is speed and effort: a shared cache is slower than local memory, and it is one more service to run Source 1সোর্স 1.
This page uses Redis as the example. Redis can serve as a shared cache for many app instances Source 1সোর্স 1. Redis Cluster splits the data across many Redis nodes automatically Source 4সোর্স 4. Redis Sentinel watches a main Redis server and, if it fails, promotes a replica (a copy) to be the new main server Source 3সোর্স 3. A managed Redis, such as Azure Managed Redis, is run for you by the cloud vendor Source 2সোর্স 2.
Common uses are:
- Session store. Data that the app links to each user's session, such as a shopping cart Source 2সোর্স 2.
- OTP and other short-lived data. Redis can give a key a timeout. After the timeout, Redis deletes the key by itself Source 5সোর্স 5.
- Rate limit counter. A counter that limits how often an action can happen Source 8সোর্স 8. See Rate Limiting.
- Leaderboard. Redis has a sorted set type for ranked lists Source 2সোর্স 2.
- Distributed lock. A "room in use" sign, so only one server does a job at a time Source 6সোর্স 6.
- Feature flags and temporary state. Small values that every server must read the same way.
Open the image in full sizeছবিটি পূর্ণ আকারে খুলুনসহজ English
A cache keeps copies of data in fast memory, so the app does not ask the database every time. A private cache, or local cache, keeps the data on the computer that runs the app Source 1সোর্স 1. A shared cache is one common store that many processes and machines can use Source 1সোর্স 1. A distributed cache is a shared cache.
With many app servers and a local cache, each server has its own copy of the data Source 1সোর্স 1. If the data changes, two servers can give two different answers Source 1সোর্স 1. With a shared cache, all servers see the same data Source 1সোর্স 1. A shared cache is slower than local memory, and it is one more service to run Source 1সোর্স 1.
This page uses Redis. Redis Cluster splits the data across many Redis nodes automatically Source 4সোর্স 4. If the main Redis server fails, Redis Sentinel makes a copy (a replica) the new main server Source 3সোর্স 3.
Common uses are session data such as a shopping cart Source 2সোর্স 2, OTP codes that Redis deletes after a timeout Source 5সোর্স 5, rate limit counters Source 8সোর্স 8, leaderboards Source 2সোর্স 2, locks Source 6সোর্স 6, and feature flags.
বাংলা ব্যাখ্যা
Cache দ্রুত memory-তে data-র copy রাখে, যাতে app-কে প্রতিবার database-এর কাছে যেতে না হয়। Cache দুই ধরনের। Private cache বা local cache সেই computer-এই data রাখে, যেখানে app চলে Source 1সোর্স 1। Shared cache হলো একটি সাধারণ store, যা অনেক process আর machine ব্যবহার করতে পারে Source 1সোর্স 1। Distributed cache হলো একটি shared cache।
আপনি একাধিক app server চালালে local cache-এ সমস্যা শুরু হয়। তখন প্রতিটি server-এর কাছে data-র নিজস্ব আলাদা copy থাকে Source 1সোর্স 1। দুইবার পড়ার মাঝে data বদলালে দুটি server দুটি ভিন্ন উত্তর রাখতে পারে Source 1সোর্স 1। Shared cache থাকলে সব server একই cached data দেখে Source 1সোর্স 1। তবে এর একটি দাম আছে: shared cache local memory-র চেয়ে ধীর, আর এটি চালানোর জন্য আরও একটি service Source 1সোর্স 1।
এই পাতায় উদাহরণ হিসেবে Redis ব্যবহার করা হয়েছে। Redis অনেক app instance-এর জন্য shared cache হিসেবে কাজ করতে পারে Source 1সোর্স 1। Redis Cluster নিজে থেকেই data অনেক Redis node-এ ভাগ করে দেয় Source 4সোর্স 4। Redis Sentinel একটি main Redis server-কে নজরে রাখে, আর সেটি fail করলে একটি replica (copy)-কে নতুন main server বানায় Source 3সোর্স 3। Azure Managed Redis-এর মতো managed Redis আপনার হয়ে cloud vendor চালায় Source 2সোর্স 2।
সাধারণ ব্যবহারগুলো হলো: session store, যেমন shopping cart Source 2সোর্স 2; OTP-র মতো অল্প সময়ের data, যেখানে timeout শেষ হলে Redis নিজেই key মুছে দেয় Source 5সোর্স 5; rate limit counter Source 8সোর্স 8; sorted set দিয়ে leaderboard Source 2সোর্স 2; distributed lock, যাতে একসময়ে শুধু একটি server একটি কাজ করে Source 6সোর্স 6; আর feature flag ও temporary state, যা প্রতিটি server-কে একইভাবে পড়তে হয়।
Frontend-facing Redis and backend Redis
One system can use Redis for two different jobs. "Frontend-facing" and "backend" are this tutorial's names for the two jobs. They are not product terms.
| Role | Goal | Examples |
|---|---|---|
| Frontend-facing Redis | Make answers to users faster | Course lists, public landing page data, dashboard summaries, menu and config data, popular API responses. Azure calls this a data cache or a content cache Source 2সোর্স 2. |
| Backend Redis | Run internal logic and background work | OTP, login and session data, rate limit counters, job queue state, distributed locks, payment state, temporary workflow data. Session store and job queuing are common Redis patterns Source 2সোর্স 2. |
Tutorial advice
The two roles have different risks. If a frontend-facing cache loses data, the page is a little slower. If a backend Redis loses an OTP or a lock, a user cannot log in, or two servers do the same job. Think about separate Redis instances for the two roles, so that a big page cache cannot push out login data.
সহজ English
One system can use Redis for two different jobs. "Frontend-facing" and "backend" are this tutorial's names for the two jobs. They are not product terms.
- Frontend-facing Redis makes the answers to users faster. Examples are course lists, public landing page data, dashboard summaries, menu and config data, and popular API responses. Azure calls this a data cache or a content cache Source 2সোর্স 2.
- Backend Redis runs internal logic and background work. Examples are OTP, login and session data, rate limit counters, job queue state, distributed locks, payment state, and temporary workflow data. Session store and job queuing are common Redis patterns Source 2সোর্স 2.
The next advice is the tutorial's own. The two jobs have different risks. If a frontend-facing cache loses data, the page is a little slower. If a backend Redis loses an OTP or a lock, a user cannot log in, or two servers do the same job. Think about separate Redis instances for the two jobs. Then a big page cache cannot push out login data.
বাংলা ব্যাখ্যা
একটি system দুটি আলাদা কাজে Redis ব্যবহার করতে পারে। "Frontend-facing" আর "backend" হলো এই দুই কাজের জন্য এই tutorial-এর নিজের দেওয়া নাম। এগুলো কোনো product term নয়।
- Frontend-facing Redis user-দের কাছে উত্তর দ্রুত পৌঁছে দেয়। উদাহরণ: course list, public landing page data, dashboard summary, menu ও config data, আর জনপ্রিয় API response। Azure একে data cache বা content cache বলে Source 2সোর্স 2।
- Backend Redis ভেতরের logic আর background-এর কাজ চালায়। উদাহরণ: OTP, login ও session data, rate limit counter, job queue state, distributed lock, payment state, আর temporary workflow data। Session store আর job queuing হলো Redis-এর সাধারণ pattern Source 2সোর্স 2।
নিচের পরামর্শটি এই tutorial-এর নিজস্ব। দুই কাজের ঝুঁকি আলাদা। Frontend-facing cache data হারালে পাতাটি একটু ধীর হয়। কিন্তু backend Redis একটি OTP বা lock হারালে একজন user login করতে পারেন না, অথবা দুটি server একই কাজ করে ফেলে। তাই দুই কাজের জন্য আলাদা Redis instance রাখার কথা ভাবুন, যাতে একটি বড় page cache login data-কে সরিয়ে দিতে না পারে।
When you need it
- You run more than one app server.
- A session, an OTP, or a rate limit counter must be the same on all servers.
- A local memory cache gives different answers on different servers.
- You need fast, shared, temporary state.
- You plan horizontal scaling.
সহজ English
You need a distributed cache in these cases:
- You run more than one app server.
- A session, an OTP, or a rate limit counter must be the same on all servers.
- A local memory cache gives different answers on different servers.
- You need state that is fast, shared, and temporary.
- You plan horizontal scaling.
বাংলা ব্যাখ্যা
নিচের যেকোনো একটি অবস্থা আপনার system-এর সাথে মিলে গেলে, আপনার একটি distributed cache দরকার:
- আপনি একাধিক app server চালান।
- একটি session, একটি OTP, বা একটি rate limit counter সব server-এ একই হতে হবে।
- একটি local memory cache আলাদা server-এ আলাদা উত্তর দেয়।
- আপনার দ্রুত, shared, আর temporary state দরকার।
- আপনি horizontal scaling-এর পরিকল্পনা করছেন।
A real-life example
An LMS uses OTP login. When a student asks for a code, the app stores it in Redis with a key such as otp:<phone> and a time-to-live (TTL) of 2 minutes:
SET otp:8801700000000 482913 EX 120The load balancer can send the verify request to App Server 1, 2, or 3. All three read the same key from Redis, so the check works on any server. After 2 minutes, Redis deletes the key, and the old code stops working.
সহজ English
An LMS uses OTP login. A student asks for a code. The app stores the code in Redis with a key such as otp:<phone>. The key has a time-to-live (TTL) of 2 minutes.
The load balancer can send the verify request to App Server 1, 2, or 3. All three servers read the same key from Redis. So the check works on any server. After 2 minutes, Redis deletes the key, and the old code stops working.
বাংলা ব্যাখ্যা
ধরুন, একটি LMS OTP দিয়ে login করায়। একজন শিক্ষার্থী code চাইলে app সেটি Redis-এ otp:<phone>-এর মতো একটি key দিয়ে রাখে, যার time-to-live (TTL) 2 মিনিট।
Load balancer verify request-টি App Server 1, 2, বা 3-এ পাঠাতে পারে। তিনটি server-ই Redis থেকে একই key পড়ে, তাই যেকোনো server-এ check কাজ করে। 2 মিনিট পরে Redis key-টি মুছে দেয়, আর পুরোনো code আর কাজ করে না।
Common mistakes
- A local cache on many servers. Each server keeps its own copy, so users see different data on each request.
- No plan for a cache failure. The app should keep working when the shared cache is down Source 1সোর্স 1. For data that lives only in Redis, such as an OTP, the app must fail with a clear message, not a crash.
- The cache as a permanent database. A cache can lose its data at any time. Keep valuable data in the real data store too Source 1সোর্স 1.
- A lock that never ends. If a server takes a lock and then crashes, other servers wait forever. A Redis lock should set the key only if it does not exist (
NX) and with an expiry (PX) Source 6সোর্স 6. The holder must remove only its own lock Source 6সোর্স 6. - No key naming rule. Redis advises a schema such as
object-type:id, for exampleuser:1000Source 7সোর্স 7. Without a rule, two features can use the same key.
সহজ English
Look out for these common mistakes:
- A local cache on many servers. Each server keeps its own copy. So users see different data on each request.
- No plan for a cache failure. The app should continue to work when the shared cache is down Source 1সোর্স 1. Some data lives only in Redis, such as an OTP. For that data, the app must fail with a clear message, not a crash.
- The cache as a permanent database. A cache can lose its data at any time. Keep valuable data in the real data store too Source 1সোর্স 1.
- A lock that never ends. A server takes a lock and then crashes. Now the other servers wait forever. A Redis lock should set the key only if it does not exist (
NX), and with an expiry (PX) Source 6সোর্স 6. The holder must remove only its own lock Source 6সোর্স 6. - No key naming rule. Redis advises a schema such as
object-type:id, for exampleuser:1000Source 7সোর্স 7. Without a rule, two features can use the same key.
বাংলা ব্যাখ্যা
এই সাধারণ ভুলগুলোর দিকে খেয়াল রাখুন:
- অনেক server-এ local cache। প্রতিটি server নিজের আলাদা copy রাখে, তাই user-রা প্রতিটি request-এ আলাদা data দেখেন।
- Cache fail করলে কী হবে, তার কোনো পরিকল্পনা নেই। Shared cache বন্ধ থাকলেও app-এর কাজ চালিয়ে যাওয়া উচিত Source 1সোর্স 1। যে data শুধু Redis-এ থাকে, যেমন OTP, সেখানে app-কে crash না করে একটি পরিষ্কার message দিয়ে fail করতে হবে।
- Cache-কে স্থায়ী database হিসেবে ব্যবহার। Cache যেকোনো সময় তার data হারাতে পারে। মূল্যবান data আসল data store-এও রাখুন Source 1সোর্স 1।
- এমন lock, যা কখনো শেষ হয় না। কোনো server lock নিয়ে crash করলে অন্য server-গুলো চিরকাল অপেক্ষা করে। Redis lock-এর key শুধু তখনই set করা উচিত, যখন key-টি নেই (
NX), আর সাথে একটি expiry (PX) দিতে হবে Source 6সোর্স 6। Lock-এর মালিক শুধু নিজের lock-টিই সরাবে Source 6সোর্স 6। - Key-এর নামের কোনো নিয়ম নেই। Redis
object-type:id-এর মতো একটি schema-র পরামর্শ দেয়, যেমনuser:1000Source 7সোর্স 7। নিয়ম না থাকলে দুটি feature একই key ব্যবহার করে ফেলতে পারে।
What a tester can check
Tutorial advice
- Same answer on every server. Send the same read to each app server (or repeat it many times through the load balancer). Change the data, then check that no server returns the old value.
- OTP across servers. Request an OTP on one server and verify it on another. Then wait past the TTL and check that the code fails.
- Redis down. Stop Redis in a test environment. Check that cached pages still load from the database, and that login fails with a clear error.
- Lock expiry. Kill a worker while it holds a lock. Check that another worker can take the lock after the expiry.
- Key collisions. Check that each key has a type prefix and an ID, so that
session:42andotp:42never mix.
সহজ English
These test ideas are the tutorial's own advice.
- Same answer on every server. Send the same read to each app server, or repeat it many times through the load balancer. Change the data. Then make sure that no server returns the old value.
- OTP across servers. Request an OTP on one server and verify it on another. Then wait past the TTL and make sure that the code fails.
- Redis down. Stop Redis in a test environment. Make sure that cached pages still load from the database, and that login fails with a clear error.
- Lock expiry. Stop a worker by force while it holds a lock. Make sure that another worker can take the lock after the expiry.
- Key collisions. Make sure that each key has a type prefix and an ID, so that
session:42andotp:42never mix.
বাংলা ব্যাখ্যা
নিচের test ধারণাগুলো এই tutorial-এর নিজস্ব পরামর্শ।
- প্রতিটি server-এ একই উত্তর। প্রতিটি app server-এ একই read পাঠান, অথবা load balancer-এর মাধ্যমে অনেকবার পাঠান। Data বদলান, তারপর দেখুন কোনো server পুরোনো value ফেরত দেয় কি না।
- বিভিন্ন server-এ OTP। একটি server-এ OTP চান আর অন্য server-এ verify করুন। তারপর TTL পার হওয়া পর্যন্ত অপেক্ষা করে দেখুন code-টি fail করে কি না।
- Redis down। একটি test environment-এ Redis বন্ধ করুন। দেখুন, cached page-গুলো database থেকে এখনো load হয় কি না, আর login একটি পরিষ্কার error দিয়ে fail করে কি না।
- Lock expiry। একটি worker lock ধরে রাখার সময় সেটিকে kill করুন। দেখুন, expiry-র পরে অন্য একটি worker lock নিতে পারে কি না।
- Key collision। দেখুন, প্রতিটি key-তে একটি type prefix আর একটি ID আছে কি না, যাতে
session:42আরotp:42কখনো মিশে না যায়।
Key words
| Word | Meaning |
|---|---|
| Cache | Fast memory that keeps copies of data |
| Local (private) cache | A cache inside one app server, seen only by that server |
| Distributed (shared) cache | One cache that all app servers use |
| TTL | Time to live: how long a key stays before Redis deletes it |
| Redis Cluster | Redis spread over many nodes, with the data split between them |
| Redis Sentinel | A Redis tool that watches the main server and switches to a replica if it fails |
| Distributed lock | A shared "in use" sign, so only one server does a job at a time |
| Key schema | A naming rule for keys, such as otp:<phone> |