question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implementing Write Throttling / Back pressure

See original GitHub issue

This is more of a design question on what is the correct way to implement Write Throttling / Back pressure, so that we don’t continue writing to a channel when isWritable flag returns false. I have added the High and Low WaterMarks to control when exactly this flag should return false.

looking at @normanmaurer 's answer on SO for this issue :

You are writing faster then the network stack can handle. Be aware it’s all asynchronous… You want to stop writing once Channel.isWritable() returns false and resume once it returns true again. You can notified for this changes by override the channelWritabilityChanged(…) method in ChannelInboundHandler.

What I have tried in order to stop writing when Channel.isWritable() returns false and resume when it returns true, is to block on the event loop thread while checking for the flag in a loop. This is not ideal since the event loop thread is blocked. What would be the correct way to do achieve throttling/back pressure handling ? . Any help is much appreciated.

Relevant code snippets.

Listening to channel writability changed and unlocking

public void channelWritabilityChanged(ChannelHandlerContext ctx)
            throws Exception {
        Channel channel = ctx.channel();
        ThrottleLock throttleLock = channel.attr(THROTTLE_LOCK_KEY).get();
        if (channel.isWritable()) {
            synchronized (throttleLock) {
                throttleLock.notifyAll();
            }
        }
        ctx.fireChannelWritabilityChanged();
    }

waiting and blocking to see if the channel is writable

    ThrottleLock throttleLock = channel.attr(THROTTLE_LOCK_KEY).get();                       
     synchronized (throttleLock) {
         while (!channel.isWritable()) {                                    
                 throttleLock.wait(TimeUnit.MILLISECONDS.toMillis(100));
       }
       channel.writeAndFlush(request);
     }
                        

Netty Version

4.1.29.Final

Java Version

java version “1.8.0_192” Java™ SE Runtime Environment (build 1.8.0_192-b12) Java HotSpot™ 64-Bit Server VM (build 25.192-b12, mixed mode)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
normanmaurercommented, May 14, 2020

@ajaygeorge basically you need to stop writing unit the channel becomes writable again. You will be notified once the writability state of the Channel changes via the ChannelInboundHandler.channelWritabilityChanged(...) callback. Once this happens you can start writing again. How you propagate this though your application / framework, is up to you. Just never block the event loop.

0reactions
ajaygeorgecommented, May 11, 2020

Thanks @johnou . Looking at that PR , it seems like trustin had some concerns over the implementation which I believe was the reason why it was not merged. As the PR description suggests this for implementing back-pressure to stop reading from a socket.

Often what can be done to implement back-pressure is to stop reading from the socket until we were able to flush out enough data. We should provide some generic re-usable handlers.

In my case, the backpressure handling is needed for writes to a remote peer (not running netty) from a local client (running netty)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Backpressure explained — the resisted flow of data through ...
Backpressure Strategies · Control the producer (slow down/speed up is decided by consumer) · Buffer (accumulate incoming data spikes temporarily) ...
Read more >
Four Strategies to Handle Backpressure - Jay Phelps ... - InfoQ
Sampling/throttling/debouncing strategies adjust the incoming flow of data to the consumer by disregarding excess data. Phlelps subsequently ...
Read more >
Throttling Made Easy — Back Pressure in Akka Streams | by Jay
This is quiet a small computation and will not result in latency issues or back pressure. We can tie the operations with Akka's...
Read more >
Backpressure Mechanism in Spring WebFlux - Baeldung
In this tutorial, we'll explain what it is and how to apply backpressure mechanism in Spring WebFlux to mitigate it.
Read more >
Mutiny - Flow control and Back-pressure - Quarkus
One of the most brilliant usages of back-pressure is in TCP. A reader, receiving data, can block the writer, on the other side...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found