简体   繁体   English

Java NIO是否适合低延迟或高吞吐量?

[英]Java NIO good for low latency or high throughput?

I am new to Java NIO and have used it a little. 我是Java NIO的新手并且已经使用了一点。 I have a general query. 我有一个通用的查询。 If you are designing a ultra low latency app vs high throughput application, which of the two gets clearly benefited by using Non blocking IO? 如果您正在设计一个超低延迟应用程序与高吞吐量应用程序,那么这两个应用程序中的哪一个明显受益于使用非阻塞IO?

My understanding is that Non blocking IO should certainly help in high throughput as worker threads are not blocking, hence not waiting for response and are free to fire new requests until previous requests are served. 我的理解是非阻塞IO当然应该有助于高吞吐量,因为工作线程没有阻塞,因此不等待响应,并且可以自由地发送新请求,直到之前的请求被提供。 Once we get responses for previously fired requests, worker threads can process them asynchronously, increasing throughput. 一旦我们获得先前激活的请求的响应,工作线程就可以异步处理它们,从而提高吞吐量。

However, I am unable to see how Non blocking IO directly benefits low latency application. 但是,我无法看到非阻塞IO如何直接使低延迟应用程序受益。

I guess "Asynchronous behavior is a great way to avoid contention." 我猜“异步行为是避免争用的好方法。” If that is the case, low contention means low latency. 如果是这种情况,低争用意味着低延迟。 Hence NIO may help in low latency. 因此,NIO可能有助于降低延迟。 Does it make sense? 是否有意义?

"Asynchronous behavior is a great way to avoid contention." “异步行为是避免争用的好方法。” - only when single thread is used. - 仅在使用单线程时。 If many threads, contention is unavoidable. 如果有很多线程,争论是不可避免的。 You have to use multitrhreading (with or without NIO) to get high throughput and/or low latency. 您必须使用多线程(使用或不使用NIO)来获得高吞吐量和/或低延迟。

NIO only helps to keep number of threads low (around the number of available processors) and thus saves memory (each thread consume a lot of memory) and allows enormous number of simultaneous connections, but usually has worse performance than blocking IO. NIO只能帮助保持线程数低(大约可用处理器数量),从而节省内存(每个线程消耗大量内存)并允许大量同时连接,但通常比阻塞IO性能更差。

It all depends on... :) 这一切都取决于... :)

  • If you have a number of simultaneous threads <= number of CPU cores, then low latency you get can using blocking IO/NIO. 如果您有多个并发线程<= CPU核心数,那么您可以使用阻塞IO / NIO获得低延迟。 Usually, throughput is slightly worse in case of blocking IO/NIO. 通常,在阻塞IO / NIO的情况下,吞吐量会稍差。 See for instance: http://vanillajava.blogspot.ru/2011/08/comparing-java-7-async-nio-with-nio.html 例如,参见: http//vanillajava.blogspot.ru/2011/08/comparing-java-7-async-nio-with-nio.html

  • If you have a large number of thread, thread-per-connection model becomes less preferred over async NIO. 如果您有大量线程,则每个连接的线程模型比异步NIO更不受欢迎。 Mostly because you pay the cost of the context switching, which may be not so small (time to call the system scheduler, more cache misses, etc). 主要是因为你支付了上下文切换的成本,这可能不是那么小(调用系统调度程序的时间,更多的缓存未命中等)。 See, for instance: http://www.cs.rochester.edu/u/cli/research/switch.pdf And more threads = more total price :) Here we need async NIO for both latency and throughput. 例如,请参阅: http//www.cs.rochester.edu/u/cli/research/switch.pdf并且更多线程=更多总价格:)这里我们需要异步NIO来延迟和吞吐量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM