Search code examples
azure-service-fabricpartitioningservice-fabric-stateful

Service Fabric - How could we generate a partitionKey?


I have a stateful service with a range of partitions keys going from
-9223372036854775808 to 9223372036854775807 (UniformInt64Partition).

How can I generate an adequate partition key when calling the service in order to improves the distribution of workloads across all the partitions ?

Thank u


Solution

  • For this large range of partition keys, the best approach is using a hashing algorithm on top of a field or collection of fields to generate a key(number) with as least collision as possible.

    Assuming you are storing a customer information, as example, a hash for the customer name from "John Smith" could generate a hash value of 32, because any user with same name as "John Smith" will generate the same hash, if it is not frequent, wouldn't be a problem, because 32 is not an id and they can be repeated, having the same hash they would be stored on same partition.

    If you really want to distribute these values as even as possible, you can use another field concatenated to differentiate "John Smith" from "John Smith", like date of birth, And unless both born on same date, you will find different values for each one.

    In your case, because the range is very large, you have to use a hashing algorithm to hash these values to fit the range of -9223372036854775808 to 9223372036854775807.

    Do you need that many keys?

    If your system does not expect to have a very high number of partitions, an easy way to manage this is using a natural number that closely reflects the range of keys provided by your hashing function chosen, you might decide to chose one with better performance, or lower collision, or both.