We have updated the content of our program. To access the current Software Engineering curriculum visit curriculum.turing.edu.
Background Workers
Background Workers
Warm Up
- What does it mean for a process to execute asynchronously?
- Why would we want something to execute asynchronously? What advantage does that give us?
Doing Stuff In the background
A background worker allows you to perform time consuming operations outside of the normal request-response loop.
-
Long-running requests are moved to threads outside of the main web application thread for processing,
-
This frees up the main web application to continue handling requests from the user.
Why do we need them?
-
Certain processes can take a long time and users get bored
-
Background workers are a way to optimize the perceived performance of your web application.
What’s good to do in the background?
There are 2 main attributes that make something a good candidate for processing in the background:
- Slow
- Immediate user feedback is not crucial
Things In Rails Apps That Need Background workers
- Data Processing - Generating Reports
- Payment processing
- Maintenance - expiring old sessions, sweeping caches
Our Tools
- Redis - Key/value store database
- Sidekiq - Background processor designed to work with rails
- ActiveJob - Rails framework to interface with background processors
How it all works together
- Rails sends jobs to Redis
- Sidekiq checks Redis for new jobs
- Sidekiq executes the jobs it finds in Redis using code defined in Rails
Turn to a neighbor and practice explaining how this works
Questions?
Workshop
Wrap up Questions
- When do you want to use a background worker?
- What is ActiveJob and how is it similar to ActiveRecord
- What is Sidekiq?
- What is Redis?
- Is it possible to define a job that doesn’t get sent redis and a background worker?