Suggested API: on_conflict: :replace

Open
#824 0 comments 1 reaction 0 assignees View on GitHub

Maintainers usually reply within 4 days

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Active
Tech stack
rails, ruby
Domain
backend, database

Research direction

The payload names no files or tests. Start from the existing on_conflict: :discard path used by limits_concurrency, then trace how a pending job is stored and identified by its concurrency key. Decide the semantics for :replace and whether payload batching is included, then add coverage for first-wins versus last-wins behavior. Done means the new option is implemented, documented, and tested for duplicate enqueues while a matching job is still waiting.

Written by the indexing model from the issue text.

Description

Description

Currently, Solid Queue provides an on_conflict: :discard option to enforce uniqueness. This follows a "first wins" behavior, where subsequent duplicate jobs are dropped if a matching job is already pending in the queue.

However, many use cases (such as syncing the latest user state or syncing inventory) require a "last value wins" strategy. If a new job with the exact same concurrency key is enqueued while a previous one is still waiting, the queue should ensure that only the latest job (carrying the freshest payload) is processed. This can be achieved either by replacing the pending job entirely or updating its payload.

Introducing this strategy would also significantly boost performance under high-frequency updates, as it prevents the system from wasting database and worker cycles processing outdated intermediate payloads.

Examples from other queue managers

• Sidekiq Enterprise / Sidekiq Unique Jobs: Supports options like on_conflict: :replace or rescheduling to overwrite the payload of an un-executed job with the freshest data.
• Lowkiq: Passes an array of all enqueued payloads directly to the perform method, allowing the developer to decide how to process or batch them.

Suggested API

It would be useful to introduce a configuration option to define how concurrency conflicts are handled:
Option A: Pure :replace strategy

class UpdateInventoryJob < ApplicationJob
  limits_concurrency to: 1, key: ->(payload) { payload.id }, on_conflict: :replace

  def perform(payload)
    # ...
  end
end

Option B: Custom conflict hook with payload batching

class UpdateInventoryJob < ApplicationJob
  limits_concurrency to: 1, key: ->(product) { product.id }, on_conflict: :replace

  def perform_on_conflict(payloads)
    # Allows manually processing the final state
    perform(payloads.last)
  end
end
Dominant language
Ruby
Stars
2.5k
Forks
271
Avg merge
5d 6h
Merged PRs (30d)
5

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

  • Ships a Dockerfile or Docker Compose file
  • No pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from rails/solid_queue

All issues in rails/solid_queue

Similar issues

More Ruby issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.