When does Drupal load variables from the database? I am writing a report module so that users can flag posts and write a description. This module has a block that shows the unwatched reports; every time this block is shown it gets the number of unread reports from a table.  

Is it better to use the Drupal variable system to set the number of unread reports?
When does Drupal load the variables from the database? Does it load them when I call variable_get()?
If the values need to be continuously updated, then it is better not to use variable_get()/variable_set() for storing it. This is because every time variable_set() is called, it clears the cache containing the values of the variables, which means that the next time a page is required, the Drupal variables will be loaded from the database, and not from the cache, with consequent lose of performance.

In such case, it is better to count the number of unread reports when reading from the database table containing the report data. This means the database table should contain a unread field that is set to TRUE when the report has not been read.

The values of the Drupal variables are loaded from the database, or the cache, when Drupal is bootstraping; the called function is variable_init(), which first checks the cache, and loads the values from the database, if the cache is empty.

  if ($cached = cache_get('variables', 'cache')) {
    $variables = $cached-&gt;data;
  }
  else {
    $result = db_query('SELECT * FROM {variable}');
    while ($variable = db_fetch_object($result)) {
      $variables[$variable-&gt;name] = unserialize($variable-&gt;value);
    }
    cache_set('variables', $variables);
  }