- Introduction
- Definition
- Examples
- Links
The modern application developer has undoubtedly dealt with the problem of process execution locality. More precisely, when responding to HTTP requests, developers must carefully balance what work is to be done in the process handling the request and the work that can be done in a process outside of the HTTP request. The Worker Pattern exists to help strike such a balance.
In this article, we will define the pattern and look at several applications of the pattern. However, before we dive in, a quick note on how to successfully grok this material. The fundamental idea of the Worker Pattern is and should always be simple. Much like mathematical techniques, the definition is quite simple and the application is not alway clear. However, with sufficient exposure to examples and with enough practice, the pattern becomes a reflex for the application developer.
Processor: Something that can execute instructions. Not a physical processor, thread, coroutine, etc...
Group of Computation: One or many atomic instructions.
The Worker Pattern: To divide a group of computations amongst a set of processors.
A web service that requires high throughput will undoubtedly need to ensure low latency while processing requests. In other words, the process that is serving HTTP requests should spend the least amount of time possible to serve the request. Subsequently if the server does not have all of the data necessary to properly respond to the request, it must not wait until the data is found. Instead it must let the client know that it is working on the fulfilment of the request and that the client should check back later. Such an arrangement will guarantee that our web servers are always available to respond to requests with low latency.
The application of the Worker Pattern in this case involves moving the fulfilment of the request to another process. Leaving the server process free to respond to other requests. Let us know explore the algorithm:
HTTP Server
receive request
look in cache for data to satisfy request
if data in cache
respond to request with cached data
else
if cache contains key=request_signature
respond to request with nothing. client should retry request
else
set cache with *key=request_signature* *value=NULL*
enqueue a job to fetch data
respond to request with nothing. client should retry request
end
end
Worker
look in queue for work
if work
process work
save process result
decode request_signature
set cache *key=request_signature* *value=process_result*
end