solid_queue: Jobs and transactional integrity docs don't seem to be what actually happens in Solid Queue when using a single DB

Having your jobs in the same ACID-compliant database as your application data enables a powerful yet sharp tool: taking advantage of transactional integrity to ensure some action in your app is not committed unless your job is also committed and viceversa, and ensuring that your job won’t be enqueued until the transaction within which you’re enqueuing it is committed.

The above text seems to indicate that if you use the same DB for ActiveRecord application models and the Solid Queue models, job enqueuing will only happen if the application model changes are successfully committed to the DB & vice versa. We changed our implementation to move from a separate DB to a single DB to avail of this. But in practice, we are seeing that this is not the case.

After we installed some debuggers, we found that eventually, this line of code is hit. That rails method creates a new nested transaction.

Such nested transactions will not roll back all transactions correctly when using something like postgres. We confirmed this behaviour by explicitly rolling back the application transaction after enqueuing the job. The Job record was saved, the application model record was not.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 17 (7 by maintainers)

Commits related to this issue

Most upvoted comments

Aha, thanks a lot for creating that. The reason this is happening is that you’re explicitly using a different connection pool for Solid Queue, as described in the README as an alternative way to not have this transactional behaviour with jobs:

Screenshot 2024-11-22 at 22 48 13

If I do this change in your repo, to ensure the same connection pool is used by SolidQueue::Record and ApplicationRecord:

diff --git a/config/application.rb b/config/application.rb
index 5043b5e..6f3ff6b 100644
--- a/config/application.rb
+++ b/config/application.rb
@@ -17,7 +17,7 @@ class Application < Rails::Application
     config.autoload_lib(ignore: %w[assets tasks])

     config.active_job.queue_adapter = :solid_queue
-    config.solid_queue.connects_to = { database: { writing: :primary, reading: :primary } }
+    # config.solid_queue.connects_to = { database: { writing: :primary, reading: :primary } }

     # Configuration for the application, engines, and railties goes here.
     #

The behaviour is what you expect and what’s documented:

Loading development environment (Rails 7.2.2)
>> SolidQueue::Job.count
  SolidQueue::Job Count (2.5ms)  SELECT COUNT(*) FROM "solid_queue_jobs"
=> 0
>> Animal.count
  Animal Count (0.6ms)  SELECT COUNT(*) FROM "animals"
=> 0
>> Animal.create! name: 'Lion'
  TRANSACTION (0.5ms)  BEGIN
  Animal Create (2.8ms)  INSERT INTO "animals" ("name", "created_at", "updated_at") VALUES ($1, $2, $3) RETURNING "id"  [["name", "Lion"], ["created_at", "2024-11-22 21:51:01.131628"], ["updated_at", "2024-11-22 21:51:01.131628"]]
  SolidQueue::Job Create (0.8ms)  INSERT INTO "solid_queue_jobs" ("queue_name", "class_name", "arguments", "priority", "active_job_id", "scheduled_at", "finished_at", "concurrency_key", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) RETURNING "id"  [["queue_name", "default"], ["class_name", "TameJob"], ["arguments", "{\"job_class\":\"TameJob\",\"job_id\":\"ed7f2a0a-13fc-4877-8d32-0e8d5f374e2b\",\"provider_job_id\":null,\"queue_name\":\"default\",\"priority\":null,\"arguments\":[],\"executions\":0,\"exception_executions\":{},\"locale\":\"en\",\"timezone\":\"UTC\",\"enqueued_at\":\"2024-11-22T21:51:01.146363000Z\",\"scheduled_at\":\"2024-11-22T21:51:01.145177000Z\"}"], ["priority", 0], ["active_job_id", "ed7f2a0a-13fc-4877-8d32-0e8d5f374e2b"], ["scheduled_at", "2024-11-22 21:51:01.145177"], ["finished_at", nil], ["concurrency_key", "[FILTERED]"], ["created_at", "2024-11-22 21:51:01.154060"], ["updated_at", "2024-11-22 21:51:01.154060"]]
  TRANSACTION (0.2ms)  SAVEPOINT active_record_1
  SolidQueue::Job Load (0.5ms)  SELECT "solid_queue_jobs".* FROM "solid_queue_jobs" WHERE "solid_queue_jobs"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  SolidQueue::ReadyExecution Create (3.3ms)  INSERT INTO "solid_queue_ready_executions" ("job_id", "queue_name", "priority", "created_at") VALUES ($1, $2, $3, $4) RETURNING "id"  [["job_id", 1], ["queue_name", "default"], ["priority", 0], ["created_at", "2024-11-22 21:51:01.191264"]]
  TRANSACTION (0.2ms)  RELEASE SAVEPOINT active_record_1
Enqueued TameJob (Job ID: ed7f2a0a-13fc-4877-8d32-0e8d5f374e2b) to SolidQueue(default)
↳ app/models/animal.rb:5:in `domesticate'
  TRANSACTION (0.2ms)  ROLLBACK
=> #<Animal:0x000000012d197cf0 id: nil, name: "Lion", created_at: "2024-11-22 21:51:01.131628000 +0000", updated_at: "2024-11-22 21:51:01.131628000 +0000">
>> Animal.count
  Animal Count (0.7ms)  SELECT COUNT(*) FROM "animals"
=> 0
>> SolidQueue::Job.count
  SolidQueue::Job Count (1.0ms)  SELECT COUNT(*) FROM "solid_queue_jobs"
=> 0