A Scalable Pattern for Displaying Simple Remote Data in Drupal 8

Oct

8

2016

Let's imagine a scenario where you need to display some data from a remote service to the user. Instagram, for example. You want to grab the 6 most recent posts, pass them through some theming, then output them into a block. How would you go about doing that? In Drupal 7, one possible approach might look like this.

This works pretty well... The time intensive mymodule_fetch_instagram_data() function is only called if the cache is empty. Clearing the cache will instantly refresh the data. But what happens when Instagram goes down and the cache is cleared? The request may hang and wait for a response from instagram. Worse yet, a second and third person might request the block, leaving 3 hung requests all trying to fetch the same thing. To work around that, you could add locking around the data intensive call, like this:

This prevents our stampede problem (where multiple requests are all executing the fetch task at once), but it still requires at least one request to sit and wait while that Instagram data is fetched. What's more, the cache lifetime here is fixed. We can make it short, and raise the risk of a blocked request waiting on the fetch task, or we can make it long, and accept that we might not have the latest data. Of course this is a simplification - there are ways to work around these issues, but they required another storage mechanism other than the cache, which greatly raised the barrier to entry for building a truly scalable solution in Drupal 7.

So how has the situation changed in Drupal 8? For starters, we have a State API for containing non-volatile data. This means that Instagram data no longer has to live in a place that can be emptied out at the whim of the site owner. We can more or less depend on State data being ready for us when we need it.

On top of that, we've got some additions to the cache API that make invalidation of true cache items a whole lot easier. Cache tags allow us to clear cached items based on their dependencies. For example, when a page contains our Instagram block, and our Instagram block contains our instagram data, we can now issue a cache clear for both the page and the block at once, and the content will be rebuilt as needed.

Notice how we've moved the fetch task out of the critical path. There is no longer any chance that Instagram will take your site down. On cron, we're fetching the data, and if it's changed from what we've got stored, we invalidate the instagram_data cache tag. Assuming you run cron frequently and are using a cache-tag friendly page cache (database or Varnish), you can now have near instaneous refresh of your instagram feed across multiple pages of your site without compromising cacheability.

Of course this approach (sticking data into the State API on cron) won't necessarily work for everything... you have to know in advance what data you need. If what you're working on requires configuration (like for instance if you need to support fetching from arbitrary Instagram feeds), you may be better off using a different approach.