Proposal: deduplicate pending jobs with coalesce

オープン
#834 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 4 日以内に返信

まだ誰も着手していません。

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
25/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
活発
技術スタック
rails, ruby
領域
backend, databases

調査の方向性

Read the original uniqueness discussion (#105), concurrency discussions (#176 and #597), and exploratory PR #835 alongside the solid_queue-coalesce reference gem. Start by evaluating whether the proposed pending-job lifecycle and API fit Solid Queue's enqueue, dispatch, and cleanup paths. Done means maintainers agree on whether the feature belongs in core and, if so, define its minimal API and persistence model.

索引モデルが issue の本文から書いたものです。

説明

Hi Rosa and team,

David (human) here. We've used Solid Queue in production for nearly a year and love it.

We found that limits_concurrency on_conflict: :discard doesn't deduplicate scheduled jobs. After causing us a production incident, we also discovered that it can discard important work on a retry. I feel that limits_concurrency is not the right tool for deduplication.

Below is my AI-authored issue that describes the problem and my proposed solution, introducing the concept I'm calling coalesce.

I've written a gem solid_queue-coalesce (that composes well with limits_concurrency) exploring these ideas further, including replacing arguments, collecting calls into batches, debouncing and throttling. For now, I'm only proposing basic deduplication, but check out the gem if you're curious about the rest.


Motivation

Issue #105 described one pending job per key, with another allowed as soon as the first starts. limits_concurrency on_conflict: :discard (#176, #594) controls execution, not pending jobs:

  • Scheduled jobs can accumulate, and more than one can run (#597).
  • Calls made during execution can be dropped, even if the running job has already read the state being updated.
  • Callers can't identify which existing job covers their request.

For example, repeated product changes should share one pending reindex. A change during that reindex should schedule the next one.

on_conflict: :discard remains useful for shedding load; coalescing would complement it.

Proposal

class ReindexProductJob < ApplicationJob
  coalesce key: ->(product_id) { product_id }

  def perform(product_id)
    Product.find(product_id).reindex!
  end
end
  • Calls with the same key, including scheduled ones, share one pending job.
  • The key is released when that job starts, so a call during execution queues the next one.
  • Keys are scoped to the job class and batch; retries never coalesce.
  • coalesce_status (:created or :discarded) and provider_job_id tell the caller what happened.
  • Semaphores from limits_concurrency are untouched.

The first job's arguments and scheduled time are retained. Collecting calls, replacing arguments and debouncing are outside this proposal.

Implementation

The standalone gem works but patches private Solid Queue methods, making upgrades fragile. Native support could avoid that. #835 illustrates a minimal implementation using a solid_queue_coalesce_keys table and row locks; it's intended to support discussion, not represent a complete solution. The gem also has benchmarks.

Trade-offs

  • A new table requires a migration.
  • Row locking introduces contention and potential deadlocks; #835 also enqueues coalesced bulk jobs individually, allowing partial batches on failure.
  • Two jobs with the same key can run at once. Add limits_concurrency to prevent that.

Questions

  1. Would you want pending-job coalescing in Solid Queue?
  2. Should it be its own coalesce key: declaration, or an option on limits_concurrency?
  3. If it stays a gem, would you take small public extension points so it doesn't have to patch private methods?

🤖 Written by @dtcristo's agent (Claude Opus 5.5)

主要言語
Ruby
スター
2.5k
フォーク
271
平均マージ
5日 6時間
マージ済み PR(30日)
5

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

  • Dockerfile または Docker Compose ファイルあり
  • プルリクエストのテンプレートなし
  • コントリビューションガイドなし

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

rails/solid_queue のほかの issue

rails/solid_queue の issue をすべて見る

似ている issue

Ruby の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。