简体   繁体   English

Rails3活动记录池和Sidekiq多线程

[英]Rails3 active record pool and Sidekiq multi-thread

I am using sidekiq with rails3. 我正在使用带有rails3的sidekiq。 Sidekiq runs 25 threads default. Sidekiq默认运行25个线程。 I would like to increase multi-thread limit, I have done this by changing sidekiq.yml. 我想增加多线程限制,我通过更改sidekiq.yml来做到这一点。

So, what is the relation between pool value in database.yml and sidekiq multi-thread. 那么,database.yml中的池值和sidekiq多线程之间是什么关系。 What is the maximun value of mysql pool. mysql池的最大值是多少? Is it depends on server memory? 是否取决于服务器内存?

sidekiq.yml sidekiq.yml

:verbose: true
:concurrency:  50
:pool: 50
:queues:
  - [queue_primary, 7]
  - [default, 5]
  - [queue_secondary, 3]

database.yml 数据库

production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: db_name
  pool: 50
  username: root
  password: root
  socket: /var/run/mysqld/mysqld.sock

Each Sidekiq job executes in one of up to 50 threads with your configuration. 每个Sidekiq作业在您的配置中最多执行50个线程之一。 Inside the job, any time an ActiveRecord model needs to access the database, it uses a database connection from the pool of available connections shared by all ActiveRecord models in this process. 在作业内部,只要ActiveRecord模型需要访问数据库,它就会在此过程中使用所有ActiveRecord模型共享的可用连接池中的数据库连接。 The connection pool lets a thread take a connection or blocks until a free connection is available. 连接池允许线程进行连接或阻塞,直到可用连接可用为止。

If you have less connections available in your ActiveRecord database connection pool than running Sidekiq jobs/threads, jobs will be blocked waiting for a connection and possibly timeout (after ~ 5 seconds) and fail. 如果与运行Sidekiq作业/线程相比,ActiveRecord数据库连接池中可用的连接较少,则作业将被阻塞,以等待连接,并且可能会超时(约5秒后)而失败。

This is why it's important that you have as many available database connections as threads in your sidekiq worker process. 这就是为什么在sidekiq worker进程中拥有与线程一样多的可用数据库连接很重要的原因。

Unicorn is a single-threaded, multi-process server - so you shouldn't need more than one connection for each Unicorn back-end worker process. Unicorn是单线程,多进程服务器-因此,每个Unicorn后端工作进程都不需要多个连接。

However, the database can only handle so many connections (depending on OS, hardware, and configuration limits) so you need to make sure that you are distributing your database connections where they are needed and not exceeding your maximum. 但是,数据库只能处理这么多的连接(取决于操作系统,硬件和配置限制),因此您需要确保将数据库连接分配到需要的地方,并且不超过最大连接数。

For example, if your database is limited to 1000 connections, you could only run 20 sidekiq processes with 50 threads each and nothing else. 例如,如果您的数据库限制为1000个连接,则您只能运行20个sidekiq进程,每个进程有50个线程,而没有其他操作。

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

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