Avoiding contention during reads

Hello,

So my understanding is that multiple frequent writes to the same document can cause contention. But does contention happen if I try to read a document while a write is happening concurrently? If so, is there a way to avoid that? For my use case, I don’t mind if the reads are stale, I just want it to succeed without interruption.

I was also looking into the At function, which seems like it could provide the kind of “snapshot” reads that I’m looking for that avoid contention, but I don’t know the timestamp that I would provide. Would At(Now(), ...) be enough to tell Fauna that I just want “snapshot” reads and avoid contention?

When you send a query to Fauna, it is first placed into the transaction log, and the order of transactions is determined by consensus (among the nodes participating in handling the transaction log). Then, once the order is determined, the query is executed in deterministic fashion by all participating query execution nodes.

If you are trying to read, your read will either happen before a write or after a write, which can (obviously) affect the read, but there is no contention there. Contention occurs when multiple transactions attempt to write to the same document(s): since they are place in some order, each write must wait for the previous write(s) to complete.

You cannot use At to achieve snapshot isolation. It merely allows you to pick a time in the past with which to query the database. Each write stores a new version of one or more documents, and each version stores a timestamp. Using At essentially filters out document (and index) history that is newer than the specified timestamp.

If you do try to use At(Now(), ..., that’s equivalent to not using At, with the exception that you have 2 additional function calls in your query that might cost you an extra compute operation.

To learn more about how Fauna works, see: Consistency without Clocks: The Fauna Distributed Transaction Protocol