Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats is back pressure on streams processing

I start learning node and streams seems to be something that you use a lot, in most of the documentation that I had read is mention the "back-pressure problem" when you are processing big sized files but I haven't fount a clear explanation of what exactly this problem is. Also I have read that use pipes can help with this problem, bu how exactly pipes fix the back-pressure problem?

thanks for any explanation in advance.

like image 275
David Florez Avatar asked Jan 04 '17 16:01

David Florez


People also ask

What is backpressure in stream processing?

What is backpressure. Streaming systems like Flink need to be able to gracefully deal with backpressure. Backpressure refers to the situation where a system is receiving data at a higher rate than it can process during a temporary load spike. Many everyday situations can cause backpressure.

What is the back pressure?

Back pressure (or backpressure) is a resistance or force opposing the desired flow of fluid through pipes, leading to friction loss and pressure drop. The term back pressure is a misnomer, as pressure is a scalar quantity, so it has a magnitude but no direction.

How do you manage back pressure?

Another technique to solve back-pressure is to scale up the callee, which means to increase the processing power of the callee, either by its replacement with a high-speed callee or by the addition of another callee. Scaling will allow the callee to respond to the requests faster and thus prevent back-pressure.

What is backpressure in distributed systems?

Backpressure. Back-pressure is a mechanism to resist the flow of data through software systems so as to maintain a stable flow in face of local variations in capacity and throughput. We want to regulate the flow of water by not letting too much water enter at too much speed, thereby risking the water-hose mesh.


1 Answers

Backpressure is when you write to a stream faster than the other process can handle/consume Using Pipes, you can control the flow, pause and resume the stream; Here is an example implementing backpressure in nodejs

var http = require('http'),
    fs = require('fs');

var server = http.createServer(function(request, response) {
  var file = fs.createWriteStream('upload.jpg'),
      fileBytes = request.headers['content-length'],
      uploadedBytes = 0;

  request.on('data', function(chunk) {
    uploadedBytes += chunk.length;
    var progress = (uploadedBytes / fileBytes) * 100;
    response.write('progress: ' + parseInt(progress, 10) + '%\n');

    var bufferOK = file.write(chunk);

    if (!bufferOK) {
      request.pause();
    }
  });

  file.on('drain', function() {
    request.resume();
  });

  request.on('end', function() {
    response.end('upload complete\n');
  });

});

server.listen(8080);

Ben foster solution - Souce: https://gist.github.com/benfoster/9543337

like image 136
Wagner Silveira Avatar answered Sep 20 '22 18:09

Wagner Silveira