08异步请求:何种场景下应该使用异步请求?
异步在计算机科学中早就是一个比较常用的词汇,从操作系统的特性( 并发、共享、虚拟、异步)开始,异步就在处理并发操作中起到很大的作用,不仅如此,在软件层面,异步同样也是解决并发请求的一个关键过程,它可以将瞬时激增的请求进行更加灵活的处理,通过异步请求,客户端可以在等待服务器响应时继续处理其它请求,从而提高系统的并发性能。
与此同时,在gRPC框架提供的服务中,在需要高并发、高吞吐量的情况下也可以使用异步请求来提高系统的并发性能,但是通常会使用缓存、消息队列等中间件才能达到更好的效果,这篇文章我们就来讲解一下gRPC中的异步使用。
同步和异步
首先我们来看下gRPC官方文档上对同步和异步的简要描述:
Synchronous RPC calls that block until a response arrives from the server are the closest approximation to the abstraction of a procedure call that RPC aspires to. On the other hand, networks are inherently asynchronous and in many scenarios it’s useful to be able to start RPCs without blocking the current thread.
The gRPC programming API in most languages comes in both synchronous and asynchronous flavors. You can find out more in each language’s tutorial and reference docu