## Module 4: Use AUTO_RANDOM Instead of AUTO_INCREMENT To Avoid Hotspots

+ In a distributed database like TiDB, data is divided into regions and distributed across multiple storage nodes (TiKV nodes).

+ Using `AUTO_INCREMENT` as the primary key may leads to the following issue:
  + When using `AUTO_INCREMENT`, IDs are sequential (1, 2, 3, 4, ...)
  + New inserts all go to the same region where the latest IDs are stored
  + This creates a "hotspot" where one storage node handles most write traffic
  + The rest of the storage nodes are underutilized
  + This bottleneck can severely limit write throughput

+ `AUTO_RANDOM` is the rescue:
  + `AUTO_RANDOM` generates non-sequential, distributed IDs
  + The shard bits help distribute writes across different regions
  + The INSERT workload is balanced across all storage nodes
  + This improves write throughput and scalability for high-concurrency scenarios

  ```bash
  rm -f DemoJdbcAutoRandom.class
  javac -cp mysql-connector-j-9.1.0.jar DemoJdbcAutoRandom.java       
  sleep 1
  java -cp :mysql-connector-j-9.1.0.jar DemoJdbcAutoRandom
  ```

  ```output
  ...
  AUTO_RANDOM IDs (distributed to avoid hotspots):
  288230376151711745
  1441151880758558730
  3746994889972252677
  4323455642275676162
  4899916394579099654
  4899916394579099655
  6052837899185946628
  7493989779944505347
  7782220156096217097
  8070450532247928840

  AUTO_INCREMENT IDs (sequential, can cause hotspots):
  1
  2
  3
  4
  5
  6
  7
  8
  9
  10
  ...
  ```
