Elastic Beanstalk offers both a
Web tier and a
Worker tier. This allows developers to build reasonably complex applications without having to maintain moving pieces. Offloading heavy-duty workloads to the worker in order to keep the web tier responsive is as easy as putting a message on a queue.
One annoyance that I have with
Beanstalk is that there is no way to direct a message to a specific endpoint, hence leaving a single endpoint the responsibility of distributing the messages to all their handlers and potentially leading to brittle code. But it doesn’t have to be that way.
SQS messages have attributes, attributes can be set by the sender and are read by the receiver. The idea is to use a known attribute to attach routing metadata to the message.
Constants are the base of any decently built
C# application. I did not want to depart from to this rule and hence added some constants:
These constansts will be used to add routing metadata to the
We can then define our routes via some more constants:
Note: those two
class will have to be referenced by the sender and the
Sending the message
The sender will most likely be the
Web tier but it could be any system being able to send a message to a
Worker, the routing is implemented via a
Note: don’t forget to add
HeaderRoutingMiddleware to the
The last piece of the puzzle is defining the expected route on the
Simple Routing in production over the last few months and am now confident that it does what it’s supposed to do. This is why I decided to release it under a
MIT license to allow others to benefit from my work.
Simple Routing is available:
NuGetas the package BeanstalkWorker.SimpleRouting
- As source on
- The implementation is so simple that you can just copy the classes into your own solution if that works better for you
Simple Routing solution contains a
SampleWeb app, you can either:
- Send “work” -
- Send “nothing” -
Peek at the messages
Now let’s look at the messages in the
Handle the messages
SampleWorker app. When running in
ElasticBeanstalk the Sqsd daemon reads
SQS messages from the
SQS queue and
POST the content to your
Worker. But we’re running the
Worker on our machine and the
Sqsd daemon is not available. This is why I wrote
Beanstalk Seeder emulates the
SQS Daemonsurrounding an
Worker Tierso that you can replicate the interaction between a
Web Tierand a
Worker Tieron your machine.
I wrote a detailed guide in the GitHub repository. Give it a try and let me know if it works for you.