Chrome's MemoryCache and DiskCache
When debugging frontend requests in Chrome DevTools, you’ll notice that caching is divided into two types: MemoryCache and DiskCache. Here’s a summary based on my understanding of the differences between them and how Chrome selects the caching medium.
Concepts
The difference is simple - you can tell from the names alone. MemoryCache stores and retrieves from memory, making it fast but with a short lifecycle. DiskCache, on the other hand, uses disk storage, which is slower for read/write operations but has a relatively longer lifecycle and provides persistent storage.
Cache Medium Selection
I couldn’t find official authoritative documentation on how Chrome selects between these two caching mediums, so I conducted some tests and reached the following conclusions:
- When a user first loads a webpage, if resources are not marked as non-cacheable, Chrome generally caches resources to disk persistently.
- When a user revisits the same URL resources, regardless of whether it’s strong caching or conditional caching, once Chrome determines to load from cache, it copies resources that are early in the request loading order from disk to memory, then loads from memory. Resources later in the queue are loaded directly from disk due to limited memory space.
- From this, we can conclude that cache medium selection is more about the resource’s loading order - prioritizing urgent needs - rather than the resource type itself.
Final Thoughts
Chrome certainly has a lot of intricacies. Paying attention to these details helps us better understand Chrome and the web in general. Understanding the differences between MemoryCache and DiskCache can help developers optimize resource loading and improve website performance.