# Cache

URL: https://softwaredictionary.org/terms/cache
Category: Backend & APIs
Last updated: 2026-09-30
In Turkish: Önbellek
Pronunciation: KASH

In short: A cache is a fast, temporary storage layer that keeps copies of frequently used data so later requests can be served quickly without repeating slow work.

## What is a cache?

A cache stores the result of slow or expensive work, such as a database query, an API call, or a rendered page, so the next request for the same data can be answered almost instantly. Caches usually keep data in memory, which is much faster to read than a disk or a remote service. When the requested data is found in the cache it is called a cache hit; when it is missing, it is a cache miss, and the system fetches the data from its original source.

Caching happens at many levels: CPU caches inside the processor, the browser cache on your device, CDNs that store files close to users, and application caches such as Redis or Memcached that sit in front of a database. A common pattern is cache-aside, where the application checks the cache first and, on a miss, reads from the database and saves the result in the cache for next time.

An everyday analogy is keeping the spices you use most on the kitchen counter instead of walking to the pantry every time. Because counter space is limited, caches remove old entries using an eviction policy such as least recently used (LRU), and entries often expire after a set time to live (TTL).

The hardest part of caching is invalidation: making sure the cache stops serving stale data after the original changes. Developers handle this with short TTLs, by deleting cache entries whenever the data is updated, or by accepting that some data may be slightly out of date. A cache is also not a database, because its contents can disappear at any time, so the real data must always live somewhere durable.

## Key takeaways

- A cache keeps copies of data in fast storage to avoid repeating slow work.
- A cache hit returns stored data; a cache miss falls back to the original source.
- Caches exist in CPUs, browsers, CDNs, and applications, for example Redis.
- Entries are removed by eviction policies such as LRU or when their TTL expires.
- Invalidating stale data is the main challenge of caching.

## Example: The cache-aside pattern with Redis

```javascript
// Check the cache first, and fall back to the database on a miss
async function getProduct(id) {
  const key = `product:${id}`;
  const cached = await redis.get(key);
  if (cached) return JSON.parse(cached); // cache hit

  const product = await db.products.findById(id); // cache miss
  await redis.set(key, JSON.stringify(product), { EX: 300 }); // keep for 5 minutes
  return product;
}
```

## Frequently asked questions

**What is the difference between a cache and a database?**

A database is the durable source of truth for your data, while a cache holds temporary copies for speed. Cached data can be evicted or lost at any time, so it must always be possible to rebuild it from the database.

**What does cache invalidation mean?**

Cache invalidation is removing or updating cached entries when the original data changes, so users don't see stale results. Common approaches are expiring entries with a TTL or deleting the cache key whenever the data is written.

**What is a TTL in caching?**

TTL, or time to live, is how long a cache entry stays valid before it expires and must be fetched again. Short TTLs keep data fresher, while long TTLs produce more cache hits.

---

Software Dictionary: https://softwaredictionary.org/ · https://softwaredictionary.org/llms.txt
