How does Drupal handle concurrent user creation? I can't understand a specific part of the Drupal code, namely this snippet: 

  $account-&gt;uid = db_next_id(db_query('SELECT MAX(uid) FROM {users}')-&gt;fetchField());

  // Allow 'created' to be set by the caller.
  if (!isset($account-&gt;created)) {
    $account-&gt;created = REQUEST_TIME;
  }

  $success = drupal_write_record('users', $account);


This is from the Drupal 7 code

My question is about concurrency. I imagined that two concurrent sessions are executing this and because the lack of transaction handling here they might try to insert two records with the same uid.

Can you tell me why this works in production? How is it possible that Drupal uses no transaction management although for example creating a node usually involves writing records into many different tables.
How is it possible that Drupal uses no transaction management?


It's not. The code you're querying is part of user_save(); the very first line in that function is:

$transaction = db_transaction();


The rest of the function is wrapped in a try...catch that explicitly rolls back the transaction upon exception. The same is true for node_save(), and plenty of other core functions that require transactions.

See the db_transaction() docs for more info.