pmalmsten / cosmosdb-extensions-sessiontokens-aspnet

Cosmos DB extensions for managing session tokens with ASP.NET
MIT License
0 stars 2 forks source link

Detect when deterministically generated container codes collide, and handle appropriately #4

Open pmalmsten opened 1 year ago

pmalmsten commented 1 year ago

To minimize the total size of generated HTTP headers, the combination of account URI, database name, and container name are hashed, and the resulting hash is truncated to a shorter integer (currently between 0 and 1,000,000).

The probability of collisions is relatively low, but the possibility of it happening is may be non-trivial when large numbers of containers are involved (e.g. 50 containers).

We should do the right thing if/when that happens. To start brainstorming, this might include some combination of:

pmalmsten commented 1 year ago

For context, the probability that a collision would occur among 50 containers using a hash code range of 0 - 1,000,000 (assuming that the hash function in use is perfectly uniform) is:

(1 - (1/1000000)^50×(1000000!)/((1000000 - 50)!)) = 0.00122, or 0.122%

That probability is already relatively small (given that using >= 50 different Cosmos DB containers in a single app is probably uncommon).