Cloud Design Pattern - Queue-Based Load Leveling(队列负载均衡)

来源:互联网 发布:wind 金融数据 编辑:程序博客网 时间:2024/05/16 19:51

1.前言

上一篇我们讨论了云计算设计模式之优先级队列模式,介绍了在消息队列中,按照消息的优先级来决定消息的被处理顺序.这一篇我们来讨论下如何基于队列的负载均衡模式.

2.概念

在传统的应用中,我们并没有使用消息队列,因为并发的请求量很小.在并发量很大的情况下,我们就需要使用消息队列来做一个缓冲,确保消息被处理的过程中不会并发太多而导致系统崩溃,所以,队列是一种负载均衡的机制,这种机制确保了系统的稳定性.

这种模式的优势一目了然,消息的产生速度不会对处理系统造成巨大的冲击,处理机制依然可以按照既定的速度进行处理.

This pattern provides the following benefits:
1)It can help to maximize availability because delays arising in services will not have an immediate and direct impact on the application, which can continue to post messages to the queue even when the service is not available or is not currently processing messages.
2)It can help to maximize scalability because both the number of queues and the number of services can be varied to meet demand.
3)It can help to control costs because the number of service instances deployed needs only to be sufficient to meet average load rather than the peak load.

不过这种模式也有一些问题值得考虑:

Consider the following points when deciding how to implement this pattern:

1)It is necessary to implement application logic that controls the rate at which services handle messages to avoid overwhelming the target resource. Avoid passing spikes in demand to the next stage of the system. Test the system under load to ensure that it provides the required leveling, and adjust the number of queues and the number of service instances that handle messages to achieve this.

2)Message queues are a one-way communication mechanism. If a task expects a reply from a service, it may be necessary to implement a mechanism that the service can use to send a response. For more information, see the Asynchronous Messaging Primer.

3)You must be careful if you apply autoscaling to services that are listening for requests on the queue because this may result in increased contention for any resources that these services share, and diminish the effectiveness of using the queue to level the load.

关于何时适合使用这种模式,官方说法很简单:

This pattern is ideally suited to any type of application that uses services that may be subject to overloading.

This pattern might not be suitable if the application expects a response from the service with minimal latency.

3.Example

在使用Azure Work Role过程中,多个实例并发写入的时候,某些实例可能会出现操作失败的问题.

这种问题情境刚好符合我们的问题域,采用下面这种模式就可以很好地解决这个问题.

4.相关阅读

The following patterns and guidance may also be relevant when implementing this pattern:

  • Asynchronous Messaging Primer. Message queues are an inherently asynchronous communications mechanism. It may be necessary to redesign the application logic in a task if it is adapted from communicating directly with a service to using a message queue. Similarly, it may be necessary to refactor a service to accept requests from a message queue (alternatively, it may be possible to implement a proxy service, as described in the example).
  • Competing Consumers Pattern. It may be possible to run multiple instances of a service, each of which act as a message consumer from the load-leveling queue. You can use this approach to adjust the rate at which messages are received and passed to a service.
  • Throttling Pattern. A simple way to implement throttling with a service is to use queue-based load-leveling and route all requests to a service through a message queue. The service can process requests at a rate that ensures resources required by the service are not exhausted, and to reduce the amount of contention that could occur.
  • The articles Queue Service Concepts and Service Bus on the MSDN website.






0 0
原创粉丝点击