简体   繁体   English

Rails应用程序连接池大小,避免最大池大小问题

[英]Rails app connection pool size, avoiding max pool size issues

I am running a JRuby on Rails application. 我正在运行JRuby on Rails应用程序。 I see a lot of this randomly in my logs: 我在日志中随机看到了很多内容:

The max pool size is currently 5; consider increasing it

I understand I can increase the max pool size in my configuration to address this. 我知道我可以增加配置中的最大池大小来解决这个问题。 The problem I'm looking to address is to understand what the optimal number should be. 我想解决的问题是要了解最佳数量应该是多少。 I am trying to avoid contention issues for connections. 我试图避免连接的争用问题。 Clearly setting this number to something obnoxiously large will not work either. 显然将这个数字设置为令人讨厌的大小也无济于事。

Is there a general protocol to follow to know your apps optimal pool size setting? 是否有一般协议可以了解您的应用程序最佳池大小设置?

From here , 这里开始

The optimum size of a thread pool depends on the number of processors available and the nature of the tasks on the work queue. 线程池的最佳大小取决于可用处理器的数量以及工作队列上任务的性质。 On an N-processor system for a work queue that will hold entirely compute-bound tasks, you will generally achieve maximum CPU utilization with a thread pool of N or N+1 threads. 在用于工作队列的N处理器系统上,它将保存完全由计算限制的任务,通常使用N或N + 1个线程的线程池实现最大CPU利用率。

For tasks that may wait for I/O to complete -- for example, a task that reads an HTTP request from a socket -- you will want to increase the pool size beyond the number of available processors, because not all threads will be working at all times. 对于可能等待I / O完成的任务 - 例如,从套接字读取HTTP请求的任务 - 您将希望将池大小增加到超过可用处理器的数量,因为并非所有线程都能正常工作每时每刻。 Using profiling, you can estimate the ratio of waiting time (WT) to service time (ST) for a typical request. 使用分析,您可以估计典型请求的等待时间(WT)与服务时间(ST)的比率。 If we call this ratio WT/ST, for an N-processor system, you'll want to have approximately N*(1+WT/ST) threads to keep the processors fully utilized. 如果我们将此比率称为WT / ST,对于N处理器系统,您将需要大约N *(1 + WT / ST)线程来保持处理器的充分利用。

Processor utilization is not the only consideration in tuning the thread pool size. 处理器利用率不是调整线程池大小的唯一考虑因素。 As the thread pool grows, you may encounter the limitations of the scheduler, available memory, or other system resources, such the number of sockets, open file handles, or database connections. 随着线程池的增长,您可能会遇到调度程序,可用内存或其他系统资源的限制,例如套接字,打开文件句柄或数据库连接的数量。

So profile your application, if your threads are mostly cpu bound, then set the thread pools size to number of cores, or number of cores + 1. If you are spending most of your time waiting for database calls to complete, then experiment with a fairly large number of threads, and see how the application performs. 因此,如果您的线程主要是cpu绑定,那么配置您的应用程序,然后将线程池大小设置为核心数或核心数+ 1.如果您花费大部分时间等待数据库调用完成,那么请尝试使用相当多的线程,并查看应用程序的执行情况。

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

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