![](https://static.wixstatic.com/media/9a3369_aa15e9e2c90341e18ba0373dfc0ada3d~mv2.png/v1/fill/w_980,h_447,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/9a3369_aa15e9e2c90341e18ba0373dfc0ada3d~mv2.png)
I was working on a problem statement where the entire solution ecosystem was designed using microservice framework architecture. All services are independent of each microservice and have their own data storage. Message queues are used for inter-service communications.
I was researching the web trying to find a solution to a problem where I wanted a microservice to act as an interceptor between two other Microservices and perform data transformation/manipulation and then pass it on for further consumption to other microservice. There also should be an option of switching this transformation microservice on or off if required. So basically if I want transformation, my transformation service should transform the data, if this service is off there should not be any transformation on data.
In order to provide a glimpse of the problem statement, let me share the below diagram on how our services were communicating with each other before the transformation service.
![](https://static.wixstatic.com/media/9a3369_6f56916f4ae145efa4fd924161638f5d~mv2.png/v1/fill/w_653,h_272,al_c,q_85,enc_auto/9a3369_6f56916f4ae145efa4fd924161638f5d~mv2.png)
As you can see, Microservice A performs some tasks and upon completion, it sends an alert to a message queue. Microservice B is the consumer of this message which eventually processes the message further.
Now as per the requirement, I want this message to be consumed by an interceptor microservice, performs some processing/data massage, and then eventually forwards this processed information for final consumption of Microservice B. However this processing should be performed only if the interceptor microservice is up and running. If no such service is running, then the message should be consumed by Microservice B as usual.
So, in short, we were looking for a service that acts as an interceptor however this should be dynamic and absolutely no level of configurations should be performed on Service A and Service B.
This is a typically interceptor design pattern, however, if you try researching this on the web, you will end up getting examples of how a class can act as an interceptor if two classes or two components are communicating with each other.
Finally, after giving it a try on the web, I thought of implementing this using my own approach.
This approach may not be very efficient and would request the readers to provide their inputs if they find an approach better than this. The only consideration is that I don't want to use an Orchestration service because of obvious reasons.
The below diagram provides oversight of the solution I implemented in order to solve the above problem
![](https://static.wixstatic.com/media/9a3369_35304e216dca4b5ca1a6b57e083e6413~mv2.png/v1/fill/w_704,h_271,al_c,q_85,enc_auto/9a3369_35304e216dca4b5ca1a6b57e083e6413~mv2.png)
Below are the steps that I performed
No changes are required with respect to Microservice A
Microservice B will continue to consume the event, however with a low consumer priority value. We changed the consumer priority to a value like 10
Interceptor Microservice will also consume the same event, however, this service has a higher consumer priority, say 20
Once both the consumers are up and running, the priority consumers will get the opportunity to digest the message which is Interceptor service in our case.
Once consumed it will do the necessary data manipulation and publishes a new event with a convention of <original event>_processed. For example, if your original queue name was orderreceived, this new queue will be named as orderreceived_processed
The only change with microservice B will be that it will now listen to both orderreceived event as well as orderreceived_processed event and process messages from both the events in the same manner (assuming the entire payload is ingested)
That's it, now If the interceptor service is up and running, it will consume the message and transform the data and if this service is not running, everything will work as was working before as if no interceptor was ever-present.
Please comment on your thought process or suggestions if you have a better option available in order to solve this use case.
Happy Reading...
Comentarios