#workpool duplication

3 messages · Page 1 of 1 (latest)

covert needle
#

<@&1228088700710293556> what's the best way to avoid adding two items to a workpool that are the same, there is a high risk of my app adding two items to the pool that are the same, but i want to avoid additional queries/mutations, so add or skip behavior is preferred.

bold agateBOT
#

Hi @covert needle. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

To avoid adding duplicate items to a Convex Workpool, the best practice is to ensure idempotency at the application level. This means you should check for the existence of a "work item" before enqueuing it, typically by asserting invariants within a transaction. Convex mutations are sequentially consistent transactions, so you can safely check your table state before updating records—no race conditions will occur. If you want an "add or skip" behavior, you can implement logic in your mutation to check if the work item already exists (e.g., by a unique key or constraint) and only enqueue if it does not. This avoids the need for additional queries or mutations outside the transaction, and ensures atomicity and correctness.

From a Discord support thread:

"just assert whatever invariants you want in your transaction before you commit. if you want to apply something only once, you can just check your table state before updating records. no race conditions will occur"
Discord support thread: Idempotency

There is no built-in "add or skip" method in Workpool itself, so this pattern—checking for existence and conditionally enqueuing—is the recommended approach. If you want to avoid additional queries, ensure your check and enqueue logic are in the same mutation, leveraging Convex's transactional guarantees.

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: