Cache
- 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.
At a glance
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
// 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;
}Readers ask
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.
See also
- DatabaseDatabases, p. 6A database is an organized collection of data stored on a computer, managed by software that lets applications save, search, and update it efficiently.
- CDNDevOps & Cloud, p. 7A CDN is a network of servers spread around the world that stores copies of website content and delivers it to each user from the nearest location.
- HTTPWeb Development, p. 19HTTP is the protocol that browsers, apps, and servers use to exchange web pages and data through a simple cycle of requests and responses.
- NoSQLDatabases, p. 29NoSQL is a family of databases that store data in models other than relational tables, such as documents, key-value pairs, wide columns, or graphs.
- Reverse ProxyDevOps & Cloud, p. 44A reverse proxy is a server that sits in front of web servers, accepts client requests on their behalf, and forwards each request to the right backend server.
- APIBackend & APIs, p. 2An API is a set of rules that lets one piece of software request data or actions from another in a predictable, documented way.
- RedisDatabases, p. 37Redis is an in-memory key-value store that reads and writes in well under a millisecond, which makes it a popular cache, session store and message broker.
- LRU CacheData Structures, p. 23An LRU (least recently used) cache holds a fixed number of items and, when full, evicts the one unused the longest, betting that recent data will be reused.
Spotted a mistake or something missing on this page?Suggest an edit