Problem/Motivation

As an example, you have the node entity type with three bundles: article, blog, and page.

JSON API provides three API resources for those: jsonapi/node/article, jsonapi/node/blog and jsonapi/node/page.

The entity list cache tag for node, node_list, will automatically be added to each of those responses but I think it's wasteful. Any operation on an article node will clear caches for blog and page. There's no need for that.

Proposed resolution

Stop adding the entity type's list cache tag to responses. Rather, use the tag that takes the bundle in to account, ie, {entity_type_id}_list:{bundle}.

Remaining tasks

  • Decide if this is a good idea and worth doing.
  • Evaluation any potential downsides such as other list cache tags not making it in the the response that may be required.
  • Write a patch.
CommentFileSizeAuthor
#18 drupal-3090131-16.patch1.41 KBleon kessler
#7 3090131-1.patch1.38 KBAnonymous (not verified)

Comments

mstef created an issue. See original summary.

gabesullice’s picture

Title: Use per-bundle entity list cache tags to potentially increase caching and performance » [PP-1] Use per-bundle entity list cache tags to potentially increase caching and performance
Version: 8.8.x-dev » 8.9.x-dev
Status: Active » Postponed
Issue tags: +API-First Initiative
Related issues: +#2145751: Introduce ENTITY_TYPE_list:BUNDLE cache tag and add it to single bundle listing

This is a good idea @mstef, the blocker is #2145751: Introduce ENTITY_TYPE_list:BUNDLE cache tag and add it to single bundle listing (see esp. comment #70).

gabesullice’s picture

Title: [PP-1] Use per-bundle entity list cache tags to potentially increase caching and performance » Use per-bundle entity list cache tags to potentially increase caching and performance
Status: Postponed » Active
wim leers’s picture

Issue tags: +D8 cacheability, +scalability
Anonymous’s picture

Assigned: Unassigned »
wim leers’s picture

Great! Looking forward to your patch, @wouter.adem, and thanks for stepping up! 🥳🙏

Anonymous’s picture

Issue tags: +Needs tests
StatusFileSize
new1.38 KB

Here's a patch with the proposed fix. Needs automated tests.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

arla’s picture

With the patch in #7, the node_list tag is indeed removed from /jsonapi/node/<bundle>. However, it still appears in /jsonapi/node/<bundle>?filter[status]=1. Not sure why or what other parameters cause this.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

edisch’s picture

Logistically how do we prevent stale relationship data with this? I think that would be especially relevant to ?include queries where potentially data from other bundles is included on the parent bundle response.

I'd love to see this implemented but I'm having trouble wrapping my head around how to properly reconcile the cache. Hardest problems in comp-sci I guess...

bbrala’s picture

The cache is already varied by ?include, if references are changes shouldn't the parent caches also been cleared normally? Therefor there isn't much issue here?

I think if we have some proper tests this could be verified.

berdir’s picture

This issue isn't about being correct, it's about having fewer invalidations while still being correct. Invalidating the overall list cache tag is the easiest option, but it is possibly also to aggressive. Imagine you have a news site, then a news is probably going to be updated every few minutes and node_list is invalidated very frequently. But you might also have dossier/topics or something, and those only change once every few days. So a jsonapi query to list all dossiers does _not_ need to be invalidated every time a news is updated. You know it will only ever list dossiers, so you can just add the node_list:dossier cache tag.

I don't know the jsonapi code very well, but I imagine that this might be much easier to implement than the sister issue for the views module.

gabesullice’s picture

Logistically how do we prevent stale relationship data with this?

I think this is a non-issue. Relationship resources don't use the *_list cache tag since new entities are not automatically added to relationship routes—they must be referenced by the parent entity in order to appear. For that reason, relationship routes use the referencing entity's cache tag. For example, if article:42 references related articles and a new article gets created, it will not automatically appear in article:42's list of related articles. However, when someone does explicitly add it, the related route will be invalidated when the node:42 cache tag is invalidated.

I think that would be especially relevant to ?include queries where potentially data from other bundles is included on the parent bundle response.

The same applies here since collections with include queries have the cache tags from each included entity.

tl;dr: relationship routes and the include query param are irrelevant because the ENTITY_TYPE_list cache tags only pertain to "automatic" listings, not lists of referenced content.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

leon kessler’s picture

StatusFileSize
new1.41 KB

Just tried out this patch and needed to make a small change. As we are using JSON:API Cross Bundles, and I noticed on these routes you would get a cachtag of node_list: (i.e. a colon was added but no bundle).

This new patch adds a check to see whether a valid bundle exists.

I also couldn't replicate the issue from #9, when I added filters to the query when using the patch, I still got the node_list:bundle_name tag.

bbrala’s picture

Great you had a look, could you please add an interdiff? That always helps understanding the changes a bit better.

See this page in the documentation

leon kessler’s picture

Yes sorry, I tried to create an interdiff but got this whitespace error that I don't have time to look into right now. interdiff: Whitespace damage detected in input

The difference is:

if ($resource_type->getBundle() !== $resource_type->getEntityTypeId()) {

Was changed to

if ($resource_type->getBundle() && $resource_type->getBundle() !== $resource_type->getEntityTypeId()) {
leon kessler’s picture

If anyone is interested, I created a custom module for our project that generates cache tags based on JSON:API filter values. (Using bundle specific list cache tags did not go far enough for our requirements).
https://github.com/ministryofjustice/prisoner-content-hub-backend/tree/m...

wim leers’s picture

@Leon Kessler: do you mind publishing this on drupal.org as a module? 🤞😊

leon kessler’s picture

I was planning to. But it's a bit bare bones at the moment, only currently supports entity reference fields being filtered by uuid. But I could release as a module in development.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

leon kessler’s picture

I've now posted this as a full project: https://www.drupal.org/project/jsonapi_filter_cache_tags
Note that tests are currently failing in D10 (they pass on D9), haven't had time to look into it yet.

Also, it only currently supports filtering on entity reference fields, no other field types.

The module was very much created for our own needs, and probably needs a bit of work to get to a stable release.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

ghost of drupal past’s picture

Isn't this a duplicate now? https://www.drupal.org/node/3107058Nevermind, the issue title/summary misled me.

ghost of drupal past’s picture

Title: Use per-bundle entity list cache tags to potentially increase caching and performance » Use per-bundle entity list cache tags to potentially increase caching and performance in jsonapi
Issue summary: View changes

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.