title: reactor stream - overwrite old value, keep only latest
body: <p>At the moment I am trying to implement a standard reactive case with Project Spring Reactor: The producer is faster than the consumer. The consumer should never work with old values if new ones are already available (example: outdated stock prices are not of interest).</p>
  <p>In my code example I have a <strong>producer that generates a new value every 100ms</strong>.  But the <strong>consumer needs 500ms</strong> for processing. Since between processing in the consumer already many several new values arise, only the newest values for the consumer/subscriber would be interesting for me and not the outdated intermediate values.</p>
  <p>Per <strong>limitRate(1)</strong> I tried to request only one value at a time to the producer and per <strong>onBackPressureLatest()</strong> I wanted to ignore intermediate values. Both did not work as desired.</p>
  <p>What would be the correct solution?</p>
  <pre><code>@Test
  void fluxTest(){
      
      Flux&lt;Integer&gt; flux = Flux.generate(AtomicInteger::new, (ai, sink) -&gt; {
  
          int i = ai.incrementAndGet();
  
          if (i &gt; 10) {
              sink.complete();
          } else {
              System.out.println(Thread.currentThread()+&quot;: generate &amp; emit value &quot;+i);
              sink.next(i);
          }
          sleep(100);
          return ai;
      });
  
      flux
              .publishOn(Schedulers.parallel())
              .onBackpressureLatest()
              .limitRate(1)
              .subscribe(i -&gt; {
                  System.out.println(Thread.currentThread()+&quot;: Receive: &quot; + i); // do something with generated and processed item
                  sleep(500);
              });
  
      sleep(10000);
  }
  
  void sleep(int ms){
      try {
          Thread.sleep(ms);
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  <p>Current result:</p>
  <pre><code>14:26:40.019 [main] DEBUG reactor.util.Loggers - Using Slf4j logging framework
  Thread[main,5,main]: generate &amp; emit value 1
  Thread[parallel-1,5,main]: Receive: 1
  Thread[main,5,main]: generate &amp; emit value 2
  Thread[main,5,main]: generate &amp; emit value 3
  Thread[main,5,main]: generate &amp; emit value 4
  Thread[main,5,main]: generate &amp; emit value 5
  Thread[parallel-1,5,main]: Receive: 2
  Thread[main,5,main]: generate &amp; emit value 6
  Thread[main,5,main]: generate &amp; emit value 7
  Thread[main,5,main]: generate &amp; emit value 8
  Thread[main,5,main]: generate &amp; emit value 9
  Thread[main,5,main]: generate &amp; emit value 10
  Thread[parallel-1,5,main]: Receive: 3
  Thread[parallel-1,5,main]: Receive: 4
  Thread[parallel-1,5,main]: Receive: 5
  Thread[parallel-1,5,main]: Receive: 6
  Thread[parallel-1,5,main]: Receive: 7
  Thread[parallel-1,5,main]: Receive: 8
  Thread[parallel-1,5,main]: Receive: 9
  Thread[parallel-1,5,main]: Receive: 10
  
  Process finished with exit code 0
  </code></pre>
  <p>My <strong>expected</strong> result would be:</p>
  <pre><code>14:26:40.019 [main] DEBUG reactor.util.Loggers - Using Slf4j logging framework
  Thread[main,5,main]: generate &amp; emit value 1
  Thread[parallel-1,5,main]: Receive: 1
  Thread[main,5,main]: generate &amp; emit value 2
  Thread[main,5,main]: generate &amp; emit value 3
  Thread[main,5,main]: generate &amp; emit value 4
  Thread[main,5,main]: generate &amp; emit value 5
  Thread[parallel-1,5,main]: Receive: 5
  Thread[main,5,main]: generate &amp; emit value 6
  Thread[main,5,main]: generate &amp; emit value 7
  Thread[main,5,main]: generate &amp; emit value 8
  Thread[main,5,main]: generate &amp; emit value 9
  Thread[main,5,main]: generate &amp; emit value 10
  Thread[parallel-1,5,main]: Receive: 10
  
  Process finished with exit code 0
  </code></pre>