Since you can only store a limited amount of data in cache, you’ll need to determine which cache update strategy works
best for your use case.
🟡 Focus: Application-level interaction with cache
Strategy
Description
Example Use Case
Cache-aside (Lazy Loading)
App checks cache first. If a miss, it loads from DB and inserts into cache.
Read-heavy systems like user profile lookups
Write-through
Writes go to cache and DB at the same time.
Product catalog where consistency is important
Write-behind (Write-back)
Writes go to cache, and are written to DB asynchronously.
Metrics/log data ingestion
Read-through
Cache itself knows how to fetch from DB on a miss.
Abstracted cache layers (e.g., using a cache proxy)
Refresh-ahead
Cache preemptively refreshes soon-to-expire keys.
Time-sensitive data like exchange rates
Cache-Aside
The application is responsible for reading and writing from storage. The cache does not interact with storage directly.
The application does the following:
Look for entry in the cache, resulting in a cache miss
Load entry from the database
Add entry to cache
Return entry
def get_user(self, user_id): user = cache.get("user.{0}", user_id) if user is None: user = db.query("SELECT * FROM users WHERE user_id = {0}", user_id) if user is not None: key = "user.{0}".format(user_id) cache.set(key, json.dumps(user)) return user
Memcached is generally used in this manner.
Subsequent reads of data added to cache are fast. Cache-aside is also referred to as lazy loading. Only requested data
is cached, which avoids filling up the cache with data that isn’t requested.Disadvantage(s): cache-aside
Each cache miss results in three trips, which can cause a noticeable delay.
Data can become stale if it is updated in the database. This issue is mitigated by setting a time-to-live (TTL) which
forces an update of the cache entry, or by using write-through.
When a node fails, it is replaced by a new, empty node, increasing latency.
Write-Through
The application uses the cache as the main data store, reading and writing data to it, while the cache is responsible
for reading and writing to the database:
Application adds/updates entry in cache
Cache synchronously writes entry to data store
Return
Application code
set_user(12345, {"foo":"bar"})
Cache code:
def set_user(user_id, values): user = db.query("UPDATE Users WHERE id = {0}", user_id, values) cache.set(user_id, user)
Write-through is a slow overall operation due to the write operation, but subsequent reads of just written data are fast
. Users are generally more tolerant of latency when updating data than reading data. Data in the cache is not stale.Disadvantage(s): write through
When a new node is created due to failure or scaling, the new node will not cache entries until the entry is updated in
the database. Cache-aside in conjunction with write through can mitigate this issue.
Most data written might never be read, which can be minimized with a TTL.
Write-Behind (write-back)
In write-behind, the application does the following:
Add/update entry in cache
Asynchronously write entry to the data store, improving write performance
Disadvantage(s): write-behind
There could be data loss if the cache goes down prior to its contents hitting the data store.
It is more complex to implement write-behind than it is to implement cache-aside or write-through.
Refresh-ahead
You can configure the cache to automatically refresh any recently accessed cache entry prior to its expiration.
Refresh-ahead can result in reduced latency vs read-through if the cache can accurately predict which items are likely to be needed in the future.Disadvantage(s): refresh-ahead
Not accurately predicting which items are likely to be needed in the future can result in reduced performance than without refresh-ahead.
Cache Invalidation Strategies
Cache invalidation strategies will go here,
Disadvantage(s): cache
Need to maintain consistency between caches and the source of truth such as the database through cache invalidation.
Cache invalidation is a difficult problem, there is additional complexity associated with when to update the cache.
Need to make application changes such as adding Redis or memcached.
{"docsConfig":{"theme":"mint","$schema":"https://mintlify.com/docs.json","name":"Mint Starter Kit","colors":{"primary":"#16A34A","light":"#07C983","dark":"#15803D"},"logo":{"light":"https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/logo/light.svg?fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=07a697a3d04ef4dc936a2014794adeae","dark":"https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/logo/dark.svg?fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=010af35734daa4b92a641bfc03707af7"},"favicon":"/favicon.svg","navbar":{"links":[{"label":"Support","href":"mailto:hi@mintlify.com"}],"primary":{"type":"button","label":"Dashboard","href":"https://dashboard.mintlify.com"}},"footer":{"socials":{"x":"https://x.com/mintlify","github":"https://github.com/mintlify","linkedin":"https://linkedin.com/company/mintlify"}},"navigation":{"global":{},"pages":[]}},"deployment":{"deploymentMetadataValue":{"subdomain":"akshanshgusain","hasAudioNarration":false,"actualSubdomain":"akshanshgusain","customDomains":[],"gitSource":{"type":"github","owner":"akshanshgusain","repo":"cheat-sheet","deployBranch":"main","contentDirectory":"","isPrivate":true,"mountPath":"","label":""},"entitlements":{},"buildId":"6ae92a8b424103980001f2bd:success","clientVersion":"0.0.4340","searchFilterCounts":[{"language":"default","tag":"High Level Design","count":46},{"language":"default","tag":"HLD Fundamentals","count":42},{"language":"default","tag":"Primer","count":41},{"language":"default","tag":"Fundamentals","count":27},{"language":"default","tag":"Low Level Design","count":25},{"language":"default","tag":"Database","count":14},{"language":"default","tag":"Design Patterns","count":14},{"language":"default","tag":"DSA","count":9},{"language":"default","tag":"NoSQL","count":8},{"language":"default","tag":"Network and Protocols","count":6}]},"assistantConfig":{},"basePath":"","hasChatPermissions":false,"hasWebMcpPermissions":false,"toggles":[{"name":"kill-isr","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"custom-rbac-roles","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"agent-mintlify-slack","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"agent-integrations-settings","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"rbac-expansion","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automations-pr-grouping","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"custom-prefix","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"analytics-v7","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"user-flows-analytics","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"no-publish-flow","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"snapshot-serving-live","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"stripTemperatureMiddleware","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"support-platform-pylon-zendesk","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"snapshot-serving","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"instant-rollback","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"granular-notifications-server","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"granular-notifications","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automations-fable","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"fixed-ai-action-pricing","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"site-roles","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"automation-resolve-merge-conflict","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"scoped-publishing","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"mintql","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"page-level-analytics","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"editor-version-history-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"billing-page-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"full-page-search","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"workspace-projects","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"fork-pr-previews","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"revalidation-jwt","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"hobby-abuse-scan","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"dashboard-inbox","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"turbopuffer-dual-write","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"approve-review-flow","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"editor-metadata","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"turbopuffer-serve","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"live-agents","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"starter-pricing","enabled":true,"variant":{"name":"treatment","enabled":true,"payload":{"type":"string","value":"treatment"},"feature_enabled":true},"impressionData":false},{"name":"llms-txt-v2","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false},{"name":"wildcard-email-auth","enabled":true,"variant":{"name":"disabled","enabled":false,"feature_enabled":true},"impressionData":false}],"starterQuestions":[],"googleFontsInlined":false},"customCss":[]}
{"page":{"pagePath":"hld/fundamentals/primer/cache/caching-strategies","slug":"/hld/fundamentals/primer/cache/caching-strategies","pageMetadata":{"title":"Caching Strategies - \"How and When You Cache\"","description":null,"sidebarTitle":"Caching Strategies","href":"/hld/fundamentals/primer/cache/caching-strategies"},"pageType":"default","mdx":{"mdxSource":{"compiledSource":"\"use strict\";const{Fragment:_Fragment,jsx:_jsx,jsxs:_jsxs}=arguments[0];const{useMDXComponents:_provideComponents}=arguments[0];function _createMdxContent(props){const _components={code:`code`,em:`em`,li:`li`,p:`p`,pre:`pre`,span:`span`,strong:`strong`,tbody:`tbody`,td:`td`,th:`th`,thead:`thead`,tr:`tr`,ul:`ul`,..._provideComponents(),...props.components},{CodeBlock,Frame,Heading,Info,OptimizedImage,Table}=_components;if(!CodeBlock)_missingMdxReference(`CodeBlock`,true);if(!Frame)_missingMdxReference(`Frame`,true);if(!Heading)_missingMdxReference(`Heading`,true);if(!Info)_missingMdxReference(`Info`,true);if(!OptimizedImage)_missingMdxReference(`OptimizedImage`,true);if(!Table)_missingMdxReference(`Table`,true);return _jsxs(_Fragment,{children:[_jsx(Heading,{level:`2`,id:`caching-strategies`,children:`Caching Strategies`}),`\n`,_jsx(_components.p,{children:_jsx(_components.strong,{children:`Since you can only store a limited amount of data in cache, you’ll need to determine which cache update strategy works\nbest for your use case.`})}),`\n`,_jsx(Info,{children:_jsx(_components.p,{children:`🟡 Focus: Application-level interaction with cache`})}),`\n`,_jsxs(Table,{children:[_jsx(_components.thead,{children:_jsxs(_components.tr,{children:[_jsx(_components.th,{style:{textAlign:`left`},children:`Strategy`}),_jsx(_components.th,{style:{textAlign:`left`},children:`Description`}),_jsx(_components.th,{style:{textAlign:`left`},children:`Example Use Case`})]})}),_jsxs(_components.tbody,{children:[_jsxs(_components.tr,{children:[_jsx(_components.td,{style:{textAlign:`left`},children:_jsx(_components.strong,{children:`Cache-aside (Lazy Loading)`})}),_jsx(_components.td,{style:{textAlign:`left`},children:`App checks cache first. If a miss, it loads from DB and inserts into cache.`}),_jsx(_components.td,{style:{textAlign:`left`},children:`Read-heavy systems like user profile lookups`})]}),_jsxs(_components.tr,{children:[_jsx(_components.td,{style:{textAlign:`left`},children:_jsx(_components.strong,{children:`Write-through`})}),_jsxs(_components.td,{style:{textAlign:`left`},children:[`Writes go to cache `,_jsx(_components.em,{children:`and`}),` DB at the same time.`]}),_jsx(_components.td,{style:{textAlign:`left`},children:`Product catalog where consistency is important`})]}),_jsxs(_components.tr,{children:[_jsx(_components.td,{style:{textAlign:`left`},children:_jsx(_components.strong,{children:`Write-behind (Write-back)`})}),_jsxs(_components.td,{style:{textAlign:`left`},children:[`Writes go to cache, and are written to DB `,_jsx(_components.em,{children:`asynchronously`}),`.`]}),_jsx(_components.td,{style:{textAlign:`left`},children:`Metrics/log data ingestion`})]}),_jsxs(_components.tr,{children:[_jsx(_components.td,{style:{textAlign:`left`},children:_jsx(_components.strong,{children:`Read-through`})}),_jsx(_components.td,{style:{textAlign:`left`},children:`Cache itself knows how to fetch from DB on a miss.`}),_jsx(_components.td,{style:{textAlign:`left`},children:`Abstracted cache layers (e.g., using a cache proxy)`})]}),_jsxs(_components.tr,{children:[_jsx(_components.td,{style:{textAlign:`left`},children:_jsx(_components.strong,{children:`Refresh-ahead`})}),_jsx(_components.td,{style:{textAlign:`left`},children:`Cache preemptively refreshes soon-to-expire keys.`}),_jsx(_components.td,{style:{textAlign:`left`},children:`Time-sensitive data like exchange rates`})]})]})]}),`\n`,_jsx(Heading,{level:`3`,id:`cache-aside`,children:`Cache-Aside`}),`\n`,_jsx(Frame,{children:_jsx(OptimizedImage,{src:`https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/cache-aside.png?fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=4455201a7269b6550ed19917c24a5f8f`,alt:`Cache-Aside`,\"data-og-width\":`1290`,width:`1290`,\"data-og-height\":`494`,height:`494`,\"data-path\":`hld/fundamentals/primer/images/cache-aside.png`,\"data-optimize\":`true`,\"data-opv\":`3`,srcset:`https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/cache-aside.png?w=280\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=581b860e875f4b5c240b247cec591b05 280w, https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/cache-aside.png?w=560\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=b050712602d577f2800cc7c871216130 560w, https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/cache-aside.png?w=840\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=fb305299519df92a574ecd6f24bbe9e6 840w, https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/cache-aside.png?w=1100\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=b0c4d8f671a1ff0c53cf6754dfe5e0ae 1100w, https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/cache-aside.png?w=1650\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=5f8e65bb446fa0553a2a83851809ec65 1650w, https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/cache-aside.png?w=2500\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=c16862a11b8abf9721cec7d11dc04323 2500w`})}),`\n`,_jsx(_components.p,{children:`The application is responsible for reading and writing from storage. The cache does not interact with storage directly.\nThe application does the following:`}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsx(_components.li,{children:`Look for entry in the cache, resulting in a cache miss`}),`\n`,_jsx(_components.li,{children:`Load entry from the database`}),`\n`,_jsx(_components.li,{children:`Add entry to cache`}),`\n`,_jsx(_components.li,{children:`Return entry`}),`\n`]}),`\n`,_jsx(CodeBlock,{filename:``,numberOfLines:`8`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`8`,children:[_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`def`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` get_user`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`self`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`user_id`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`):`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` user `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` cache.get(`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"user.`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`{0}`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, user_id)`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` if`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` user `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`is`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#569CD6`},children:` None`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`:`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` user `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` db.query(`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"SELECT * FROM users WHERE user_id = `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`{0}`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, user_id)`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` if`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` user `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`is`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:` not`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#569CD6`},children:` None`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`:`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` key `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:` \"user.`}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`{0}`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`.format(user_id)`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` cache.set(key, json.dumps(user))`})}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#C586C0`},children:` return`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` user`})]}),`\n`]})})}),`\n`,_jsxs(_components.p,{children:[_jsx(_components.code,{children:`Memcached`}),` is generally used in this manner.\nSubsequent reads of data added to cache are fast. Cache-aside is also referred to as `,_jsx(_components.code,{children:`lazy loading`}),`. Only requested data\nis cached, which avoids filling up the cache with data that isn’t requested.`]}),`\n`,_jsxs(_components.p,{children:[_jsx(_components.strong,{children:`Disadvantage(s): cache-aside`}),`\nEach cache miss results in three trips, which can cause a noticeable delay.\nData can become stale if it is updated in the database. This issue is mitigated by setting a `,_jsx(_components.code,{children:`time-to-live`}),` (TTL) which\nforces an update of the cache entry, or by using write-through.\nWhen a node fails, it is replaced by a new, empty node, increasing latency.`]}),`\n`,_jsx(Heading,{level:`3`,id:`write-through`,children:`Write-Through`}),`\n`,_jsx(Frame,{children:_jsx(OptimizedImage,{src:`https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/write-through.png?fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=23e7320a6aa02713ede41d6424726d73`,alt:`Write-Through`,\"data-og-width\":`514`,width:`514`,\"data-og-height\":`728`,height:`728`,\"data-path\":`hld/fundamentals/primer/images/write-through.png`,\"data-optimize\":`true`,\"data-opv\":`3`,srcset:`https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/write-through.png?w=280\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=1e8e28ed2d94b4b4b9366602f6080c7a 280w, https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/write-through.png?w=560\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=56451cefa532c9d6800ba824fbe3ebd3 560w, https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/write-through.png?w=840\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=7dcf504dd86cea2de9f5446fc17e8428 840w, https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/write-through.png?w=1100\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=aba8e9d51c3ccbe4411cb4af4a1e5701 1100w, https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/write-through.png?w=1650\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=d4eaeba9642fa74f652cc6e53e08acae 1650w, https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/write-through.png?w=2500\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=f56d8a6f574949c09551f638d5257988 2500w`})}),`\n`,_jsxs(_components.p,{children:[`The application uses the cache as the main data store, `,_jsx(_components.strong,{children:`reading and writing data`}),` to it, while the cache is responsible\nfor reading and writing to the database:`]}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsx(_components.li,{children:`Application adds/updates entry in cache`}),`\n`,_jsx(_components.li,{children:`Cache synchronously writes entry to data store`}),`\n`,_jsx(_components.li,{children:`Return`}),`\n`]}),`\n`,_jsx(CodeBlock,{filename:`Application code`,numberOfLines:`2`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`2`,children:[_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`set_user(`}),_jsx(_components.span,{style:{color:`#0550AE`,\"--shiki-dark\":`#B5CEA8`},children:`12345`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, {`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"foo\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`:`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"bar\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`})`})]}),`\n`]})})}),`\n`,_jsx(CodeBlock,{filename:`Cache code:`,numberOfLines:`4`,language:`python`,children:_jsx(_components.pre,{className:`shiki shiki-themes github-light-default dark-plus`,style:{backgroundColor:`#ffffff`,\"--shiki-dark-bg\":`#0B0C0E`,color:`#1f2328`,\"--shiki-dark\":`#D4D4D4`},language:`python`,children:_jsxs(_components.code,{language:`python`,numberOfLines:`4`,children:[_jsx(_components.span,{className:`line`}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`def`}),_jsx(_components.span,{style:{color:`#8250DF`,\"--shiki-dark\":`#DCDCAA`},children:` set_user`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`(`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`user_id`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, `}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#9CDCFE`},children:`values`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`):`})]}),`\n`,_jsxs(_components.span,{className:`line`,children:[_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` user `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#D4D4D4`},children:`=`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` db.query(`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"UPDATE Users WHERE id = `}),_jsx(_components.span,{style:{color:`#CF222E`,\"--shiki-dark\":`#569CD6`},children:`{0}`}),_jsx(_components.span,{style:{color:`#0A3069`,\"--shiki-dark\":`#CE9178`},children:`\"`}),_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:`, user_id, values)`})]}),`\n`,_jsx(_components.span,{className:`line`,children:_jsx(_components.span,{style:{color:`#1F2328`,\"--shiki-dark\":`#D4D4D4`},children:` cache.set(user_id, user)`})}),`\n`]})})}),`\n`,_jsx(_components.p,{children:`Write-through is a slow overall operation due to the write operation, but subsequent reads of just written data are fast\n. Users are generally more tolerant of latency when updating data than reading data. Data in the cache is not stale.`}),`\n`,_jsx(_components.p,{children:_jsx(_components.strong,{children:`Disadvantage(s): write through`})}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsx(_components.li,{children:`When a new node is created due to failure or scaling, the new node will not cache entries until the entry is updated in\nthe database. Cache-aside in conjunction with write through can mitigate this issue.`}),`\n`,_jsx(_components.li,{children:`Most data written might never be read, which can be minimized with a TTL.`}),`\n`]}),`\n`,_jsx(Heading,{level:`3`,id:`write-behind-write-back`,children:`Write-Behind (write-back)`}),`\n`,_jsx(Frame,{children:_jsx(OptimizedImage,{src:`https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/write-behind.png?fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=11731d1ebbdeeb006efa418a4bf5e42d`,\"data-og-width\":`786`,width:`786`,\"data-og-height\":`728`,height:`728`,\"data-path\":`hld/fundamentals/primer/images/write-behind.png`,\"data-optimize\":`true`,\"data-opv\":`3`,srcset:`https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/write-behind.png?w=280\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=f911078047f940fc2fa8f0b1b9abeae1 280w, https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/write-behind.png?w=560\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=0a191b51bfdf84d89de78a12ce46fee8 560w, https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/write-behind.png?w=840\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=b5a49b0a65c8486716c623de57780eeb 840w, https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/write-behind.png?w=1100\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=85be3caeaeb837b68abab9786ee882f2 1100w, https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/write-behind.png?w=1650\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=1660985ebc03be19e51d371f7e3f7b77 1650w, https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/write-behind.png?w=2500\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=59f9290ea12ca33a2b9f3ac66a555861 2500w`})}),`\n`,_jsx(_components.p,{children:`In write-behind, the application does the following:`}),`\n`,_jsxs(_components.ul,{children:[`\n`,_jsx(_components.li,{children:`Add/update entry in cache`}),`\n`,_jsx(_components.li,{children:`Asynchronously write entry to the data store, improving write performance`}),`\n`]}),`\n`,_jsxs(_components.p,{children:[_jsx(_components.strong,{children:`Disadvantage(s): write-behind`}),`\nThere could be data loss if the cache goes down prior to its contents hitting the data store.\nIt is more complex to implement write-behind than it is to implement cache-aside or write-through.`]}),`\n`,_jsx(Heading,{level:`3`,id:`refresh-ahead`,children:`Refresh-ahead`}),`\n`,_jsx(Frame,{children:_jsx(OptimizedImage,{src:`https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/refresh-ahead.png?fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=f90fe92e793e47d2ca9de5d712a1c93b`,\"data-og-width\":`1290`,width:`1290`,\"data-og-height\":`296`,height:`296`,\"data-path\":`hld/fundamentals/primer/images/refresh-ahead.png`,\"data-optimize\":`true`,\"data-opv\":`3`,srcset:`https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/refresh-ahead.png?w=280\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=566ef5a265a809d18a4a556ced61abfd 280w, https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/refresh-ahead.png?w=560\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=79281370f290e612fa0355f9c0988528 560w, https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/refresh-ahead.png?w=840\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=81950272f85b31ce76fa92bd99e3f359 840w, https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/refresh-ahead.png?w=1100\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=ce42c6817a8ac6ac14a3aa28225669f8 1100w, https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/refresh-ahead.png?w=1650\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=ce46d41a59911ae65de8b1778fd5fc44 1650w, https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/hld/fundamentals/primer/images/refresh-ahead.png?w=2500\u0026fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=b6f3ea31fb540b2fe349b83fe606da9e 2500w`})}),`\n`,_jsx(_components.p,{children:`You can configure the cache to automatically refresh any recently accessed cache entry prior to its expiration.\nRefresh-ahead can result in reduced latency vs read-through if the cache can accurately predict which items are likely to be needed in the future.`}),`\n`,_jsxs(_components.p,{children:[_jsx(_components.strong,{children:`Disadvantage(s): refresh-ahead`}),`\nNot accurately predicting which items are likely to be needed in the future can result in reduced performance than without refresh-ahead.`]}),`\n`,_jsx(Heading,{level:`2`,id:`cache-invalidation-strategies`,children:`Cache Invalidation Strategies`}),`\n`,_jsx(_components.p,{children:`Cache invalidation strategies will go here,`}),`\n`,_jsx(Heading,{level:`2`,id:`disadvantages--cache`,children:`Disadvantage(s): cache`}),`\n`,_jsx(_components.p,{children:`Need to maintain consistency between caches and the source of truth such as the database through cache invalidation.\nCache invalidation is a difficult problem, there is additional complexity associated with when to update the cache.\nNeed to make application changes such as adding Redis or memcached.`})]})}function MDXContent(props={}){const{wrapper:MDXLayout}={..._provideComponents(),...props.components};return MDXLayout?_jsx(MDXLayout,{...props,children:_jsx(_createMdxContent,{...props})}):_createMdxContent(props)}return{default:MDXContent};function _missingMdxReference(id,component){throw new Error(`Expected `+(component?`component`:`object`)+\" `\"+id+\"` to be defined: you likely forgot to import, pass, or provide it.\")}","frontmatter":{},"scope":{"config":{},"pageMetadata":{"title":"Caching Strategies - \"How and When You Cache\"","description":null,"sidebarTitle":"Caching Strategies","href":"/hld/fundamentals/primer/cache/caching-strategies"}}},"mdxSourceWithNoJs":null},"mdxExtracts":{"tableOfContents":[{"title":"Caching Strategies","slug":"caching-strategies","depth":2,"children":[{"title":"Cache-Aside","slug":"cache-aside","depth":3,"children":[]},{"title":"Write-Through","slug":"write-through","depth":3,"children":[]},{"title":"Write-Behind (write-back)","slug":"write-behind-write-back","depth":3,"children":[]},{"title":"Refresh-ahead","slug":"refresh-ahead","depth":3,"children":[]}]},{"title":"Cache Invalidation Strategies","slug":"cache-invalidation-strategies","depth":2,"children":[]},{"title":"Disadvantage(s): cache","slug":"disadvantages--cache","depth":2,"children":[]}],"codeExamples":{}},"jsLinks":[],"subdomain":"akshanshgusain","siteName":"Mint Starter Kit","title":"Caching Strategies - \"How and When You Cache\" - Mint Starter Kit","contextualStarterQuestions":[],"navMaps":{"firstHrefInVersion":{},"firstHrefInLanguage":{}},"lastModified":"2025-10-27T11:22:10.569Z","originalFileLocation":"hld/fundamentals/primer/cache/caching-strategies.mdx"},"headHtml":"\u003cscript data-mint-head>(function(){var d=document.documentElement,p=null;if(!p){try{p=localStorage.getItem(\"isDarkMode\")}catch(e){}if(p==='true')p='dark';else if(p==='false')p='light';if(p!=='light'\u0026\u0026p!=='dark'\u0026\u0026p!=='system')p=\"system\"}var r=p==='system'?(matchMedia(\"(prefers-color-scheme: dark)\").matches?'dark':'light'):p;d.classList.remove(r==='dark'?'light':'dark');d.classList.add(r);d.setAttribute(\"data-theme-preference\",p)})();\u003c/script>\u003cmeta data-mint-head name=\"application-name\" content=\"Mint Starter Kit\">\u003cmeta data-mint-head name=\"generator\" content=\"Mintlify\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/mintlify-assets/_mintlify/favicons/akshanshgusain/JS_E2GliXwwTj604/_generated/favicon/android-chrome-192x192.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"apple-touch-icon\" type=\"image/png\" sizes=\"180x180\" href=\"/mintlify-assets/_mintlify/favicons/akshanshgusain/JS_E2GliXwwTj604/_generated/favicon/apple-touch-icon.png\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/mintlify-assets/_mintlify/favicons/akshanshgusain/JS_E2GliXwwTj604/_generated/favicon/favicon-16x16.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/mintlify-assets/_mintlify/favicons/akshanshgusain/JS_E2GliXwwTj604/_generated/favicon/favicon-32x32.png\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/mintlify-assets/_mintlify/favicons/akshanshgusain/JS_E2GliXwwTj604/_generated/favicon/favicon.ico\" media=\"(prefers-color-scheme: light)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"192x192\" href=\"/mintlify-assets/_mintlify/favicons/akshanshgusain/JS_E2GliXwwTj604/_generated/favicon-dark/android-chrome-192x192.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"16x16\" href=\"/mintlify-assets/_mintlify/favicons/akshanshgusain/JS_E2GliXwwTj604/_generated/favicon-dark/favicon-16x16.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"icon\" type=\"image/png\" sizes=\"32x32\" href=\"/mintlify-assets/_mintlify/favicons/akshanshgusain/JS_E2GliXwwTj604/_generated/favicon-dark/favicon-32x32.png\" media=\"(prefers-color-scheme: dark)\">\u003clink data-mint-head rel=\"shortcut icon\" type=\"image/x-icon\" href=\"/mintlify-assets/_mintlify/favicons/akshanshgusain/JS_E2GliXwwTj604/_generated/favicon-dark/favicon.ico\" media=\"(prefers-color-scheme: dark)\">\u003cmeta data-mint-head property=\"og:title\" content=\"Caching Strategies - \u0026quot;How and When You Cache\u0026quot; - Mint Starter Kit\">\u003cmeta data-mint-head property=\"og:site_name\" content=\"Mint Starter Kit\">\u003cmeta data-mint-head property=\"og:url\" content=\"https://akshanshgusain.mintlify.app/hld/fundamentals/primer/cache/caching-strategies\">\u003cmeta data-mint-head property=\"og:type\" content=\"website\">\u003cmeta data-mint-head property=\"og:image\" content=\"https://akshanshgusain.mintlify.site/_mintlify/api/og/v2?division=Cache\u0026amp;title=Caching+Strategies+-+%22How+and+When+You+Cache%22\u0026amp;theme=77ece92d591d6b1fdf64d1e8\u0026amp;signature=c8df5c9994ed36c08ff398450572cb0b4a6e5ff98850592ebff52c9ff83dafbf\">\u003cmeta data-mint-head property=\"og:image:width\" content=\"1200\">\u003cmeta data-mint-head property=\"og:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"twitter:card\" content=\"summary_large_image\">\u003cmeta data-mint-head name=\"twitter:title\" content=\"Caching Strategies - \u0026quot;How and When You Cache\u0026quot; - Mint Starter Kit\">\u003cmeta data-mint-head name=\"twitter:image\" content=\"https://akshanshgusain.mintlify.site/_mintlify/api/og/v2?division=Cache\u0026amp;title=Caching+Strategies+-+%22How+and+When+You+Cache%22\u0026amp;theme=77ece92d591d6b1fdf64d1e8\u0026amp;signature=c8df5c9994ed36c08ff398450572cb0b4a6e5ff98850592ebff52c9ff83dafbf\">\u003cmeta data-mint-head name=\"twitter:image:width\" content=\"1200\">\u003cmeta data-mint-head name=\"twitter:image:height\" content=\"630\">\u003cmeta data-mint-head name=\"msapplication-config\" content=\"/mintlify-assets/_mintlify/favicons/akshanshgusain/JS_E2GliXwwTj604/_generated/favicon/browserconfig.xml\">\u003cmeta data-mint-head name=\"apple-mobile-web-app-title\" content=\"Mint Starter Kit\">\u003cmeta data-mint-head name=\"msapplication-TileColor\" content=\"#16A34A\">\u003clink data-mint-head rel=\"canonical\" href=\"https://akshanshgusain.mintlify.app/hld/fundamentals/primer/cache/caching-strategies\">\u003clink data-mint-head rel=\"alternate\" type=\"application/xml\" href=\"/sitemap.xml\">\u003clink data-mint-head rel=\"alternate\" type=\"text/markdown\" href=\"/hld/fundamentals/primer/cache/caching-strategies.md\">\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https://akshanshgusain.mintlify.app/#organization\",\"name\":\"Mint Starter Kit\",\"url\":\"https://akshanshgusain.mintlify.app\",\"logo\":{\"@type\":\"ImageObject\",\"url\":\"https://mintcdn.com/akshanshgusain/U9KiYWNLHNn_ZtUv/logo/light.svg?fit=max\u0026auto=format\u0026n=U9KiYWNLHNn_ZtUv\u0026q=85\u0026s=07a697a3d04ef4dc936a2014794adeae\"}},{\"@type\":\"WebSite\",\"@id\":\"https://akshanshgusain.mintlify.app#website\",\"name\":\"Mint Starter Kit\",\"url\":\"https://akshanshgusain.mintlify.app\",\"publisher\":{\"@id\":\"https://akshanshgusain.mintlify.app/#organization\"}},{\"@type\":\"WebPage\",\"@id\":\"https://akshanshgusain.mintlify.app/hld/fundamentals/primer/cache/caching-strategies#webpage\",\"url\":\"https://akshanshgusain.mintlify.app/hld/fundamentals/primer/cache/caching-strategies\",\"name\":\"Caching Strategies - \\\"How and When You Cache\\\"\",\"dateModified\":\"2025-10-27T11:22:10.569Z\",\"isPartOf\":{\"@id\":\"https://akshanshgusain.mintlify.app#website\"},\"breadcrumb\":{\"@id\":\"https://akshanshgusain.mintlify.app/hld/fundamentals/primer/cache/caching-strategies#breadcrumb\"}},{\"@type\":\"BreadcrumbList\",\"@id\":\"https://akshanshgusain.mintlify.app/hld/fundamentals/primer/cache/caching-strategies#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"HLD Fundamentals\",\"item\":\"https://akshanshgusain.mintlify.app/hld/fundamentals/primer/scalability\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Primer\",\"item\":\"https://akshanshgusain.mintlify.app/hld/fundamentals/primer/scalability\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Cache\",\"item\":\"https://akshanshgusain.mintlify.app/hld/fundamentals/primer/cache/intro\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Caching Strategies - \\\"How and When You Cache\\\"\",\"item\":\"https://akshanshgusain.mintlify.app/hld/fundamentals/primer/cache/caching-strategies\"}]},{\"@type\":[\"Article\",\"TechArticle\"],\"@id\":\"https://akshanshgusain.mintlify.app/hld/fundamentals/primer/cache/caching-strategies#article\",\"headline\":\"Caching Strategies - \\\"How and When You Cache\\\"\",\"name\":\"Caching Strategies - \\\"How and When You Cache\\\"\",\"url\":\"https://akshanshgusain.mintlify.app/hld/fundamentals/primer/cache/caching-strategies\",\"mainEntityOfPage\":{\"@id\":\"https://akshanshgusain.mintlify.app/hld/fundamentals/primer/cache/caching-strategies#webpage\"},\"dateModified\":\"2025-10-27T11:22:10.569Z\",\"publisher\":{\"@id\":\"https://akshanshgusain.mintlify.app/#organization\"},\"isPartOf\":{\"@id\":\"https://akshanshgusain.mintlify.app#website\"}}]}\u003c/script>\u003cscript data-mint-head type=\"application/ld+json\">{\"@context\":\"https://schema.org\",\"@type\":\"WebSite\",\"name\":\"Mint Starter Kit\",\"publisher\":{\"@type\":\"Organization\",\"name\":\"Mint Starter Kit\"},\"provider\":{\"@type\":\"Organization\",\"name\":\"Mintlify\",\"url\":\"https://mintlify.com\"}}\u003c/script>","agentPointerHref":"/hld/fundamentals/primer/cache/caching-strategies.md","siteName":"Mint Starter Kit","nav":{"global":{},"tabs":[{"tab":"Guides","pages":[{"title":"Guides","href":""}]},{"tab":"DSA","pages":[{"title":"DSA","href":"/dsa/fundamentals/computational-complexity"}]},{"tab":"Low Level Design","pages":[{"title":"Low Level Design","href":"/lld/fundamentals/oop/encapsulation"}]},{"tab":"High Level Design","groups":[{"group":"HLD Fundamentals","pages":[{"group":"Primer","pages":[{"title":"Scalability in Distributes Systems","sidebarTitle":"Scalability","href":"/hld/fundamentals/primer/scalability"},{"title":"Fallacies of Distributes Systems","sidebarTitle":"Fallacies","href":"/hld/fundamentals/primer/fallacies"},{"title":"CAP Theorem","sidebarTitle":"CAP Theorem","href":"/hld/fundamentals/primer/cap"},{"title":"Consistency in Distributes Systems","sidebarTitle":"Consistency","href":"/hld/fundamentals/primer/consistency-patterns"},{"title":"Availability in Distributes Systems","sidebarTitle":"Availability","href":"/hld/fundamentals/primer/availability-patterns"},{"title":"Load Balancer","sidebarTitle":"Load Balance","href":"/hld/fundamentals/primer/load-balancer"},{"title":"API Gateway","sidebarTitle":"API Gateway","href":"/hld/fundamentals/primer/api-gateway"},{"title":"Reverse proxy","sidebarTitle":"Reverse proxy","href":"/hld/fundamentals/primer/reverse-proxy"},{"title":"Domain Name Servers","sidebarTitle":"DNS","href":"/hld/fundamentals/primer/dns"},{"title":"Content Delivery Network","sidebarTitle":"CDN","href":"/hld/fundamentals/primer/cdn"},{"title":"What is Serverless?","sidebarTitle":"Serverless","href":"/hld/fundamentals/primer/serverless"},{"title":"Service Discovery","sidebarTitle":"Service Discovery","href":"/hld/fundamentals/primer/service-discovery"},{"title":"Calc","href":"/hld/fundamentals/primer/calc"},{"group":"Database","pages":[{"title":"Introduction","sidebarTitle":"Introduction","href":"/hld/fundamentals/primer/db/intro"},{"group":"SQL","pages":[{"title":null,"href":"/hld/fundamentals/primer/db/sql/acid"},{"title":"Scale a relational database","sidebarTitle":"Scaling","href":"/hld/fundamentals/primer/db/sql/scale"},{"title":"Scale a relational database","sidebarTitle":"Replication","href":"/hld/fundamentals/primer/db/sql/replication"},{"title":"Automatic Conflict Resolution","sidebarTitle":"Automatic Conflict Resolution","href":"/hld/fundamentals/primer/db/sql/conflict-resolution"}]},{"group":"NoSQL","pages":[{"title":"NoSQL","sidebarTitle":"NoSQL","href":"/hld/fundamentals/primer/db/nosql/nosql"},{"title":"Key-value store","sidebarTitle":"Key-value store","href":"/hld/fundamentals/primer/db/nosql/key-value"},{"title":"Document store","sidebarTitle":"Document store","href":"/hld/fundamentals/primer/db/nosql/document"},{"title":"Wide Column store","sidebarTitle":"Wide Column store","href":"/hld/fundamentals/primer/db/nosql/wide-column"},{"title":"Graph Database","sidebarTitle":"Graph Database","href":"/hld/fundamentals/primer/db/nosql/graph"},{"title":"Distributed NoSQL, Availability-Partitioning","sidebarTitle":"AP-NoSQL","href":"/hld/fundamentals/primer/db/nosql/ap-dbs"},{"title":"Distributed NoSQL, Consistency-Partitioning","sidebarTitle":"CP-NoSQL","href":"/hld/fundamentals/primer/db/nosql/cp-dbs"},{"title":"AWS DynamoDB","sidebarTitle":"DynamoDB","href":"/hld/fundamentals/primer/db/nosql/aws-dynamo-db"}]},{"title":"Comparison Between SQL and NoSQL DataStores","sidebarTitle":"SQL vs NoSQL","href":"/hld/fundamentals/primer/db/sql-vs-nosql"}]},{"group":"Cache","pages":[{"title":"Introduction to Caches","description":null,"sidebarTitle":"Introduction","href":"/hld/fundamentals/primer/cache/intro"},{"title":"Caching Strategies - \"How and When You Cache\"","sidebarTitle":"Caching Strategies","href":"/hld/fundamentals/primer/cache/caching-strategies"},{"title":"Cache Invalidation Strategies – \"How You Keep Cache Up-to-date\"","description":"Cache invalidation strategies focus on removing or updating stale data from the cache when the source of truth (e.g., DB) changes:","sidebarTitle":"Cache Invalidation Strategies","href":"/hld/fundamentals/primer/cache/cache-invalidation"},{"title":"Cache Eviction Policies – \"How You Decide What to Remove When Cache Is Full\"","sidebarTitle":"Cache Eviction Strategies","href":"/hld/fundamentals/primer/cache/cache-eviction"},{"title":"Redis In-Depth","sidebarTitle":"Redis","href":"/hld/fundamentals/primer/cache/redis"}]},{"group":"Queue","pages":[{"title":"Message Queues","sidebarTitle":"Message Queues","href":"/hld/fundamentals/primer/queue/intro"},{"title":"Message Queues vs Pub-Subs","sidebarTitle":"Queues vs Pub-Subs","href":"/hld/fundamentals/primer/queue/queue-v-pubsub"},{"title":"Comparison Between SQL and NoSQL DataStores","sidebarTitle":"BackPressure","href":"/hld/fundamentals/primer/queue/back-pressure"},{"title":"Apache Kafka","sidebarTitle":"Kafka","href":"/hld/fundamentals/primer/queue/apache-kafka"}]},{"group":"Network and Protocols","pages":[{"title":"Network and Communications","sidebarTitle":"Introduction","href":"/hld/fundamentals/primer/network/intro"},{"group":"Layer 4 Protocols","pages":[{"title":"Transport Layer Protocols","sidebarTitle":"TCP","href":"/hld/fundamentals/primer/network/layer-4-protocols/tcp"}]},{"group":"Layer 7 Protocols","pages":[{"title":"Application Layer Protocols","sidebarTitle":"Introduction","href":"/hld/fundamentals/primer/network/layer-7-protocols/intro"},{"title":"Application Layer Protocols","sidebarTitle":"HTTP","href":"/hld/fundamentals/primer/network/layer-7-protocols/http"},{"title":"Application Layer Protocols","sidebarTitle":"RPC","href":"/hld/fundamentals/primer/network/layer-7-protocols/rpc"},{"title":"Application Layer Protocols","sidebarTitle":"REST","href":"/hld/fundamentals/primer/network/layer-7-protocols/rest"}]}]}]},{"title":"Scalable System Design Patterns","sidebarTitle":"Scalable System Design Patterns","href":"/hld/fundamentals/scalability-patterns"}]},{"group":"HLD Case Studies","pages":[{"title":"Framework for System Design Interviews","sidebarTitle":"Framework","href":"/hld/case-studies/framework"},{"title":"Back of the Envelope Calculation","sidebarTitle":"Estimation Calculations","href":"/hld/case-studies/calc"},{"title":"Design Pastebin","sidebarTitle":"Design Pastebin","href":"/hld/case-studies/pastebin"},{"title":"Consistent Hashing","sidebarTitle":"Consistent Hashing","href":"/hld/case-studies/consistent-hashing"}]}]},{"tab":"Behavioural","pages":[{"title":"Behavioural","href":"/behavioural/competency-interviews"}]}]},"basePath":"","title":"Caching Strategies - \"How and When You Cache\" - Mint Starter Kit","deploymentId":"dpl_EtLmMeZHkKGqAjV1DiX4ZXCriDj3","buildId":"dpl_EtLmMeZHkKGqAjV1DiX4ZXCriDj3","dynamics":["07c022d89b","a74be98ba7"],"chunks":["Frame-CCOo5IeD.js","DynamicMarkdown-Czhs88Hc.js","zoom-image-lightbox-BA_UUN68.js","lightbox-handle-r17bVUz3.js"]}