How to Generate Unique IDs for Chat Messages in Laravel with GridDB?

I’m building a real-time chat application using Laravel and want to use GridDB as my database due to its high performance with time-series data. Since GridDB doesn’t have an official PHP client, I’ve set up a custom integration using a middleware API in Python that communicates with GridDB. My Laravel application interacts with this middleware via HTTP requests.

In my chat application, each message needs a unique ID. In traditional relational databases like MySQL, I would use an auto-incrementing integer as the primary key. Since GridDB doesn’t support auto-incrementing IDs natively, I need a reliable way to generate unique IDs for chat messages that can handle high concurrency since multiple users might send messages simultaneously.

I tried combining the current timestamp with a random number:

$messageId = time() . mt_rand(1000, 9999);

This reduces the chance of collisions but isn’t foolproof, especially under high load.

I attempted to simulate auto-increment by querying the maximum existing ID and incrementing it:

$maxId = $this->getMaxMessageIdFromGridDB();
$messageId = $maxId + 1;

However, this approach isn’t safe in a concurrent environment and can lead to race conditions.

Would using GridDB’s TIMESTAMP row key with sub-second precision be a viable option for unique IDs?