<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/cache-stop-asking-the-database-the-same-question-qjeabvnp4" -->

---
title: Cache: Stop Asking the Database the Same Question
description: A practical introduction to caching concepts for developers, covering cache hits and misses, TTL (Time To Live), and cache invalidation strategies. Uses a...
canonical: https://daily.dev/posts/cache-stop-asking-the-database-the-same-question-qjeabvnp4
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Cache: Stop Asking the Database the Same Question | daily.dev
og:description: A practical introduction to caching concepts for developers, covering cache hits and misses, TTL (Time To Live), and cache invalidation strategies. Uses a...
og:url: https://daily.dev/posts/cache-stop-asking-the-database-the-same-question-qjeabvnp4
og:image: https://api.daily.dev/og/posts/qjEabvNP4.png
og:image:alt: Cache: Stop Asking the Database the Same Question
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Cache: Stop Asking the Database the Same Question

**[Medium](https://daily.dev/sources/medium_js)** · 10 min read · 1 upvotes · 1 comments

## Summary

A practical introduction to caching concepts for developers, covering cache hits and misses, TTL (Time To Live), and cache invalidation strategies. Uses a relatable analogy to explain why caching exists, then walks through what data is safe to cache versus what requires careful thought (permissions, balances, tokens). Includes a Go CLI demo with SQLite showing all cache states: hit, miss, expired, stale-after-update, and cleared-on-write, with concrete response time comparisons.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://medium.com/@sara.pilav/cache-stop-asking-the-database-the-same-question-20a85f717918>

## Questions this post answers

### What are the main strategies for invalidating a cache when the underlying database changes?

Four common approaches exist: TTL expiry, where cached data simply times out and refreshes after a set duration; clear on write, where updating the database also clears the related cache entry; update in place, where the new value is written to both database and cache simultaneously; and versioned cache keys, where a version identifier changes so old cached values become unreachable rather than being served stale. The right choice depends on how much staleness is tolerable and how frequently the data changes.

_daily.dev surfaces practical caching patterns for developers weighing invalidation trade-offs in real systems._

### Why is caching user permissions or access roles risky?

Caching permissions for too long creates a security gap rather than just a performance issue: if a user's access is revoked but the cache still says they're allowed in, that stale entry becomes an active security hole. Data like payment status, live inventory, and one-time tokens carries similar risk and needs a very short TTL or explicit invalidation tied to the moment access changes, not just a timeout.

_Developers hardening auth logic follow caching and security trade-offs like this one through daily.dev._

### What is the difference between a cache hit and a cache miss?

A cache hit means the requested value is already stored in cache, so the response is served quickly without touching the database at all. A cache miss means the cache has nothing useful, so the application falls back to the database, retrieves the data, and typically stores it in cache so a subsequent request becomes a hit. In a demo with a 200ms artificial database delay, a hit returned in under 1ms versus roughly 202ms for a miss.

_daily.dev helps developers comparing caching approaches keep tabs on real-world hit and miss trade-offs._

## Similar posts on daily.dev

- [Caching in Distributed Systems - Part III](https://daily.dev/posts/caching-in-distributed-systems---part-iii-qcazxcqub) · The Polymathic Engineer · 18 upvotes · 0 comments
- [Semantic Caching for LLMs: TTLs, Confidence, and Cache Safety](https://daily.dev/posts/semantic-caching-for-llms-ttls-confidence-and-cache-safety-w9mohd4go) · PyImageSearch · 0 upvotes · 0 comments
- [Cache consistency: strategies to keep data fresh](https://daily.dev/posts/cache-consistency-strategies-to-keep-data-fresh-o2gww6sna) · Redis · 6 upvotes · 0 comments
- [Caching isn’t hard. Some data is hard to cache](https://daily.dev/posts/caching-isn-t-hard-some-data-is-hard-to-cache-olerdiegk) · ITNEXT · 3 upvotes · 0 comments
- [Caching and Latency \[Book Excerpt\]](https://daily.dev/posts/caching-and-latency-book-excerpt--xatnprmvu) · P99 Conf · 6 upvotes · 0 comments

---

Tags: [#backend](https://daily.dev/tags/backend), [#golang](https://daily.dev/tags/golang), [#redis](https://daily.dev/tags/redis)

[View this post on daily.dev](https://daily.dev/posts/cache-stop-asking-the-database-the-same-question-qjeabvnp4)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Cache: Stop Asking the Database the Same Question","url":"https://daily.dev/posts/cache-stop-asking-the-database-the-same-question-qjeabvnp4","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/cache-stop-asking-the-database-the-same-question-qjeabvnp4"},"datePublished":"2026-07-19T19:35:12.382Z","dateModified":"2026-09-14T06:12:50.173Z","description":"A practical introduction to caching concepts for developers, covering cache hits and misses, TTL (Time To Live), and cache invalidation strategies. Uses a...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/35ec9ceaa2350c3d63c2746e4929a64b?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/35ec9ceaa2350c3d63c2746e4929a64b?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Medium","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Medium","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/medium","url":"https://daily.dev/sources/medium_js"},"commentCount":1,"discussionUrl":"https://daily.dev/posts/cache-stop-asking-the-database-the-same-question-qjeabvnp4","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":1}],"keywords":"backend,golang,redis","timeRequired":"PT10M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Medium","item":"https://daily.dev/sources/medium_js"},{"@type":"ListItem","position":3,"name":"Cache: Stop Asking the Database the Same Question"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/cache-stop-asking-the-database-the-same-question-qjeabvnp4#faq","mainEntity":[{"@type":"Question","name":"What are the main strategies for invalidating a cache when the underlying database changes?","acceptedAnswer":{"@type":"Answer","text":"Four common approaches exist: TTL expiry, where cached data simply times out and refreshes after a set duration; clear on write, where updating the database also clears the related cache entry; update in place, where the new value is written to both database and cache simultaneously; and versioned cache keys, where a version identifier changes so old cached values become unreachable rather than being served stale. The right choice depends on how much staleness is tolerable and how frequently the data changes. daily.dev surfaces practical caching patterns for developers weighing invalidation trade-offs in real systems."}},{"@type":"Question","name":"Why is caching user permissions or access roles risky?","acceptedAnswer":{"@type":"Answer","text":"Caching permissions for too long creates a security gap rather than just a performance issue: if a user's access is revoked but the cache still says they're allowed in, that stale entry becomes an active security hole. Data like payment status, live inventory, and one-time tokens carries similar risk and needs a very short TTL or explicit invalidation tied to the moment access changes, not just a timeout. Developers hardening auth logic follow caching and security trade-offs like this one through daily.dev."}},{"@type":"Question","name":"What is the difference between a cache hit and a cache miss?","acceptedAnswer":{"@type":"Answer","text":"A cache hit means the requested value is already stored in cache, so the response is served quickly without touching the database at all. A cache miss means the cache has nothing useful, so the application falls back to the database, retrieves the data, and typically stores it in cache so a subsequent request becomes a hit. In a demo with a 200ms artificial database delay, a hit returned in under 1ms versus roughly 202ms for a miss. daily.dev helps developers comparing caching approaches keep tabs on real-world hit and miss trade-offs."}}]}
```

