<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Raja S on Medium]]></title>
        <description><![CDATA[Stories by Raja S on Medium]]></description>
        <link>https://medium.com/@rajas30?source=rss-ee6694f3e942------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*t70WvI5GbJEnkovNWRKsDA.png</url>
            <title>Stories by Raja S on Medium</title>
            <link>https://medium.com/@rajas30?source=rss-ee6694f3e942------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 16:50:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rajas30/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Utilizing Temporal Durable Execution to Build an Application-Level Service Bus or Message Broker]]></title>
            <link>https://rajas30.medium.com/utilizing-temporal-durable-execution-to-build-an-application-level-service-bus-or-message-broker-ba6e13a821ae?source=rss-ee6694f3e942------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba6e13a821ae</guid>
            <dc:creator><![CDATA[Raja S]]></dc:creator>
            <pubDate>Fri, 04 Oct 2024 21:25:18 GMT</pubDate>
            <atom:updated>2024-10-04T21:43:14.824Z</atom:updated>
            <content:encoded><![CDATA[<p>An <strong>application-level service bus</strong> is a communication layer that allows different components or services within an application to interact with each other in a decoupled and scalable manner. It facilitates the exchange of messages between these components, often enabling asynchronous communication, so that services don’t need to be directly aware of each other’s details (e.g., location or specific methods). This makes it easier to build loosely coupled systems, where individual services can be developed, deployed, and scaled independently.</p><p>On the other hand, a <strong>Message Broker</strong> is a simpler intermediary that primarily focuses on routing, translating, and distributing messages between producers and consumers. It is often used for asynchronous message delivery but does not usually offer the same level of service integration or advanced orchestration features as a Service Bus. Instead, a Message Broker ensures reliable message delivery and often manages point-to-point or publish-subscribe communication patterns.</p><h3>Popular Service Bus and Message Broker Implementations</h3><ul><li><strong>Azure Service Bus</strong>: A fully managed enterprise messaging service provided by Microsoft Azure, designed for cloud-based messaging.</li><li><strong>AWS Eventbridge</strong>: A fully managed event bus service that enables applications to connect using events, provided by AWS.</li><li><strong>Apache ActiveMQ</strong>: An open-source service bus that supports multiple messaging protocols and allows for reliable communication across distributed systems.</li><li><strong>RabbitMQ</strong>: A lightweight and widely-used messaging broker that is often used in microservice-based applications.</li><li><strong>IBM MQ</strong>: A widely-used enterprise messaging solution that ensures message delivery across multiple platforms.</li><li><strong>Kafka</strong> : Apache Kafka is a distributed, open-source streaming platform designed for high-throughput, fault-tolerant, real-time data processing. It is widely used for building real-time data pipelines and event-driven applications.</li></ul><p><strong>Choreography</strong>: A Service Bus or Message Broker typically follows a <strong>choreography-style architecture</strong>, where there is no central controller managing the interactions. Each service listens for events and responds independently, triggering other services as needed. In this setup, microservices are connected through the message broker, which facilitates the flow of events between them, allowing the services to interact asynchronously and maintain loose coupling.</p><p><strong>Orchestration: Temporal</strong> is an open-source, workflow orchestration engine that allows developers to build and manage complex, long-running business processes and microservices workflows in a reliable and scalable way. It abstracts the complexities of state management, retries, and error handling, enabling developers to focus on business logic rather than infrastructure concerns.</p><p><strong>Distributed System:</strong> In a typical distributed architecture, it’s common to implement a combination of both Choreography and Orchestration models to achieve the best of both worlds. Choreography is used to create event-driven systems, where services communicate through event chaining — each service listens for specific events and reacts autonomously without relying on a central controller. This approach enables services to be loosely coupled, promoting scalability and flexibility.</p><p>However, to manage more complex workflows or business processes, Orchestration is introduced. With Orchestration, a central controller (or orchestrator) coordinates the interactions between services, ensuring tasks are executed in a specific order and handling error recovery, retries, and state management. This model provides greater visibility and control over the flow of processes, which is beneficial for handling long-running workflows or tasks that require coordination across multiple services.</p><p>By integrating Choreography, which offers the advantages of event-driven, decentralized interactions, with Orchestration, known for its reliable and controlled workflow management, architects can design a resilient system that harnesses the strengths of both approaches. This hybrid model enables distributed systems to achieve high scalability while maintaining centralized oversight for complex workflows when required. Generally, Choreography is well-suited for inter-domain communication, while Orchestration is ideal for intra-domain interactions.</p><p>Below is a representative architecture diagram for a Hybrid system:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*WEGA_AGNS-wFu_7A" /></figure><p>This architecture can be implemented in various ways. In the example above, events are first submitted to a Service Bus (e.g., AWS EventBridge, Azure Service Bus), which acts as a router, forwarding messages to a Message Broker like SQS or Kafka. This triggers a series of microservices, each responsible for processing and enriching the message. As each microservice handles its part of the workflow, the message is progressively decorated with additional data or context. Eventually, the updated message is submitted back to the Service Bus, making it available for other systems to consume and act upon.</p><p>You can eliminate the need for an intermediate message broker by sending the message directly back to the service bus. With intelligent routing in place, the service bus can then route each incoming message to the appropriate microservice, streamlining the architecture and reducing complexity while maintaining effective communication between services. Something like the below diagram.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*fAi1jYTW-Xn8gvXT" /></figure><p>A hybrid system combining Choreography (event-driven) and Orchestration (centralized control) offers several high-level advantages:</p><p><strong>Flexibility and Scalability</strong>: Choreography allows services to act independently, making it easier to scale individual components without affecting the entire system. This enhances system flexibility and performance in distributed environments.</p><p><strong>Loose Coupling</strong>: The use of a Service Bus and Message Broker ensures that microservices are loosely coupled. Services don’t need to know about each other’s internal workings, promoting modularity and easier service updates.</p><p><strong>Improved Resilience</strong>: With distributed event processing, services are decoupled from direct dependencies, increasing system fault tolerance. Failures in one service won’t cascade, as other services can continue to operate.</p><p><strong>Enhanced Workflow Control</strong>: Orchestration adds a layer of control by managing complex workflows and ensuring that business rules and processes are consistently followed, especially in situations that require coordination across multiple services.</p><p><strong>Simplified Error Handling</strong>: Orchestrators can centralize error handling, making it easier to manage retries, timeouts, and recovery processes across multiple services.</p><p><strong>Real-time Event Processing</strong>: Choreography through the event bus enables real-time responses to changes in the system, allowing microservices to trigger actions based on relevant events dynamically.</p><p><strong>Improved Message Flow</strong>: The hybrid approach leverages the strength of message brokers to handle large-scale asynchronous communication efficiently while ensuring that orchestrated workflows follow the desired business logic.</p><p><strong>So, what are the challenges?</strong> Operating a hybrid system of this kind introduces significant complexity. It typically results in higher operational overhead, integration challenges, and the need for teams with a wide range of expertise. Debugging can also be particularly tricky due to the intricate interactions between components. While the system may appear loosely coupled from an architectural standpoint, making changes can be tightly constrained, requiring careful coordination across services to ensure stability and prevent cascading failures.</p><p><strong>Is there a way to achieve the best of both worlds? </strong>Can we design an architecture that simplifies operations, thereby addressing the challenges mentioned earlier, while still providing the advantages associated with a hybrid system? <strong>The answer is YES.</strong></p><p>Temporal is a durable execution platform that guarantees the immediate backup of messages in Cassandra DB upon receipt. This robust design ensures that messages are securely stored and can be reliably retrieved when needed. Similarly, a Message Broker also provides durable storage and reliable retrieval of messages. Consequently, we can replace a traditional Message Broker with a workflow in Temporal, allowing it to accept signals as messages to manage and transition the state effectively.</p><p><strong>Using Temporal Durable Execution as an Application level Service Bus:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*WiR9nA_c5ugFidu8" /></figure><p>In this scenario, the Parent Workflow functions as a message broker, initiated by the SignalWithStart method. This approach enables the Parent Workflow to either start a new workflow if one is not currently active or send a signal to an existing workflow. Additionally, any signals generated by the Workflow Activity or a Child Workflow can be fed back into the Parent Workflow while it is actively running and listening for messages. This allows for seamless communication and coordination between workflows, enhancing overall system responsiveness.</p><p><strong>Let’s look at some code: </strong>In this implementation, we define the MessageBrokerWorkflow interface, which includes multiple signal methods alongside a single workflow method. The primary purpose is to enable sending different types of signals for a specific entity (e.g., an individual user in the User entity) to either an existing workflow or initiate a new one. When a signal is received, the workflow triggers the corresponding actions based on the type of signal. The following is the implementation of this workflow.</p><p>Here all the different types of signals accept JsonNode as an input instead of strongly typed object. This allows us to be flexible with the schema and access the fields using standard Jackson core methods.</p><p><strong>MessageBrokerWorkflow.java</strong></p><pre><br>@WorkflowInterface<br>public interface MessageBrokerWorkflow {<br><br>  @WorkflowMethod<br>  void execute();<br><br>  @SignalMethod<br>  void signalA(JsonNode object);<br><br>  @SignalMethod<br>  void signalB(JsonNode object);<br><br>  @SignalMethod<br>  void signalC(JsonNode object);<br><br>  @SignalMethod<br>  void signalD(JsonNode object);<br><br>  @SignalMethod<br>  void signalE(JsonNode object);<br><br>  @SignalMethod<br>  void signalF(JsonNode object);<br><br><br>}<br><br></pre><p>WorkflowActivities.java</p><pre>@ActivityInterface<br>public interface WorkflowActivities {<br><br>  @ActivityMethod<br>  void activity1(JsonNode object);<br><br>  @ActivityMethod<br>  void activity2(JsonNode object);<br><br>  @ActivityMethod<br>  void activity3(JsonNode object);<br><br>  @ActivityMethod<br>  void activity4(JsonNode object);<br><br>  @ActivityMethod<br>  void activity5(JsonNode object);<br><br><br>}</pre><blockquote>The main difference between a traditional application-level service bus and using Temporal as a service bus is that Temporal offers workflow-level isolation. You can create workflows at either the entity or item level, but creating workflows at the entity level is not recommended. An entity-level workflow will quickly accumulate a large workflow history as it receives signals for all items within the entity. To avoid this, it’s best practice to create item-level workflows, ensuring each item has its own isolated workflow. This approach is essential when using Temporal as a Message Broker or Service Bus..</blockquote><pre>public class MessageBrokerWorkflowImpl implements MessageBrokerWorkflow {<br><br>  private final Logger logger = Workflow.getLogger(this.getClass().getName());<br><br>  private int processCount = 0;<br>  private final WorkflowActivities activities =<br>      Workflow.newActivityStub(<br>          WorkflowActivities.class,<br>          ActivityOptions.newBuilder().setStartToCloseTimeout(Duration.ofMinutes(10)).build());<br><br>  // Flag to indicate shutdown condition<br>  private boolean processingComplete = false;<br><br>  // Queue to hold incoming message objects for processing<br>  private final BlockingQueue&lt;JsonNode&gt; messageQueue = new LinkedBlockingQueue&lt;&gt;();<br><br>  // Shutdown time tracker, updated on each signal<br>  private long shutdownTime;<br><br>  public MessageBrokerWorkflowImpl() {<br>    // Initialize shutdown timer for 30 days. This is an arbitary number and<br>    // reducing it to lower time does have any impact on the functionality.<br><br>    resetShutdownTime();<br>  }<br><br>  @Override<br>  public void execute() {<br>    //Combination of this while and the messageQueue are responsible for<br>    //the workflow behaving like a Message Broker<br>   while (!processingComplete) {<br>      try {<br>        // Wait until a new message is available or shutdown time is reached<br>        Workflow.await(<br>            () -&gt; Workflow.currentTimeMillis() &gt;= shutdownTime || !messageQueue.isEmpty());<br><br>        if (Workflow.currentTimeMillis() &gt;= shutdownTime) {<br>          // Final check to see if there are any remaining messages in the queue<br>          if (!messageQueue.isEmpty()) {<br>            logger.info(&quot;Pending messages in the queue, resetting shutdown time.&quot;);<br>            resetShutdownTime(); // Reset shutdown timer since we still have messages<br>          } else {<br>            logger.warn(&quot;Workflow running for more than 30 days. Shutting down.&quot;);<br>            processingComplete = true;<br>            break;<br>          }<br>        }<br><br>        // Process the next lead in the queue<br>        JsonNode message = messageQueue.poll();<br>        if (message != null) {<br>          processMessage(message);<br><br>          // Reset the shutdown time since we processed a new signal<br>          resetShutdownTime();<br>        }<br><br>        logger.info(&quot;Process count is &quot; + processCount);<br>        if (processCount &gt; 10) {<br>          drainAllMessages();<br>          Workflow.continueAsNew();<br>        }<br><br>      } catch (Exception e) {<br>        logger.error(&quot;Error processing lead: &quot;, e);<br>        break; // Optional: break out on error, or handle differently<br>      }<br>    }<br><br>    // Perform any cleanup actions before shutting down the workflow<br>    logger.info(&quot;Workflow is shutting down.&quot;);<br>  }<br><br>  private void drainAllMessages() {<br>    logger.info(&quot;Draining all pending messages before continueAsNew...&quot;);<br>    JsonNode message;<br>    while ((message = messageQueue.poll()) != null) {<br>      processMessage(message);<br>    }<br>  }<br><br> ...<br><br>  // Helper method to reset shutdown time for another 7 days<br>  private void resetShutdownTime() {<br>    shutdownTime = Workflow.currentTimeMillis() + Duration.ofDays(7).toMillis();<br>  }<br><br>  ...<br>}</pre><p>The implementation above enqueues incoming signals objects into a message queue. This queue-based approach allows for immediate handling of messages while providing the flexibility to throttle or control the processing rate as needed, ensuring efficient workload management over time.</p><blockquote>When designing a Message Broker or Service Bus-style architecture with Temporal, it’s important to avoid creating a single workflow for an entire entity, like <em>User</em> or <em>Order</em>. Instead, you should create a separate workflow for each item within that entity. For example, instead of using a shared <em>MessageBrokerWorkflow</em> for all users, you&#39;d create distinct workflows for each user or order, such as <em>MessageBrokerWorkflow_&lt;UserID&gt;</em> or <em>MessageBrokerWorkflow_&lt;OrderID&gt;</em>. This ensures that each item within an entity has its own isolated Service Bus, improving scalability and performance<strong>.</strong></blockquote><blockquote>Events in the LinkedBlockingQueue are processed sequentially. When a workflow starts for an item and its activities are added to the queue, messages are handled one at a time. If an activity fails, it creates a blocking call that must be resolved before the next message can proceed. This blocking only impacts the specific item&#39;s workflow, while workflows for other items within the entity continue uninterrupted. In other words, processing is sequential at the item level but parallelized across the application. Unlike a traditional Service Bus, which would parallelize at both levels, you can address this by creating separate queues for each signal, rather than relying on a shared queue as in this implementation</blockquote><p>You’ll notice the processCount variable, which tracks the number of messages processed from the queue. Once a defined threshold is reached, this triggers continueAsNew to start a new workflow, helping to avoid exceeding Temporal&#39;s history size limit and ensuring long-running workflows run smoothly. Before invoking continueAsNew, it&#39;s important to ensure the queue is fully drained</p><pre>@Override<br>  public void signalA(JsonNode object) {<br>    messageQueue.offer(object);<br>    processCount++;<br>  }<br><br>  @Override<br>  public void signalB(JsonNode object) {<br>    messageQueue.offer(object);<br>    processCount++;<br>  }<br><br>  @Override<br>  public void signalC(JsonNode object) {<br>    messageQueue.offer(object);<br>    processCount++;<br>  }<br><br>  @Override<br>  public void signalD(JsonNode object) {<br>    messageQueue.offer(object);<br>    processCount++;<br>  }<br><br>  @Override<br>  public void signalE(JsonNode object) {<br>    messageQueue.offer(object);<br>    processCount++;<br>  }<br><br>  @Override<br>  public void signalF(JsonNode object) {<br>    messageQueue.offer(object);<br>    processCount++;<br>  }</pre><p><strong>ProcessMessage: </strong>The processMessage method is invoked for each incoming message for a given workflow, where the signal field is used to determine the signal type and trigger the corresponding activity. Instead of calling an activity, we could initiate a new child workflow at this stage, following the same logic. The underlying concept remains the same—mapping signals to specific tasks, whether they are activities or workflows. This signal field could be set at the signal method or in the client.</p><pre>private void processMessage(JsonNode messageObject) {<br>    try {<br>      String signal = messageObject.get(&quot;signal&quot;).asText();<br>      //Here you can add more rules based on the message and<br>      // even store those rules in an external JSON file.<br>      // This routing is similar to how Service Bus route filtering works.<br>      switch (signal) {<br>        case &quot;signalA&quot;:<br>          activities.activity1(messageObject);<br>          //Add more activites are call child workflow here if needed<br>          break;<br>        case &quot;signalB&quot;:<br>          activities.activity2(messageObject);<br>          //Add more activites are call child workflow here if needed<br>          break;<br>        case &quot;signalC&quot;:<br>          activities.activity3(messageObject);<br>          //Add more activites are call child workflow here if needed<br>          break;<br>        default:<br>          logger.warn(&quot;Unknown signal type received: &quot; + signal);<br>      }<br>    } catch (Exception e) {<br>      logger.error(&quot;Error processing messageObject object: &quot;, e);<br>    }</pre><p><strong>Route Rules: </strong>You can implement more complex rules alongside Signals to enable comprehensive message routing, similar to a Service Bus. Alternatively, you can keep the architecture simple and focus on basic message handling. This approach follows a classic Pub-Sub model, where a message is published to the workflow, triggering the corresponding Activity or child workflow. The triggered activity can handle any task you need, offering great flexibility in defining your processes.</p><p>This is a sample starter code for generating a SignalWithStart event without a Worker.</p><p><strong>SignalA.java</strong></p><pre>public class SignalA {<br><br>  static final String TASK_QUEUE = &quot;MessageBrokerTaskQueue&quot;;<br><br>  public static void main(String[] args) throws Exception {<br>    // Load your client certificate, private key, and setup SSL context<br>     ...<br>    // Create SSL context<br>     ...<br>   <br>    // Setup WorkflowServiceStubs and WorkflowClient<br>    ...<br><br>    // Create sample message data as a Map<br>    Map&lt;String, Object&gt; sampleFields = new HashMap&lt;&gt;();<br>    sampleFields.put(&quot;firstName&quot;, &quot;John&quot;);<br>    sampleFields.put(&quot;lastName&quot;, &quot;Doe&quot;);<br>    sampleFields.put(&quot;email&quot;, &quot;john.doe@example.com&quot;);<br>    sampleFields.put(&quot;address1&quot;, &quot;123 Main St&quot;);<br>    sampleFields.put(&quot;address2&quot;, &quot;Apt 4B&quot;);<br>    sampleFields.put(&quot;phoneNumber&quot;, &quot;+1-555-123-4567&quot;);<br>    sampleFields.put(&quot;city&quot;, &quot;New York&quot;);<br>    sampleFields.put(&quot;state&quot;, &quot;NY&quot;);<br>    sampleFields.put(&quot;postalCode&quot;, &quot;10001&quot;);<br>    sampleFields.put(&quot;country&quot;, &quot;USA&quot;);<br>    sampleFields.put(&quot;messageId&quot;, UUID.randomUUID().toString());<br>    sampleFields.put(&quot;signal&quot;, &quot;signalA&quot;);<br>    // Additional rules can be added here that can be used for routing<br>    // Convert Map to JsonNode<br>    ObjectMapper objectMapper = new ObjectMapper();<br>    JsonNode messageJsonNode = objectMapper.valueToTree(sampleFields);<br><br>    // Define workflow options<br>    String workflowId =<br>        &quot;MessageBroker_&quot; + sampleFields.get(&quot;email&quot;).toString(); // Use email as workflowId<br><br>    String signal = sampleFields.get(&quot;signal&quot;).toString(); // Use email as workflowId<br>    WorkflowOptions workflowOptions =<br>        WorkflowOptions.newBuilder()<br>            .setWorkflowId(workflowId)<br>            .setTaskQueue(TASK_QUEUE)<br>            .setWorkflowTaskTimeout(Duration.ofSeconds(15))<br>            .build();<br><br>    // Use signalWithStart to either start the workflow or send a signal if it&#39;s already running<br>    MessageBrokerWorkflow workflow =<br>        client.newWorkflowStub(MessageBrokerWorkflow.class, workflowOptions);<br><br>    try {<br>      System.out.println(&quot;Executing or Signaling Workflow...&quot;);<br><br>      // Pass the JsonNode as arguments<br>      WorkflowStub.fromTyped(workflow)<br>          .signalWithStart(<br>              signal, // Name of the signal method<br>              new Object[] {messageJsonNode}, // Arguments for the signal method<br>              null);<br><br>    } catch (Exception e) {<br>      System.err.println(&quot;Error executing or signaling workflow: &quot; + e.getMessage());<br>    }<br>  }<br>}</pre><p><strong>Temporal UI</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PFujdwB5s8wTRf4DwR21wQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BfJ_kVaPYekmu7btGt62Lw.png" /></figure><p>You can see that different signals are received at different times and the workflow acts like a Service bus where it looks at the message and calls the corresponding activity or child workflow.</p><p>This workflow was created based on the User journey and is unique per user.</p><blockquote><strong>Conclusion</strong>: This architecture may not be suitable for every use case where a traditional message broker is typically employed. If you require parallel processing at the individual signal level, this solution might not be the best fit. In that case, you might want to create a Signal level queue. However, it becomes a compelling choice if your goal is to streamline your system by consolidating multiple technologies into a single platform. This approach reduces complexity while still meeting core messaging requirements. Additionally, one of the greatest benefits I’ve seen is the considerable time saved in building a distributed system — without compromising on functionality or reliability</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba6e13a821ae" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>