Skip to content

Change the suggestion about adapters and enqueue_after_transaction_commit - #52659

Merged
rafaelfranca merged 1 commit into
rails:mainfrom
rosa:suggestion-for-db-adapters-for-enqueue_after_transaction_commit
Aug 20, 2024
Merged

rafaelfranca merged 1 commit into
rails:mainfrom
rosa:suggestion-for-db-adapters-for-enqueue_after_transaction_commit

Conversation

@rosa

@rosa rosa commented Aug 20, 2024

Copy link
Copy Markdown
Member

After discussing with @dhh about this, Solid Queue will set this to :always by default so the comment could be confusing for people using it with the same DB as the app.

See rails/solid_queue#212 (comment)

…ommit`

Solid Queue will set this to `:always` by default so the comment could
be confusing for people using it with the same DB as the app.

See rails/solid_queue#212 (comment)
@rafaelfranca
rafaelfranca merged commit d8d5554 into rails:main Aug 20, 2024
@dhh

dhh commented Aug 21, 2024

Copy link
Copy Markdown
Member

Actually, what adapters do set something else by default? I don't love the :default option being this mystery box that's dependent on the adapter. That just seems confusing. I think this setting should just either be true or false. Even the :always implies that there's a :sometimes, which is also just odd.

@rosa
rosa deleted the suggestion-for-db-adapters-for-enqueue_after_transaction_commit branch August 21, 2024 15:51
@rosa

rosa commented Aug 21, 2024 •

Copy link
Copy Markdown
Member Author

@dhh GoodJob sets it to false bensheldon/good_job#1311, possibly because of the guides' hint to do that. Not sure if there are others.

Something I found a bit confusing is that the adapter is expected to return true or false from #enqueue_after_transaction_commit? but the configuration option, with the same name, needs to return :always, :never or :default 🤔

after_transaction = case self.class.enqueue_after_transaction_commit
when :always
true
when :never
false
else # :default
queue_adapter.enqueue_after_transaction_commit?
end

@rosa

rosa commented Aug 21, 2024 •

Copy link
Copy Markdown
Member Author

In fact I made that mistake myself in this PR description! I said:

Solid Queue will set this to :always by default

But I meant:

Solid Queue will set this to true, so the default will be like :always

😅

@dhh

dhh commented Aug 21, 2024

Copy link
Copy Markdown
Member

That's all just way too confusing. I think we need to stop delegating this to the adapter. And instead we simply go with enqueue_after_transaction_commit = true|false. That's clear. Feel like this is the kind of change that it's fine to be strict about. So we can simply raise an exception if it's set to one of the old symbols and explain what folks need to do instead. cc @rafaelfranca

@rafaelfranca

Copy link
Copy Markdown
Member

We delegate to the adapter because I think our users should not have to decide this. If they swap from SolidQueue to Sidekiq, they should not have to know they also need to change enqueue_after_transaction_commit from false to true.

But, if adapters are themselves making people chose which value they should set for this option, like it seems GoodJob is, then I agree we should just use the boolean.

@rosa

rosa commented Aug 21, 2024

Copy link
Copy Markdown
Member Author

But, if adapters are themselves making people chose which value they should set for this option, like it seems GoodJob is, then I agree we should just use the boolean.

No, I don't think GoodJob is doing that, GoodJob is choosing a value because it needs to, adapters need to return true or false for enqueue_after_transaction_commit? because it's called from Active Job:

after_transaction = case self.class.enqueue_after_transaction_commit
when :always
true
when :never
false
else # :default
queue_adapter.enqueue_after_transaction_commit?
end

So, I don't think adapters are making people choose. It's rather Active Job is making adapters choose, and then the user needs to know what the adapter chose.

@rosa

rosa commented Aug 21, 2024

Copy link
Copy Markdown
Member Author

I think our users should not have to decide this. If they swap from SolidQueue to Sidekiq, they should not have to know they also need to change enqueue_after_transaction_commit from false to true

So, with the current behaviour of delegating to the adapter, what happens is that the behaviour might change from under the user without them knowing when changing adapters. To ensure the behaviour doesn't change under them, users would need to opt for always or never, so whatever the adapter chose via queue_adapter.enqueue_after_transaction_commit? is never taken into account.

I'd say it's Active Job's behaviour what forces the user to know and choose 🤔

I might be missing something here, about why it was implemented this way. I'll review the original PR that introduced this setting.

@rosa

rosa commented Aug 21, 2024

Copy link
Copy Markdown
Member Author

I've re-read all comments in #51426, and it seems the idea with the adapter delegation and the different values always, never, default was just to allow DB-backed adapters like Solid Queue opt out of this new behaviour 🤔 But Solid Queue will opt-in by default at least, and this differentiation would cause the issues when moving from a DB-backed adapter to another type of adapter, or changing to run jobs in a different DB...

I also noticed delayed sets this to false too, like GoodJob: Betterment/delayed#40, but it goes even further and raises if this is being overridden in some job. This makes sense if you want to emphasize the transactional integrity of enqueuing jobs in the same DB as your app.

@rafaelfranca

Copy link
Copy Markdown
Member

But, if adapters are themselves making people chose which value they should set for this option, like it seems GoodJob is, then I agree we should just use the boolean.

I mean this line here https://github.com/luizkowalski/good_job/blob/8a2e3d7f6280956297d6dc401ae0dcc715e8500a/lib/good_job/configuration.rb#L397. It seems GoodJob allow users to change that value.

Rails is defaulting to :default, that means, delegate to the job adapter.

We can change this option to be a boolean, but then the job adapter will have to do like delayed and raise an error depending on which option the user chose, and Rails used as a default.

That is why I think this should be up to the adapters. They know what they support.

@rosa

rosa commented Aug 21, 2024

Copy link
Copy Markdown
Member Author

I mean this line here https://github.com/luizkowalski/good_job/blob/8a2e3d7f6280956297d6dc401ae0dcc715e8500a/lib/good_job/configuration.rb#L397. It seems GoodJob allow users to change that value.

Ohhhh! I totally missed that! So sorry. Now what you say makes sense. I think, for the user, it doesn't make sense to change this in both places, the adapter and active job configuration 🤔 I suppose most won't change it there.

We can change this option to be a boolean, but then the job adapter will have to do like delayed and raise an error depending on which option the user chose, and Rails used as a default.

That is why I think this should be up to the adapters. They know what they support.

That sounds good to me, I still find the whole thing a bit confusing, but I imagine that if the defaults make sense, most people won't need to worry about this at all?

@rafaelfranca

Copy link
Copy Markdown
Member

That sounds good to me, I still find the whole thing a bit confusing, but I imagine that if the defaults make sense, most people won't need to worry about this at all?

That is my hope as well. Not sure how that will pan out, but given 7.2 leaves this to the adapters and people don't need to chose, if we see reports that people need to explicitly set this value we can totally simplify and take ownership of setting this config in the framework.

@dhh

dhh commented Aug 21, 2024

Copy link
Copy Markdown
Member

We delegate to the adapter because I think our users should not have to decide this. If they swap from SolidQueue to Sidekiq, they should not have to know they also need to change enqueue_after_transaction_commit from false to true.

I don't think this is a good path, to be honest. First of all, the setting is altering a setting on our end with when the job is actually enqueued, no? Like we handle the delay until the transaction commit.

But more importantly, it's of critical importance that people actually understand just how sharp the knife of queuing within the transaction is. And that it's a recipe of bad surprises if hopping from one adapter to another changes that underneath you. I think we must make the user responsible for this choice.

I ALSO think that this is very rarely going to be used. Which actually brings me to a more natural conclusion: We shouldn't offer this setting at all. If it's that sharp, that rarely used, and we don't have anyone on core who'd actually use it themselves, it's not a good knife.

And I say that knowing that I advocated for the inclusion of this feature for quite a while! And I remember that @byroot trying to convince me it wasn't a good idea at the last Rails World core meeting, and I thought that it was, and I realize that I was wrong 😂

In other words, I think the right path forward is simply to drop this feature entirely. Don't offer a path to enqueue within the transaction. Accept that we give up on some potential benefit, but that the situation it's available in is just too narrow or too time limited.

@rosa

rosa commented Aug 21, 2024

Copy link
Copy Markdown
Member Author

I ALSO think that this is very rarely going to be used. Which actually brings me to a more natural conclusion: We shouldn't offer this setting at all. If it's that sharp, that rarely used, and we don't have anyone on core who'd actually use it themselves, it's not a good knife.

I think it might get used more than this, but on very specific cases, and not necessarily because of the advantages of having jobs in the same DB as the app. We use this ourselves for cases where not enqueuing the job would mean ending up with serious inconsistencies. If we always defer to after all transactions are committed and can't even check if the job was successfully enqueued, we're in trouble. This is the case even if the adapter uses Redis. I think we shouldn't lose this.

What we should avoid is the inadvertent dependency on jobs being enqueued within a transaction, but that'd be covered by Rails's default and the adapter's default.

@rafaelfranca

Copy link
Copy Markdown
Member

Just to make sure I understood, @dhh you are advocating that we always keep rails with the behavior of enqueue_after_transaction_commit = true and not let the adapter chose it?

I have no problem with that, is it is what we advocated, but I thought we wanted to allow ACID adapters to enqueue the job inside the transaction.

If you don't believe SQ will do that, I don't see why we should let adapters chose it. We can allow true/false as configuration, but it will be it, an application wide configuration.

@rosa

rosa commented Aug 21, 2024 •

Copy link
Copy Markdown
Member Author

we always keep rails with the behavior of enqueue_after_transaction_commit = true and not let the adapter chose it?

I'd be totally fine with that as long as the user can still opt out of this behaviour for specific jobs, as it is now 😅

In Solid Queue we're planning to set enqueue_after_transaction_commit = true anyway, but we do need to enqueue some jobs within a transaction. We already did that with Resque and in some cases we need to know whether a job was enqueued or not. When the enqueuing is deferred until after all transactions commit, it's impossible to know because there's no way to check the job that you tried to enqueue, you can't pass a block, or rescue certain exceptions, etc.

@rafaelfranca

Copy link
Copy Markdown
Member

Yeah. I'd keep the ability of changing this per job. I think what we are discussing here is if we should delegate to the adapter and the :always/:never options instead of true/false.

@dhh

dhh commented Aug 21, 2024

Copy link
Copy Markdown
Member

Yeah, I'm saying we shouldn't expose this as a top-level setting that we encourage neither people nor adapters to mess with. In terms of timing, yes, totally fine that a job can overwrite this, just as long as we document the issues with ACID. You're not going to get that job rolled back if the txn fails when the SQ db is separate from the app DB.

@rafaelfranca

Copy link
Copy Markdown
Member

Opened an issue so we don't forget to do it #52675

@bensheldon

bensheldon commented Aug 22, 2024 •

Copy link
Copy Markdown
Contributor

For perspective, when I launched GoodJob I was surprised by the interest and awareness by developers for transactional integrity when enqueuing jobs, and that was a major adoption-reason for GoodJob. This was not on my radar initially and I have since emphasized that more. When developers are considering alternate backends, I think they are intensely studying the spec-sheets, and transactional integrity as a checkbox is pretty high on that list.

The reason I exposed the enqueue_after_transaction_commit as a configuration value on GoodJob I didn't see mentioned explicitly here: using multiple databases. I consider that an advanced usage pattern in Rails, and discourage people from solely storing jobs in a separate database without reason (that's usually a sign they should be using Sidekiq Enterprise or a custom solution specifically built for massive scale).

Developmentally, I think someone's new application defaults would ideally include transactional integrity, and then as their application grows (or they add additional customization / non-omakase things) only then do they have to consider transactional integrity as something they can lose.

It's not the end of the world if this configuration is deprecated, but I imagine part of GoodJob's setup guide would be to tell people to enable transactional-integrity for all jobs (or whatever mechanism that was) with some notes/caveats (not cause I'm driving that necessarily, but I know that someone else will suggest it). Devs will be coming to GoodJob's docs for setup and usage guidance not Rails' docs. I feel like that's the adapter's responsibility to provide a good experience with the sharp knives.

@rosa

rosa commented Aug 22, 2024 •

Copy link
Copy Markdown
Member Author

Great points, @bensheldon 🙏 Thanks a lot for writing that up. I agree with pretty much everything. I used to feel very strongly about transactional integrity as a feature (advocating for it, I mean) but I learnt this was not what Rails Core wanted, more the opposite. Since that's not my call, I just want to make things as simple and clear as possible and don't mind jumping extra hoops for the cases where I want a job enqueued within a transaction, for whatever reason 😅 The discussion is all around transaction integrity but there are other reasons why you don't want to defer enqueuing to after all transactions have committed. As I said, one big disadvantage of the current implementation, transactional integrity aside, is that you have no way to know if a job was enqueued or not because successfully_enqueued? just returns true directly, and there's no other way to check later. For many jobs, this is fine but for certain jobs you really need to know.

This is also quite interesting, I didn't know this:

I was surprised by the interest and awareness by developers for transactional integrity when enqueuing jobs, and that was a major adoption-reason for GoodJob.

I thought this was a minor factor; it's good to know it's not.

Thanks for explaining why you exposed enqueue_after_transaction_commit for GoodJob, that makes sense, and I didn't consider that. With Solid Queue and SQLite you'd probably want to start with your jobs in a separate database from the beginning, and I think that'd be the default configuration (for SQLite only).

It's not the end of the world if this configuration is deprecated, but I imagine part of GoodJob's setup guide would be to tell people to enable transactional-integrity for all jobs

Yes, that makes sense, too, and since it should be possible to set it application-wide, it shouldn't be too much of a problem 🤔

@rafaelfranca

Copy link
Copy Markdown
Member

Just as an implementation detail, removing the global config and keeping the per-job config doesn't remove the global config since one could do:

class ApplicationJob < ActiveJob::Job
  self.enqueue_after_transaction_commit = false
end

The work will mostly to simplify the config to be true/false instead of :always/:never/:default, and lower the documentation from a application config to a per-job config.

rosa added a commit to rails/solid_queue that referenced this pull request Aug 24, 2024
…ging it

See the discussion in rails/rails#52659.
Adapters should set their own behaviour without making users choose. If
users want to choose, they can just use Active Job's configuration.
Besides, we're changing the value of this to `true` to make it harder
for people to run into trouble in the future when switching adapters.
rosa added a commit to rails/solid_queue that referenced this pull request Sep 2, 2024
…ging it

See the discussion in rails/rails#52659.
Adapters should set their own behaviour without making users choose. If
users want to choose, they can just use Active Job's configuration.
Besides, we're changing the value of this to `true` to make it harder
for people to run into trouble in the future when switching adapters.
rosa added a commit to rails/solid_queue that referenced this pull request Sep 9, 2024
…ging it

See the discussion in rails/rails#52659.
Adapters should set their own behaviour without making users choose. If
users want to choose, they can just use Active Job's configuration.
Besides, we're changing the value of this to `true` to make it harder
for people to run into trouble in the future when switching adapters.
rosa added a commit to rails/solid_queue that referenced this pull request Sep 16, 2024
…ging it

See the discussion in rails/rails#52659.
Adapters should set their own behaviour without making users choose. If
users want to choose, they can just use Active Job's configuration.
Besides, we're changing the value of this to `true` to make it harder
for people to run into trouble in the future when switching adapters.
rosa added a commit to rails/solid_queue that referenced this pull request Sep 16, 2024
…ging it

See the discussion in rails/rails#52659.
Adapters should set their own behaviour without making users choose. If
users want to choose, they can just use Active Job's configuration.
Besides, we're changing the value of this to `true` to make it harder
for people to run into trouble in the future when switching adapters.
thomasnynas12 pushed a commit to thomasnynas12/solid-queue-e that referenced this pull request Mar 13, 2025
…ging it

See the discussion in rails/rails#52659.
Adapters should set their own behaviour without making users choose. If
users want to choose, they can just use Active Job's configuration.
Besides, we're changing the value of this to `true` to make it harder
for people to run into trouble in the future when switching adapters.
KingStar365 pushed a commit to KingStar365/solid_queue that referenced this pull request Mar 25, 2025
…ging it

See the discussion in rails/rails#52659.
Adapters should set their own behaviour without making users choose. If
users want to choose, they can just use Active Job's configuration.
Besides, we're changing the value of this to `true` to make it harder
for people to run into trouble in the future when switching adapters.
sincerydev123 added a commit to sincerydev123/queue that referenced this pull request Jan 26, 2026
…ging it

See the discussion in rails/rails#52659.
Adapters should set their own behaviour without making users choose. If
users want to choose, they can just use Active Job's configuration.
Besides, we're changing the value of this to `true` to make it harder
for people to run into trouble in the future when switching adapters.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants