Question: How to store the temporary data in Apache storm?

In storm topology, bolt needs to access the previously processed data.

```
Eg: if the bolt processes varaiable1 with result as 20 at 10:00 AM.

```

and again `varaiable1` is received as `50` at `10:15 AM` then the result should be `30 (50-20)` 

later if varaiable1 receives `70` then the result should be `20 (70-50)` at `10:30`.

How to achieve this functionality.

Answer: 
In short, you wanted to do micro-batching calculations with in storm’s running tuples.
First you need to define/find key in tuple set. 
Do field grouping(don't use shuffle grouping) between bolts using that key. This will guarantee related tuples will always send to same task of downstream bolt for same key.
Define class level collection List/Map to maintain old values and add new value in same for calculation, don’t worry they are thread safe between different executors instance of same bolt.
This question is a good candidate to demonstrate Apache Spark's in memory computation over the micro batches. However, your use case is trivial to implement in Storm.

1. Make sure the bolt uses fields grouping. It will consistently hash the incoming tuple to the same bolt so we do not lose out on any tuple.
2. Maintain a Map<String, Integer> in the bolt's local cache. This map will keep the last known value of a "variable".

```java
    class CumulativeDiffBolt extends InstrumentedBolt{

    Map<String, Integer> lastKnownVariableValue;

    @Override
    public void prepare(){
         this.lastKnownVariableValue = new HashMap<>();
         ....

    @Override
    public void instrumentedNextTuple(Tuple tuple, Collector collector){
         .... extract variable from tuple
         .... extract current value from tuple
         Integer lastValue = lastKnownVariableValue.getOrDefault(variable, 0)
         Integer newValue = currValue - lastValue

         lastKnownVariableValue.put(variable, newValue)
         emit(new Fields(variable, newValue));
       ...
    }

```