Kalix entity key generator

Currently below entity key generator, generates the key in format “ba081059-5fa6-4727-b376-45fc0b6f3176”
option (kalix.method).entity.key_generator = VERSION_4_UUID;

Can we customize this to generate the key of length 10 with all numbers

1 Like

Hi @thamizh07,

you can do this with a Action forwarding the call to the entity.

@aludwiko Could you please explain with some example.

Ok, so you can remove the generator from the entity and simply accept the id from the request, but instead of calling the entity directly, you can call an Action that will generate the id and call (forward the call) the entity with this id. This way, you can have any id generation algorithm you want.

An issue with what you want is that Kalix entity key generation is stateless. We don’t have any sequences/counters that we can use to generate keys, so we have to use key generation methods that are stateless. This is why we offer UUIDs, UUIDs are 128 bit values that can be generated in such a way that they are guaranteed to be unique. Version 4 UUIDs are generated using a random number generator, and, chances are, it will take longer than the heat death of the universe before a single duplicate version 4 UUID is ever generated by any system on Earth.

Contrast that with a 10 digit number. It has approx 33 bits of entropy. Generating such a value, and guaranteeing that it is unique… you can’t. With 33 bits of entropy, you would only have to generate around 100 000 keys before you’d have a 50% chance of having a duplicate key.

So, I would rethink what you want to use as your keys. UUIDs are well suited to this purpose, but if you really want to use numbers, Snowflake IDs may be something you might consider. These are 64 bit numbers (so can be up to 20 digits long).