How to write big updation code using Batch API?
The timeout is caused because you're trying to load all 5K+ users before you start the batch. Instead get the User IDs first and load when processing. Check an example below. Also have a look at Batch API Overview documentation page, before you continue:

function anonymize_data_menu() {
  $items = array();
  $items['admin/config/content/anonymize-user-data'] = array(
    'title' =&gt; 'Anonymize User Data',
    'description' =&gt; 'Click this link to anonymize user data',
    'type' =&gt; MENU_LOCAL_ACTION,
    'page callback' =&gt; '_anonymize_users_data',
    'access arguments' =&gt; array('administer user anonymize'),
    'file' =&gt; 'anonymize_data.user.inc',
  ); 
  return $items; 
}

function _anonymize_users_data() {
  // Instead of loading all users at once,
  // first get there uid and then load chunk of user while processing.
  // $users = entity_load('user');
  $uids = db_select('users', 'u')
    -&gt;condition('u.uid', 1, '&gt;')
    -&gt;fields('u', array('uid'))
    -&gt;execute()-&gt;fetchAllKeyed(0, 0);

  $chunk_count = 20;
  // We've all rows here, split them in chunks.
  $chunks = array_chunk($uids, $chunk_count, TRUE);
  // Get all rows count.
  $num_operations = count($uids);

  $operations = array();
  foreach ($chunks as $chunk) {
    $operations[] =  array('user_anonymize_op', array(
      $chunk,
      array(
        'max' =&gt; $num_operations,
        'chunk' =&gt; $chunk_count,
      )
    ));
  }

  $batch = array(
    'operations' =&gt; $operations,
    'title' =&gt; t('Import batch'),
    'init_message' =&gt; t('Initializing'),
    'error_message' =&gt; t('An error occurred'),
    'finished' =&gt; 'user_anonymize_op_finished'
  );

  batch_set($batch);
  batch_process('admin/people');
}

function user_anonymize_op($rows, $batch_data, &amp;$context) {
  // Define variables on start of batch operation.
  if (!isset($context['results']['progress'])) {
    $context['results']['progress'] = 0;
    $context['results']['chunk'] = $batch_data['chunk'];
    $context['results']['max'] = $batch_data['max'];
    $context['results']['failed'] = array();
  }

  // Do your processing here. START HERE.
  $names = [];
  $users = entity_load('user', $rows);
  foreach ($users as $account) {
    if (!empty($account-&gt;uid) &amp;&amp; ($account-&gt;uid &gt; 1)) {
      // What is $i here?
      $account-&gt;name = $i . '-' . $account-&gt;name;
      $account-&gt;pass = user_hash_password('blah');
      user_save($account);
      $names[] = $account-&gt;name;
    }
  }
  // Do your processing here. END HERE.

  // Update completion count.
  $context['results']['progress'] = $context['results']['progress'] + count($rows);

  $context['message'] = t('Processed @name user data.', array('@name' =&gt; implode(', ', $names)));

  // Inform the batch engine that we are not finished,
  // and provide an estimation of the completion level we reached.
  if ($context['results']['progress'] != $context['results']['max']) {
    $context['finished'] = $context['results']['progress'] % $context['results']['max'];
  }
}

function user_anonymize_op_finished($success, $results, $operations) {
  if ($success) {
    $message = count($results) . ' processed.';
  }
  drupal_set_message(t($message));
}