Single Page Application — With Worker Process
What is Worker Process
A worker process is a type of computer process that runs in the background and performs tasks as assigned by a main process. It is typically used to offload tasks from a main process, allowing the main process to continue with other operations while the worker process handles the task in the background.
This article constitutes a component of a comprehensive collection of web architecture pattern references.
Single Page Application Frontend
Backend for SPA and Mobile App (Managed Services)
Backend for SPA and Mobile App (Serverless)
Multi Page Application (Integrated Web)
Single Page Application with Worker Process
How it works
In cloud computing, worker processes can be implemented using messaging or queues. The main process adds tasks to a queue, and worker processes monitor the queue for new tasks. When a worker process finds a new task in the queue, it takes the task and performs the work. This ensures that tasks are performed in the order in which they were received and that multiple worker processes can operate in parallel to handle a large number of tasks.
Examples of messaging systems used for worker process implementation in the cloud include RabbitMQ, Apache Kafka, and Amazon Simple Queue Service (SQS). The messaging system provides a way for the main process to communicate with the worker processes, allowing tasks to be dispatched and results to be returned.
Components
The main components of a worker process can include:
-
Task queue: A task queue is used to store the tasks that need to be performed. The worker processes monitor the queue for new tasks.
-
Task dispatcher: The task dispatcher is responsible for allocating tasks to the worker processes. It ensures that tasks are dispatched to the worker processes in a fair and efficient manner.
-
Worker processes: The worker processes are the actual processes that perform the tasks. They receive tasks from the task queue, perform the work, and return the results.
-
Result storage: A result storage mechanism is used to store the results of the tasks performed by the worker processes. This can be a database, a file system, or another type of data storage.
-
Error handling: A worker process should have error handling mechanisms in place to deal with unexpected errors that may occur during task processing. This can include retrying failed tasks or logging the error for later review.
-
Monitoring and reporting: A monitoring and reporting system is used to monitor the performance of the worker processes and provide feedback to the main process. This can include information about the number of tasks processed, the processing time for each task, and any errors that may have occurred.
Architecture
There are multiple architecture patterns to implement a Worker Process, and those are totally dependant on use cases.
Use Case 1
An example use case can be sending newsletters to all subscribed users at 10 AM.
- Application has to execute some long running jobs on specific intervals.
- Application should not use any compute resources if it’s not executing any jobs
- Jobs should be triggered based some external or internal events or from schedulers
In this use case we can utilize AWS EventBridge Scheduler to trigger the jobs on specific intervals. AWS ECS Tasks can be used to execute the jobs, and once the job is done these tasks will end the execution and de-allocate the compute resources. We don’t require any Messaging/Queue services in this case(will cover that in the next use case).
The following diagram explains the architecture of the worker process along with other important components.
Use Case 2
An example use case can be sending registration welcome messages to users after their successful registration on the platform
- After completing the registration process, a message object with userId will be pushed to a Queue(eg: SQS).
- A Lambda configured as an SQS trigger will be triggered and send the email to the user.
- Update database if required.
- As lambda is a serverless component, the costing will be calculated based on number of executions and time.
The following diagram explains the architecture of the worker process along with other important components.