Contact Free trial Login

Anypoint Exchange

Home to the complete listing of connectors, templates, examples and APIs.
Discover and use proven assets built by the MuleSoft ecosystem, or add assets to a private
tenant of Anypoint Exchange for collaboration and sharing of internal best practices.

Cache Scope with Fibonacci

Cache Scope with Fibonacci

(0 reviews)

Demonstrates the power of the cache scope with a Fibonacci function. When implementing this example, you can see how to deploy the cache scope within a Mule application. This also provides insights on caching strategies and how sub flows work within Mule.


This example solves the Fibonacci function F(n) for the n which is given as input by the user. Intermediate results are cached and later can be retrieved by the subsequent requests. The Finobacci sequence is a series of numbers in which the next number in the series is always the sum of the two numbers preceding it.

In this example, the Mule flow receives and performs two tasks for each request:

Executes and returns the answer to the Fibonacci equation (see below) using a number (n) provided by the caller
F(n) = F(n-1) + F(n-2) with F(0) = 0 and F(1) = 1.

Records and returns the cost of the calculation, wherein each individual invocation of a calculation task (that is, add two numbers in the sequence) adds 1 to the cost.

Set Up and Run this Example

  1. Open this Mule Example in Anypoint Studio.

  2. Once the application is running, go to your localhost server and type in the following request:


    You should get a response similar to what is shown below:

     Fibonacci(10) = 55
     COST: 11
  3. Now, change the value of n to 5.


    You should get a response similar to what is shown below:

     Fibonacci(5) = 5
     COST: 0

Note: The cost for calculating f(n) is 0. This is because f(5) has already been calculated and cached in the cache scope block while calculating f(10). If you try calculating f(10) again, the cost would be 0 as f(10) has already been calculated and cached.


Read full documentation in GitHub


Log into Anypoint Platform to download this asset


Created by
MuleSoft Organization
Published onJun 22, 2017


VersionRuntime version