Querying a custom table does not return latest results for anonymous user I have a custom module and a simple custom table in the database. When I try to query the table to get the row, it seems Drupal 8, or maybe mysql, is caching the results.

$connection = \Drupal::database();

$result = $connection-&gt;query("SELECT SQL_NO_CACHE * FROM {mytable} WHERE id = (:id)", [':id' =&gt; $key]);

$result-&gt;fetch()


We get the latest data if a I am  logged in to Drupal, but the data is cached if we are not logged in.
Try to user drupal select instead of normal query something like following:

  // Add this use at the top of the file
  use Drupal\Core\Database\Database;

  $connection = Database::getConnection();
  $query = $connection-&gt;select('mytable', 'my_table')
    -&gt;condition('id', $key, '=');
  $result = $query-&gt;execute()-&gt;fetchAll(PDO::FETCH_ASSOC);