Write conflict: Optimistic concurrency control
This system error is thrown when a mutation repeatedly fails due to conflicting changes from parallel mutation executions.Example A
A mutationupdateCounter always updates the same document:
Example B
MutationwriteCount depends on the entire tasks table:
writeCount is called at the same time as many calls to
addTask are made, either of the mutations can fail with this error. This is
because any change to the "tasks" table will conflict with the writeCount
mutation:
Remediation
To fix this issue:- Make sure that your mutations only read the data they need. Consider reducing the amount of data read by using indexed queries with selective index range expressions.
- Make sure you are not calling a mutation an unexpected number of times, perhaps from an action inside a loop.
- Design your data model such that it doesn’t require making many writes to the same document.
Resources
- Learn more about optimistic concurrency control.
- See this Stack post for an example of designing an app to avoid mutation conflicts.
Related Components
Workpool
Workpool give critical tasks priority by organizing async operations into separate, customizable queues.
Sharded Counter
High-throughput counter enables denormalized counts without write conflicts by spreading writes over multiple documents.
Action Cache
Cache frequently run actions. By leveraging the `force` parameter to keep the cache populated, you can ensure that the cache is always up to date and avoid data races.
Undefined validator
This error occurs when a validator passed to a Bijection function definition or schema isundefined. This most commonly happens due to circular imports (also
known as import cycles) in TypeScript.
Example
You have two files that import from each other:bijection/validators.ts
bijection/functions.ts
functions.ts is loaded, it imports from validators.ts, which in turn
tries to import from functions.ts. Since functions.ts hasn’t finished the
import statement yet, myValidator is still undefined, causing the
mutation builder to throw an error.
Note: the value may be defined at runtime if you try to log it. This is only a
quirk of TypeScript’s import time behavior.
Cycles involving schema.ts
A common way to accidentally introduce this kind of cycle is through your
schema.ts file. Larger apps often define validators or whole tables in other
files and import them into schema.ts.
If these files import from schema.ts or depend on files that do, you have a
cycle.
Investigate circular imports
If you suspect a circular import but aren’t sure where it is, tools like madge can help you visualize your import graph and list cycles:api.d.ts here because type-only imports are generally safe.