title: POST InputStream with RestTemplate and handling backpresure
body: <p>We have 3 tier System (UI,Server1,Server2)</p>
  <p>We have to transfer files from our client (Angular) to some server (java server1, Spring) and this server has to pass the file data to the second server</p>
  <p>UI =&gt; Server 1 ==&gt; Server 2</p>
  <p>We want to stream the bytes from the UI to &quot;Server 2&quot; without storing the files on the disk of &quot;Server 1&quot; or without storing the file data in the memory of &quot;Server 1&quot;</p>
  <p>The problem that the link &quot;UI&quot; ==&gt; &quot;Server 1&quot; is fast</p>
  <p>and the link from &quot;Server 1&quot; ==&gt; &quot;Server 2&quot; is slow</p>
  <p>And we face the problem that the file data gets buffered in the output stream towards &quot;server 2&quot;</p>
  <p>This is the function that we currently use:</p>
  <pre><code>private ResponseEntity&lt;String&gt; upload(String url, HttpHeaders headers, HttpServletRequest request) {
          // copy bytes from caller input stream to FE request stream
          RequestCallback requestCallback = (ClientHttpRequest req) -&gt; {
              Utils.propagateHeaders(headers,req);
              request.getInputStream().transferTo(req.getBody());
          };
  
          // execute the call to server 2
          ResponseEntity executeResult = template.execute(url, HttpMethod.PUT, requestCallback, (clientHttpResponse) -&gt; {
              HttpStatus statusCode = clientHttpResponse.getStatusCode();               
              return ResponseEntity.status(statusCode);
          });
          return executeResult;
      }
  </code></pre>
  <p>So we understand that this is an issue of back-pressure ,request.getInputStream() has to send the data to the output stream in the speed that the output stream can send the data over the wire</p>
  <p>What is the best way to do that?</p>
  <p>Thanks</p>