Using FaunaDB to track unique values

Greetings, I’m new to Fauna and would love some advice with what I’m trying to do. This is to make sure I’m making a good decision while also furthering my understanding of the DB.

I have multiple processes writing to a versioned S3 bucket. I want to ensure unique object keys are generated.

My solution is to use Fauna to hold object key names and for writers to query fauna in order to 1) check if a key is unique and 2) reserve an object key by writing to a collection. Fauna’s global accessibility and strong consistency seem to be a good fit here. (Any suggestions to simplify solving the original problem are also valuable.)

My FaunaDB specific query is the following:

I see there are two ways to implement this.
a) Create an index to enforce uniqueness on a specific term in the collection’s documents.
b) Use the Create call on the collection with a generated key. This should fail if a document already exists with the given key.

My intuition is that b) would use less storage. I’m unsure what the actual trade offs are and how an experienced Fauna user would view this problem.

Thanks for the help!

Hi @Tuhin_Nair and welcome!

Would the NewId function work for you? That would guarantee a unique ID. Or when you create a Document, you might just use the ref id which you know will also be unique.

Hey, I should’ve mentioned that these unique object keys are used to generate pre-signed S3 URLs for direct browser based uploads. My understanding is that the Twitter Snowflake algorithm produces id’s that are somewhat guessable. Although there are other checks in place to prevent malicious use I thought it’d be better to use id’s that don’t depend on time.

So just to confirm, using the document creation check for uniqueness of the ID would be better than a dedicated index for this use case, correct? Again, thanks for the help!

That sounds like a fair assessment. This is usually not a problem since Fauna’s ABAC rules still secure access to your Documents. However, if you are using the ids as part of a URL to obtain public documents, I can see more potential for abuse.

If you are using the built in ID’s, then the Document’s Ref is already guaranteed to be unique. If you want to store an external ID (perhaps one you generate on your own serverless function) then enforcing uniqueness with an index sounds like a good idea to me.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.