Why Background Processing Handles Heavy Tasks in NestJS
📖 Scenario: You are building a NestJS application that sends welcome emails to new users. Sending emails can take time and slow down the app if done directly during user registration.To keep the app fast and responsive, you will use background processing to handle the email sending task separately.
🎯 Goal: Build a simple NestJS service that queues email sending tasks to a background processor instead of sending emails immediately during user registration.This shows how background processing helps handle heavy tasks without blocking the main app flow.
📋 What You'll Learn
Create a list of new users with their email addresses
Add a configuration variable to set the maximum number of emails to send at once
Use a loop to queue email sending tasks for each user
Add a final method call to start processing the queued email tasks
💡 Why This Matters
🌍 Real World
Background processing is used in real apps to handle slow tasks like sending emails, processing images, or generating reports without slowing down the user experience.
💼 Career
Understanding background processing is important for backend developers to build scalable and responsive applications using NestJS or similar frameworks.
Progress0 / 4 steps
1
Create the list of new users
Create a constant array called newUsers with these exact objects: { id: 1, email: 'alice@example.com' }, { id: 2, email: 'bob@example.com' }, and { id: 3, email: 'carol@example.com' }.
NestJS
Hint
Use const newUsers = [ ... ] with objects inside the array.
2
Add max emails per batch configuration
Create a constant called maxEmailsPerBatch and set it to 2 to limit how many emails are sent at once.
NestJS
Hint
Use const maxEmailsPerBatch = 2; to set the batch size.
3
Queue email sending tasks for each user
Use a for loop with const user of newUsers to iterate over newUsers. Inside the loop, call emailQueue.add(user.email) to queue sending an email to each user's email address.
NestJS
Hint
Use for (const user of newUsers) { emailQueue.add(user.email); } to queue emails.
4
Start processing the queued email tasks
Call emailQueue.process(maxEmailsPerBatch) to start sending emails in batches of maxEmailsPerBatch.
NestJS
Hint
Call emailQueue.process(maxEmailsPerBatch); to start sending emails.
Practice
(1/5)
1. Why does NestJS use background processing for heavy tasks like sending emails or processing files?
easy
A. To make the app slower so users wait longer
B. To keep the main app fast and responsive by running heavy tasks separately
C. To avoid using any decorators in the code
D. To run all tasks at the same time in the main thread
Solution
Step 1: Understand app responsiveness
Heavy tasks like sending emails can slow down the app if run directly in the main thread.
Step 2: Role of background processing
Background processing runs these heavy tasks separately, so the main app stays fast and responsive.
Final Answer:
To keep the main app fast and responsive by running heavy tasks separately -> Option B
Quick Check:
Background processing = keeps app fast [OK]
Hint: Background tasks run separately to keep app fast [OK]
Common Mistakes:
Thinking background tasks slow down the app
Believing all tasks run in the main thread
Ignoring the use of decorators for background jobs
2. Which NestJS decorator is used to mark a class as a background job processor?
easy
A. @Process
B. @Controller
C. @Processor
D. @Injectable
Solution
Step 1: Identify decorators for background jobs
In NestJS, @Processor marks a class that handles background jobs.
Step 2: Differentiate from other decorators
@Process marks methods inside the processor, @Controller is for HTTP routes, and @Injectable is for services.
Final Answer:
@Processor -> Option C
Quick Check:
@Processor = class for background jobs [OK]
Hint: Use @Processor for job classes, @Process for job methods [OK]
Common Mistakes:
Confusing @Processor with @Process
Using @Controller for background jobs
Forgetting to use any decorator
3. Given this NestJS background processor code, what will happen when a job named 'sendEmail' is added?
A. Missing async keyword before handleHeavyTask method
B. @Processor decorator is used incorrectly
C. Job parameter is missing type annotation
D. doHeavyWork should not be awaited
Solution
Step 1: Check method using await
The method handleHeavyTask uses 'await' but is not marked async, which causes a syntax error.
Step 2: Confirm correct fix
Adding 'async' before the method fixes the error, allowing await to work properly.
Final Answer:
Missing async keyword before handleHeavyTask method -> Option A
Quick Check:
await needs async method [OK]
Hint: Use async before methods that await promises [OK]
Common Mistakes:
Ignoring async keyword with await
Thinking @Processor is wrong here
Assuming doHeavyWork can't be awaited
5. You want to process image uploads in the background to avoid slowing down user requests. Which approach best uses NestJS background processing to achieve this?
hard
A. Create a @Processor class with a @Process method that resizes images asynchronously
B. Resize images directly inside the controller handling uploads
C. Use @Controller with @Get to process images synchronously
D. Run image processing code in the main thread without decorators
Solution
Step 1: Identify background processing use case
Image resizing is a heavy task that can slow down user requests if done synchronously.