Back to Top

Implementing Cache for Collection Query Results in Magento 2

Updated 17 November 2025

Improving performance is a top priority for any Magento 2 store, especially when handling heavy database queries.

Collections, whether for products, orders, or custom entities, can become expensive to load repeatedly.

One of the most effective ways to improve performance is to cache the results of collection queries, so Magento doesn’t hit the database every time.

Looking to improve your store’s speed and overall performance? Check out our Magento 2 Speed & Optimization services.

In this guide, we’ll walk through why caching is important, how Magento handles caching internally, and how you can implement collection-level caching safely and efficiently.

Start your headless eCommerce
now.
Find out More

Why Cache Collection Queries?

Magento collections often include:

  • Multiple JOIN operations
  • Filters, grouping, or sorting
  • EAV attribute loading
  • Large datasets (products, orders, customers)

Without caching, these queries run on every request, which:

  • Increases database load
  • Slows down page generation
  • Impacts overall site performance

By caching collection results, you reduce repetitive processing and deliver faster responses to users—and to APIs.

How Magento Handles Collection Caching

Magento 2 provides a built-in mechanism to cache database fetch operations. This is done using the FetchStrategy class.

The default strategy does not cache data, but Magento includes a ready-to-use caching strategy here:

Magento\Framework\Data\Collection\Db\FetchStrategy\Cache

When enabled, this strategy stores collection results in Magento’s cache and returns them instantly on subsequent loads.

How to Enable Cache for a Custom Collection

You can apply collection caching using di.xml by specifying the FetchStrategy class for your collection.

Example: Enable Caching for a Custom Collection

<type name="Vendor\Module\Model\ResourceModel\Custom\Collection">
    <arguments>
        <argument name="fetchStrategy" xsi:type="object">
            Magento\Framework\Data\Collection\Db\FetchStrategy\Cache
        </argument>
    </arguments>
</type>

Once added, Magento will automatically start caching the results of this collection.

But what if you want to clear the cached collection data using custom cache tags?

By default, Magento stores collection results under the generic COLLECTION_DATA tag. If you need to associate your own tags for more granular control, you can do so.
Here’s an example:

<virtualType name="CustomCollectionCacheFetchStrategy" type="Magento\Framework\Data\Collection\Db\FetchStrategy\Cache">
    <arguments>
        <argument name="cacheTags" xsi:type="array">
            <item name="custom" xsi:type="string">custom_collection</item>
        </argument>
    </arguments>
</virtualType>
<type name="Vendor\Module\Model\ResourceModel\Custom\Collection">
    <arguments>
        <argument name="fetchStrategy" xsi:type="object">CustomCollectionCacheFetchStrategy</argument>
    </arguments>
</type>

After enabling caching for Collection, Magento applies this cache globally.

That means every part of the system that loads this collection—including UI Components—will automatically use the cached data.

To prevent this, we create a virtual class for caching so only the targeted context uses it, while the UI Component uses the original collection.

Below is an example showing how to enable caching for Collection exclusively within the module by updating di.xml:

<virtualType name="CustomCollectionCacheFetchStrategy" type="Magento\Framework\Data\Collection\Db\FetchStrategy\Cache">
    <arguments>
        <argument name="cacheTags" xsi:type="array">
            <item name="custom" xsi:type="string">custom_collection</item>
        </argument>
    </arguments>
</virtualType>
<virtualType name="VirtualCustomCollection" type="Vendor\Module\Model\ResourceModel\Custom\Collection">
    <arguments>
        <argument name="fetchStrategy" xsi:type="object">CustomCollectionCacheFetchStrategy</argument>
    </arguments>
</virtualType>
<virtualType name="VirtualTypeCustomCollectionFactory" type="Vendor\Module\Model\ResourceModel\Custom\CollectionFactory">
    <arguments>
        <argument name="instanceName" xsi:type="string">VirtualCustomCollection</argument>
    </arguments>
</virtualType>
<type name="Vendor\Module\Block\Custom">
    <arguments>
        <argument name="customCollectionFactory" xsi:type="object">VirtualTypeCustomCollectionFactory</argument>
    </arguments>
</type>

This way, you can have a context-specific cache for your custom collection.

If you want to create a custom cache tag in Magento 2, please refer to our blog:
How to create a custom cache type in Magento

Please check the image links for the difference between before and after implementing cache collection.
Before:

before-collection-cache

After:

after-collection-cache

Hope this makes the caching workflow easier to implement and adapt to your needs.
Happy Coding 🙂

If you need any technical assistance, feel free to reach out to us at support@webkul.com.

You can also explore a wide range of solutions to enhance your Magento 2 store by visiting our Magento 2 extensions page.

For expert guidance or custom feature development, you may hire our Magento 2 developers to support your project.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home