Router Implementation – Message Forwarding – Copy/Pass through
For greater flexibility our router can be something like a pass through router. If we are just calling a backend service then we can use a generic contract to receive and forward messages to the back end service as shown below.
Here we create a copy of the message to consume locally on the broker incase we want to validate some parts of the message or log etc. Ideally the fastest would be to just directly forward it over but application sometimes require all incoming messages to be logged or validated at the entry point of the DMZ.
Pros
- Loosely Coupling
- Potentially can avoid a lot of serialization and deserialization cost. (Encoders need to match for this)
- Changes in the backend usually do not require changes in the Broker provided the broker uses generic client contracts.
Cons
- Synchronous pattern has its overhead and not scalable.
- The router has to be of very high capacity to support high loads even though backend machines may block on IO.
Best Practices
- Match your encoders – Encoder mismatches can cause a heavy serialization and deserialization on the router. This is because any change between the backend and the frontend encoders on the binding would require re-encoding of the message and hence a full read and write at the encoder layer.
- Avoid having to create message copy. If the backend can create the copy/validate you save the routers CPU for more messages per second.
- Make sure you use fast message copy - Buffered messages have a very optimized message copy path that WCF would take provided you haven’t changed parts in the message. I will talk about how to hit this fast path next.
Next – How to Optimize Message Copying using CreateBufferedCopy?