Understanding the Fan-Out/Fan-In API Integration Pattern

The fan-out/fan-in pattern is a staple of more advanced API integrations. Let's check out in practice how, with Zato, it can simplify asynchronous communication across applications that do not always exhibit the same kind of availability or performance characteristics.

What Is Fan-Out/Fan-In?

Fan-out / fan-in integration pattern

The core idea is this:

The above concludes the initial phase and Zato can now carry out the rest of the work:

Now, the data collecting services and data sources do their job. What it means is purely up to a given integration process. Perhaps some SQL databases are consulted, maybe Cassandra queries run, and maybe a list of REST endpoints is invoked; any protocol or data format can be used.

Thus, we can see why it is called fan-out/fan-in:

When to Use It

The pattern can be considered each time there is a rather long-running integration process that requires communication with more than one system.

This raises a question of what “long-running” is. The answer lies in whether there is a human being waiting for the results. Generally, people can wait 1-2 seconds for a response before growing impatient. Hence, if the process takes 30 seconds or more, they should not be made to wait as this can only lead to dissatisfaction.

Yet, even if there is no real person for the result, it is usually not a good idea to wait for results too long as that is a blocking process and blocking may mean consuming up resources unnecessarily, which in turn may lead to wasted CPU and, ultimately, energy.

This kind of process is found when the data sources are uneven in the sense that some or all of them may use different technologies or because they may each return responses with different delays. For instance:

Another trait of such processes is that the parallel paths are independent. If they are separate and each potentially takes a significant amount of time, then we can just as well run them in parallel rather than serially, saving time from the caller's perspective.

How Does It Look in Python?

An example service using the pattern may look like below:

Note that this particular code happens to use AMQP in the callback service but this is just for illustration purposes and any other technology can be used just as well.

Python
 




x
36


 
1
from zato.server.service import Service
2
 
          
3
class GetUserScoringCredit(Service):
4
 
          
5
    def handle(self):
6
 
          
7
        user_id = self.request.input.user_id
8
 
          
9
        # A dictionary of services to invoke along with requests they receive
10
        targets = {
11
            'crm.get-user': {'user_id':user_id},            
12
            'erp.get-user-information': {'USER_ID':user_id},            
13
            'scoring.get-score': {'uid':user_id},       
14
        }
15
 
          
16
        # Invoke both services and get the CID
17
        cid = self.patterns.fanout.invoke(targets, callbacks)
18
 
          
19
        # Let the caller know what the CID is
20
        self.response.payload = {'cid': cid}
21
 
          
22
class GetUserScoringCreditAsyncCallbac(Service):
23
    def handle(self):
24
        # AMQP connection to use
25
        out_name = 'AsyncResponses'
26
 
          
27
        # AMQP exchange and routing key to use
28
        exchange = '/exchange'        
29
        route_key = 'zato.responses'
30
 
          
31
        # The actual data to send - we assume that we send it
32
        # just as we received, without any transformations.
33
        data = self.request.raw_request
34
 
          
35
        self.outgoing.amqp.send(data, out_name, exchange, route_key)
36
 
          


More Options

Be sure to check details of the pattern in the documentation to understand what other options and metadata are available for your services.

In particular, note that there is another type callback not shown in this article. The code above has only one final callback when all of the data is already available. But what if you need to report the progress each of the sub-tasks completed? This is when per-invocation callbacks come in handy; check the samples in the documentation for more information.

More Patterns

Fan-out/fan-in is just one of the patterns that Zato ships with. The most prominent ones are listed below.

Learn more

Are you interested in learning more about enterprise API integrations in Python? You can start out by visiting the Zato main page, familiarizing yourself with the extensive documentation or by jumping straight into the first part of the tutorial.

Be sure to visit our Twitter, GitHub, and Gitter communities too!

 

 

 

 

Top