#stop a bad mutation from retrying
4 messages · Page 1 of 1 (latest)
Hi @forest coyote. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Convex mutations are designed to automatically retry until they succeed, which provides strong reliability guarantees. However, there are situations where you might want to stop a mutation from retrying. Let me explain the options available:
Understanding Mutation Retries in Convex
By default, when you call a mutation from a Convex React client, it will automatically retry until it succeeds. This is part of Convex's design to ensure that user edits aren't lost, even during network issues or browser tab closures Stack: Durable Workflows and Strong Guarantees.
Stopping Mutation Retries
Currently, there isn't a direct way to cancel a mutation once it's been submitted. This is by design, as Convex aims to provide strong guarantees about data consistency. However, there are several approaches you can take:
1. Use Optimistic Updates
Instead of waiting for the mutation to complete, you can use optimistic updates to immediately update the UI while the mutation is processing. This way, even if the mutation is retrying in the background, the user experience isn't blocked:
// Configure an optimistic update to make your app more responsive
// while the mutation is being processed or retried
Convex React: Editing data explains that optimistic updates are temporary, local changes to your query results that make your app more responsive.
2. Handle Errors Appropriately
You can catch errors from mutations and handle them appropriately:
try {
await sendMessage(newMessageText);
} catch (error) {
// Handle the error here
console.error(error);
}
Functions: Error Handling provides guidance on handling mutation errors.
3. Design Idempotent Mutations
Design your mutations to be idempotent, meaning they can be called multiple times without causing unintended side effects. This way, even if a mutation retries, it won't cause problems.
4. Use Workpool for Controlled Execution
For more control over execution, you can use the Workpool component which allows you to:
- Limit parallelism
- Configure retry behavior
- Cancel work that hasn't finished yet
// Cancel work that hasn't finished yet
await pool.cancel(ctx, workId);
Components: Workpool provides details on how to use this approach.
For Network Issues
It's worth noting that if a user is offline, mutations will be queued and retried when the connection is restored. There's currently no built-in way to cancel these pending mutations, but you can use the connection state to inform users about pending operations:
// Check connection state
const connectionState = convexClient.connectionState();
Discord: Offline Mutations discusses this scenario.
If you're experiencing issues with mutations being retried too aggressively, consider reviewing your implementation to ensure you're not accidentally triggering the same mutation multiple times, which can lead to "too many concurrent connections" errors Discord: Too many concurrent connections.