How to serialize multiple calls to the same query


I need to assign servers unique integer ids. So I created a vertex named Counter that has one attribute named uniqueId which is type INT. I then created a query named getServerId that prints the current uniqueId and then increments uniqueId by 1.

However, the issue I’m running into is that when 2 servers call at exactly the same time, they both get the same Id. Is there a way to process these calls serially so they get unique Ids? According to the docs a Write-Write scenario should result in sequential execution. I’m probably misunderstanding this.

Here is my simple code:
INT serverId;

result = SELECT c
FROM Counter:c
WHERE == “counter”
POST-ACCUM serverId = c.uniqueId, c.uniqueId += 1

PRINT serverId AS data;

Any help would be much appreciated.



I would recommend using gsql_uuid_v4() for this, which is used to generate generic PRIMARY IDs for vertices as STRING. However, you can also use this function in your custom case for things other than PRIMARY ID. The generated IDs will be a combination of numbers, letters, and dashes.

There was a discussion about this on the forums recently, as well: Beginner: GSQL/TG Inserts

I feel like this would be safer for running in parallel, since the IDs are lengthy and completely random.

The generated IDs look like this: 576352f0-fa69-4e87-bd45-114f86734695

Thx for the help Ishestakov.

I wish it were so easy. However I am generating 64 bit Snowflake Ids on the front end web servers and each server must have a unique id from 0-1023.