Understanding the Product Cache

Introduction

When there are a lot of products to display, the shopfront loads very slowly (~30 s for Stroudco with 386 products). Customers tend to leave when they get frustrated waiting for websites to load, so to make this faster products are ‘cached’.

What is Caching?

Caching is the storage of a set of data for a specific aim. Commonly the aim is to enable requests to read the data to be served faster. A cache is generally created prior to the data being needed such that the data can be retrieved more efficiently when required.

In our case we are creating a set of product data for a specific order cycle that we can read quickly to allow the shopfront to load.

How does the Product Cache Work?

Due to the complexity of relationships possible within OFN between suppliers, products, order cycle and customers creating the product list as shown on the shopfront requires a large number of database queries. As order cycles increase in complexity the time taken to load a shopfront increases exponentially.

The Product Cache allows for all of this work to be done in the background so that when a customer wants to load a shopfront the hard work is already done. The Product Cache creates a JSON file with all of the product data for a particular Order Cycle. There is a different cache for every order cycle. When the shopfront loads the product list is read directly from this fast and organised JSON file rather than from the database.

When a change is made to a product in an order cycle a ‘Cache Refresh’ is triggered. The cache refresh will update the specific parts of the JSON file for the particular Order Cycle in which the change was made. This does cause a delay to updates appearing in the shopfront, but the cache is designed for the delay to be short such that data integrity is maintained.

You can check if the cache is in sync and up to date using the Cache Status Display Page. In super-admin you can access this page from Configuration → Caching. If the cache for the specific Order Cycle in question is reading ‘OK’ you can be confident that the cache is storing accurate information. In this case the cause of an issue is not likely to be the cache, but another error in the code. The checks performed on this page are also performed by a background task that compares a fresh cache to the existing cache every hour, and reports any errors to Bugsnag.

Answers to Questions

1) The products in the shopfront have not updated after a few minutes. Why might this be?
This might have occurred for a number of reasons:

  • Delayed Job might have stopped running. If this is the case your system administrator will need to restart it. In 1.7 an API call is being added to allow monitoring systems to be notified if this happens, which in turn can notify the appropriate technical folks.

  • The changes might have affected a large number of large order cycles. Currently cache updates are handled by a single process meaning that a change that affects a large number of order cycles (and hence a large number of caches) will result in a queue of cache update jobs. Allow for 30 seconds per order cycle, though this is a rough estimate based on developer testing. If you know that your update will affect a large number of order cycles it might take longer for the updates to appear in the shopfront. If this is a regular occurrence the instance tech team may need to consider spinning up additional Delayed Job workers to share the work of refreshing the cache.

2) The Caching Status page is showing the cache is continuously out of sync. Why might this be?
The most likely reason the Cache Status for an order cycle will continuously be out of sync is because a change was made to some product or configuration that did not trigger a cache update when it should have.

If this happens report an issue detailing the exact change that did not trigger the cache update so that developers can confirm if this is something that needs to also trigger an update. Out of sync cache will also create errors reported in Bugsnag. This should provide additional information to developers.

3) Is there a way to manually refresh the cache?
System administrators can manually refresh the cache using a rake task. This should not be required unless the Cache Status page is showing perpetual inconsistencies, probably caused by a change not updating the cache. The task is:

rake openfoodnetwork:cache:warm_products

Thanks for writing this up, Lynne! I’ve made a few edits, the main one being around the super admin cache status page. This page checks the cache integrity every page load, so there’s no need to wait for the background task to run - it will report errors immediately.

Thanks for checking through and fixing, rohan :slight_smile: