简体   繁体   English

使用带有阻塞方法的反应性 spring 数据 (mongdb) 的不利/性能影响

[英]Downside/Performace Impact in using reactive spring data (mongdb) with blocking methods

I am very new to reactive programming, and currently working on microservice where in spring mvc is used and Spring Data MongoDb for database connectivity.我对反应式编程非常陌生,目前正在研究微服务,其中使用 spring mvc 和 Spring 数据 MongoDb 用于数据库连接。

As I am going through spring data mongo db docs, there is support for reactive repositories, reative template api etc.当我浏览 spring 数据 mongo db 文档时,支持响应式存储库、响应式模板 api 等。

So Is there going to be any downside if I choose to use reactive templates and repository with blocking nature?那么,如果我选择使用具有阻塞性质的反应式模板和存储库,会有什么缺点吗?

Ex.    
reactiveMongoTemplate.add(entity).block()
reactiveMongoTemplate.update(id, entity).block()

Also is there any significant difference with using calls like above than using blocking repository and template api itself?使用上述调用与使用阻塞存储库和模板 api 本身有什么显着区别吗?

The answer depends on which stack you use: Spring WebFlux or Spring Web MVC.答案取决于您使用的堆栈:Spring WebFlux 或 Spring Web MVC。

In case of Spring WebFlux the choice is obvious: you have to use ReactiveMongoTemplate and never call block on it, instead return Mono/Flux as you get it from the template.Spring WebFlux的情况下,选择是显而易见的:您必须使用 ReactiveMongoTemplate并且永远不要在其上调用block ,而是在从模板中获取 Mono/Flux 时返回它。

In case of Spring Web MVC you are free to use both the regular blocking MongoTemplate and ReactiveMongoTemplate with block .Spring Web MVC的情况下,您可以自由使用常规阻塞 MongoTemplate 和 ReactiveMongoTemplate with block Although, in most cases you should go with the good old MongoTemplate for sake of simplicity and performance.虽然,在大多数情况下,为了简单和性能,您应该使用旧的 MongoTemplate go ReactiveMongoTemplate has some overhead compared to the blocking MongoTemplate because the reactive types Mono/Flux put some additional pressure on memory.与阻塞 MongoTemplate 相比,ReactiveMongoTemplate 有一些开销,因为反应类型 Mono/Flux 对 memory 施加了一些额外的压力。

I can imagine one use case where ReactiveMongoTemplate can provide some advantage even in Spring MVC: when during one HTTP request you have to execute multiple Mono operations concurrently.我可以想象一个用例,即使在 Spring MVC 中,ReactiveMongoTemplate 也可以提供一些优势:在一个 HTTP 请求期间,您必须同时执行多个 Mono 操作。 If you used blocking MongoTemplate then you would need to set up a thread pool and delegate the query execution there.如果您使用阻塞 MongoTemplate,那么您需要设置一个线程池并在那里委派查询执行。 However, with ReactiveMongoTemplate you could use the many operators of Mono and Flux to accomplish this task without worrying about threads, thread pools and scaling issues.但是,使用 ReactiveMongoTemplate,您可以使用 Mono 和 Flux 的许多运算符来完成此任务,而无需担心线程、线程池和缩放问题。

In the traditional programming you usually own the thread that you're running on, in the reactive programming its not the case.在传统编程中,您通常拥有正在运行的线程,而在反应式编程中,情况并非如此。 This "underlying unit of execution" (cpu resources consumer if you wish) is not yours, but rather a "global" thing that currently happens to execute your task, but can switch to do other things really soon.这个“底层执行单元”(如果您愿意,可以使用 cpu 资源消费者)不是您的,而是当前恰好执行您的任务的“全局”事物,但可以很快切换到做其他事情。

So when you block, you say to this "global unit of execution" like "hey, stop doing anything else, wait for me".所以当你阻塞时,你对这个“全局执行单元”说“嘿,停止做其他事情,等我”。 In the traditional approach, its kind of ok, because you have a thread associated with the current request, other requests (or flows if your system is not web based) are supposed to be executed with other threads taken from a fairly large thread pool.在传统方法中,这还不错,因为您有一个与当前请求相关联的线程,其他请求(或如果您的系统不是基于 web 的流)应该与从相当大的线程池中获取的其他线程一起执行。 In the reactive system however, its not the case since you're trying to utilize a small amount of these "global units of execution".然而,在反应式系统中,情况并非如此,因为您试图使用少量这些“全局执行单元”。

Ok, so if you block, the events all over the place will stop emitting and will get start to buffer.好的,所以如果你阻塞,整个地方的事件将停止发射并开始缓冲。 And this may lead to the whole system becoming unusable.这可能会导致整个系统变得无法使用。

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

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