Updating cached REST resources I'm building my first custom REST resources.  I have a custom entity and returning specific fields from it.  However, it's very important that if a specific field changes (a status field) that it is reflected in the response.

What I'm seeing is that I have to clear the cache every time I update the entity.  How do I make sure the response is up-to-date?

I have tried using both \Drupal::entityQuery and \Drupal::entityTypeManager()-&gt;getStorage()... but both require a cache clear.  Maybe I'm loading the entity incorrectly?
It's not about how you load the entity. It's how you build the response. You have to add the correct cache tags. A cacheable response has the method addCacheableDependency(), which accepts objects which provide cache metadata:

$response-&gt;addCacheableDependency($entity);


This adds the tag custom_entity:[id] to the cached response. All cached responses with this tag will be invalidated, when the entity is updated.

This is an example from core for the cache data and access checks you need in a response which contains entity data:

Drupal\rest\Plugin\rest\resource::get()

public function get(EntityInterface $entity) {
    $entity_access = $entity-&gt;access('view', NULL, TRUE);
    if (!$entity_access-&gt;isAllowed()) {
      throw new AccessDeniedHttpException();
    }

    $response = new ResourceResponse($entity, 200);
    $response-&gt;addCacheableDependency($entity);
    $response-&gt;addCacheableDependency($entity_access);

    if ($entity instanceof FieldableEntityInterface) {
      foreach ($entity as $field_name =&gt; $field) {
        /** @var \Drupal\Core\Field\FieldItemListInterface $field */
        $field_access = $field-&gt;access('view', NULL, TRUE);
        $response-&gt;addCacheableDependency($field_access);

        if (!$field_access-&gt;isAllowed()) {
          $entity-&gt;set($field_name, NULL);
        }
      }
    }

    return $response;
  }