Querying a custom table does not return latest results for anonymous user
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);