Skip to content

Add Fiber-Based Non-Blocking IO Worker Support - #21

Merged
jujustayfly merged 20 commits into
mainfrom
run-on-fiber-scheduler
Aug 4, 2025
Merged

Add Fiber-Based Non-Blocking IO Worker Support#21
jujustayfly merged 20 commits into
mainfrom
run-on-fiber-scheduler

Conversation

@jujustayfly

@jujustayfly jujustayfly commented Jun 30, 2025

Copy link
Copy Markdown
Contributor

Add async execution support with fiber-based concurrency

Feature Overview:
Adds hybrid execution mode combining threads with fibers for higher concurrency on IO-bound jobs. Jobs can now yield during database queries, HTTP requests, and other IO operations, allowing other jobs to execute concurrently within the same thread.

Key Features:

  • Hybrid architecture: Multiple threads each running multiple fibers (e.g., 4 threads × 10 fibers = 40 concurrent jobs)
  • Async-compatible jobs: Jobs using async gems automatically benefit from non-blocking IO
  • Automatic fiber scheduling: PostgreSQL queries, HTTP requests, and file operations yield control when using fiber scheduler
  • Backward compatibility: Existing jobs run unchanged in thread-only mode

Configuration:

Gouda.configure do |config|
  config.use_fibers = true
  config.fibers_per_thread = 10
  config.worker_thread_count = 4
end

Performance Benefits:

  • IO-bound jobs: 10x+ throughput improvement for jobs with database queries, API calls, file operations
  • CPU-bound jobs: No performance penalty (can disable fibers per queue)
  • Memory efficient: Fibers use ~4KB vs threads at ~8MB each

Safety Features:

  • PostgreSQL isolation level checking: Prevents segfaults with incorrect Rails configuration
  • Graceful fallback: Jobs work in both fiber and thread modes
  • Per-job execution tracking: Clear identification of fiber vs thread execution context

Implementation:

  • Single Worker class supporting both execution modes
  • Unified sleep/polling mechanism for threads and fibers
  • Consolidated test suite and documentation

@jujustayfly
jujustayfly marked this pull request as ready for review July 3, 2025 09:25
@jujustayfly
jujustayfly requested review from julik and svanhesteren July 3, 2025 09:25
@jujustayfly jujustayfly changed the title Add Configuration to run Gouda in Fiber Scheduler (Async Mode) Add Fiber-Based Non-Blocking IO Worker Support Jul 3, 2025

@julik julik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Epic stuff! It does feel like we could merge the two workers and have a wrapper which either uses Async or not, and also sets the appropriate isolation level - instead of having two separate worker modules.

Can't wait to try this with the sync job!

Comment thread lib/gouda/fiber_worker.rb Outdated
Comment thread lib/gouda/fiber_worker.rb Outdated
Comment thread lib/gouda/workload.rb Outdated
Comment thread test/gouda/fiber_worker_test.rb Outdated
Comment thread examples/fiber_configuration.rb Outdated
Comment thread examples/async_job_example.rb Outdated
Comment thread examples/fiber_configuration.rb Outdated
@jujustayfly

Copy link
Copy Markdown
Contributor Author

@julik Thanks for the insight! Implemented all your suggestions. Feel free to review again or we can discuss the last details on a call

Otherwise we lose thread/fiber information and I cannot see any reason why this is being overwritten in the first place

@svanhesteren svanhesteren left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey cool stuff! I like the current implementation. Sorry it took so long! Since you tested this thoroughly I assume the comments I made are not an issue (yet).

I like the idea of running both kind of workers for different kind of jobs. This gives us a bit more control in optimizing for certain jobs. Are there any jobs we have currently that would benefit from sticking to classic thread-only execution?

Comment thread lib/gouda/worker.rb Outdated
Comment thread lib/gouda/worker.rb Outdated
Comment thread examples/async_job_example.rb
@jujustayfly
jujustayfly merged commit 80da92a into main Aug 4, 2025
3 checks passed
@jujustayfly
jujustayfly deleted the run-on-fiber-scheduler branch August 4, 2025 09:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants