# 关于工人数的建议：核心数 × 2？

**URL:** <https://meta.discourse.org/t/recommendation-for-number-of-workers-cores-x-2/363055>\
**Category:** Self-hosting\
**Created:** [2025年四月22日 14:48 UTC](https://meta.discourse.org/t/recommendation-for-number-of-workers-cores-x-2/363055 "2025-04-22T14:48:20Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Editor0732](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/editor0732/32/492698_2.png) [@Editor0732](https://meta.discourse.org/u/Editor0732)\
**Post date:** [2025年四月22日 14:48 UTC](https://meta.discourse.org/t/recommendation-for-number-of-workers-cores-x-2/363055/1 "2025-04-22T14:48:20Z")

</div>

所以所有关于“`UNICORN WORKER` 应为 2\*vCPU”的说法都是错误的？  
我的服务器是 Intel Xeon E5-2686 v4 @ 2.30GHz—24vCPU+32GB 内存  
我需要设置多少个 `UNICORN WORKER`？  
8 个？还是 48 个？  
我的网站有超过 7k 用户和大约 1k 的每日活跃用户。用户每天发送 3k-7k 条帖子。我们的社区每天有 120k-200k 的页面浏览量/天，包括爬虫和匿名用户。

---

<div class="post-metadata">

**Author:** ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)\
**Post date:** [2025年四月22日 17:41 UTC](https://meta.discourse.org/t/recommendation-for-number-of-workers-cores-x-2/363055/2 "2025-04-22T17:41:01Z")

</div>

这取决于许多因素。例如，您的数据库大小与 RAM 的比例、登录流量与匿名流量的比例、是否有插件使您的 Sidekiq 队列更繁忙、您是否正在运行 YJIT 等。

简单的方法是查看高峰时段的 MiniProfiler 数据，浏览论坛，看看性能是否有所下降，并找出瓶颈。

由于这台 CPU 比较老旧，我会比通常的 Unicorn 工作进程数设置得更多一些，因为每个请求都会比平时花费更长的时间。但如果您的 PostgreSQL 和 Redis 运行在同一台服务器上，您就不能通过运行过多的工作进程来耗尽它们。

尝试从 16 个工作进程开始运行，并评估网站的性能。

---

<div class="post-metadata">

**Author:** ![Ed\_S](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/ed_s/32/134015_2.png) [@Ed\_S](https://meta.discourse.org/u/Ed_S)\
**Post date:** [2025年四月22日 18:41 UTC](https://meta.discourse.org/t/recommendation-for-number-of-workers-cores-x-2/363055/3 "2025-04-22T18:41:12Z")

</div>

有没有一个简单易懂的描述，说明 Unicorn 工作进程在做什么？我的理解是，每次用户页面请求都必须由一个 Unicorn 工作进程来处理。如果不够，用户就得等待。如果太多……嗯，也许会消耗一点内存？

---

<div class="post-metadata">

**Author:** ![supermathie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/supermathie/32/507518_2.png) [@supermathie](https://meta.discourse.org/u/supermathie)\
**Post date:** [2025年四月22日 19:06 UTC](https://meta.discourse.org/t/recommendation-for-number-of-workers-cores-x-2/363055/4 "2025-04-22T19:06:00Z")

</div>

> [@Ed\_S](#):
>
> 有没有一个简单的、人类可读的描述，说明 Unicorn 工作进程在做什么？

它们是应用程序 Web 服务器。

---

<div class="post-metadata">

**Author:** ![Editor0732](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/editor0732/32/492698_2.png) [@Editor0732](https://meta.discourse.org/u/Editor0732)\
**Post date:** [2025年四月22日 20:00 UTC](https://meta.discourse.org/t/recommendation-for-number-of-workers-cores-x-2/363055/5 "2025-04-22T20:00:53Z")

</div>

现在不是高峰期，但我认为加载时间有问题。

 ![性能监控图表（由 AI 生成标题）](https://global.discourse-cdn.com/meta/original/4X/2/a/d/2ad0e6159f2490b1f3871d63121f87d4307b9a64.png)

---

<div class="post-metadata">

**Author:** ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)\
**Post date:** [2025年四月22日 20:14 UTC](https://meta.discourse.org/t/recommendation-for-number-of-workers-cores-x-2/363055/6 "2025-04-22T20:14:49Z")

</div>

看起来您10年前的CPU已经显露老态了。增加Unicorn工作进程可以允许您同时处理更多用户，但不会让单个请求的速度变快。

您可以尝试启用YJIT吗？

> [@tgxworld](#):
>
> ### Discourse 自托管用户
> 
> 对于自托管 Discourse 的用户，您可以通过在容器定义文件中添加 `enable-ruby-yjit.template.yml` 来启用 YJIT。例如：
> 
> ```plaintext
> templates:
> - 'templates/enable-ruby-yjit.yml'
> 
> ```

---

<div class="post-metadata">

**Author:** ![supermathie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/supermathie/32/507518_2.png) [@supermathie](https://meta.discourse.org/u/supermathie)\
**Post date:** [2025年四月22日 20:42 UTC](https://meta.discourse.org/t/recommendation-for-number-of-workers-cores-x-2/363055/9 "2025-04-22T20:42:04Z")

</div>

> [@Editor0732](#):
>
> 我认为加载时间有问题

使用您的硬件，我_预计_应用程序的平均登录列表/最新时间约为 150 毫秒，SQL 约为 80 毫秒。

我建议从 12 个工作进程开始，看看它的表现如何。您可以做的最好的事情是跟踪指标；如果您想知道是否应该添加更多工作进程，请查看请求是否在等待应用程序工作进程。

您是否正在跟踪 Discourse 本身通过 Prometheus 导出器导出的指标？这些将让您全面了解实例的整体性能。

匿名用户和普通用户（非管理员）的性能数字是多少？

---

<div class="post-metadata">

**Author:** ![Editor0732](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/editor0732/32/492698_2.png) [@Editor0732](https://meta.discourse.org/u/Editor0732)\
**Post date:** [2025年四月25日 16:18 UTC](https://meta.discourse.org/t/recommendation-for-number-of-workers-cores-x-2/363055/11 "2025-04-25T16:18:43Z")

</div>

![The image displays a series of performance metrics for two pages over a period from 21:15 to 23:45, showcasing median and 98th percentile page performance, total example count, successful HTTP requests, and error rates with redirects. (由 AI 生成标题)](https://global.discourse-cdn.com/meta/original/4X/d/b/1/db1babce536d92db4a793d4966a60c9dfa062d1f.png)  
 ![The image shows various graphs and metrics related to server and container performance, including page views per second, active web requests, object allocation rate, Ruby heap stats, and container CPU and memory usage, along with sections indicating no data for CPU usage and memory usage. (由 AI 生成标题)](https://global.discourse-cdn.com/meta/original/4X/9/5/9/959754947bdb599df7408e0bb543fb4aa85c15fb.png)  
 ![The image shows four graphs depicting performance metrics for a system, including Max RSS per Host, V8 Used Memory, Sidekiq Job Duration, and Scheduled Job Duration, with timestamps from April 25, 2025, showing various spikes and activities. (由 AI 生成标题)](https://global.discourse-cdn.com/meta/original/4X/0/6/6/066682396e43b792ad4ed06ab231e1ab0997b21d.png)  
There are many mega topics in our Discourse, and the largest one is already in the twelfth section.  
Reply views  
 ![The image displays a table of numerical data, with each pair underlined in orange. (由 AI 生成标题)](https://global.discourse-cdn.com/meta/original/4X/8/1/1/811a2b3963c6ba872963cf94db505a07dcd2e232.png)

---

<div class="post-metadata">

**Author:** ![merefield](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/merefield/32/176214_2.png) [@merefield](https://meta.discourse.org/u/merefield)\
**Post date:** [2025年四月25日 18:09 UTC](https://meta.discourse.org/t/recommendation-for-number-of-workers-cores-x-2/363055/12 "2025-04-25T18:09:31Z")

</div>

> [@Falco](#):
>
> 看起来你10年前的CPU已经显露老态了。

😆 (/停止大笑，登录托管服务商… )

> [@Falco](#):
>
> 你能试试启用YJIT吗？

哇，这难道不是默认设置吗？？

编辑：哦，当然，你可能是用旧的app.yml构建的，并且没有更新过。

---

<div class="post-metadata">

**Author:** ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)\
**Post date:** [2025年四月25日 18:21 UTC](https://meta.discourse.org/t/recommendation-for-number-of-workers-cores-x-2/363055/13 "2025-04-25T18:21:50Z")

</div>

> [@merefield](#):
>
> 哇，这现在不是默认设置吗？？

在我们的托管环境中是默认设置，但将其设为默认设置有点困难，因为人们可能在内存受限的情况下运行……

话虽如此，我们的 JS 构建使用的 RAM 比 Discourse 本身多得多，所以你可以说任何能够构建 JS 资源的人都有足够的 RAM 可以随意使用 😛

---

<div class="post-metadata">

**Author:** ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)\
**Post date:** [2025年四月25日 18:24 UTC](https://meta.discourse.org/t/recommendation-for-number-of-workers-cores-x-2/363055/14 "2025-04-25T18:24:12Z")

</div>

在这些图片中，您设置了多少个工作进程？

我认为您应该

1. 稍微增加工作进程，因为您会遇到一些排队
2. 启用 YJIT，因为您的 Web 时间相当慢

---

<div class="post-metadata">

**Author:** ![Editor0732](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/editor0732/32/492698_2.png) [@Editor0732](https://meta.discourse.org/u/Editor0732)\
**Post date:** [2025年四月25日 18:29 UTC](https://meta.discourse.org/t/recommendation-for-number-of-workers-cores-x-2/363055/15 "2025-04-25T18:29:56Z")

</div>

现在只有 8 个工作进程，并且 YJIT 已启用。  
我应该增加多少个工作进程？

---

<div class="post-metadata">

**Author:** ![supermathie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/supermathie/32/507518_2.png) [@supermathie](https://meta.discourse.org/u/supermathie)\
**Post date:** [2025年四月25日 18:58 UTC](https://meta.discourse.org/t/recommendation-for-number-of-workers-cores-x-2/363055/16 "2025-04-25T18:58:43Z")

</div>

> [@Editor0732](#):
>
> 我应该增加多少个工作进程？

顺便说一下，这是 Falco 查看的内容，以便提出该建议：

 ![图像是一个显示活动 Web 请求随时间变化的折线图，在 22:45 和 00:00 有峰值，排队的请求用红色箭头突出显示。（由 AI 题注）](https://global.discourse-cdn.com/meta/original/4X/d/5/a/d5a3be3195bd79faa16dbb027f9272b4007a06ed.png)

我建议将 8 个增加到 12 个。这样您就有一些余量，并且应该可以清除这些队列。

顺便说一下，那个大的峰值表明其他请求正在等待……某个东西，可能是某个公共锁。也许是一个大型主题帖子。

如果您还能获取 PostgreSQL 使用情况指标，那将很有用。

> [@Editor0732](#):
>
> 我们的 Discourse 中有很多大型主题

大型主题是一个薄弱环节，请参阅 [Improving Instance Performance (Megatopics, Database Size and Extreme Load)](https://meta.discourse.org/t/improving-instance-performance-megatopics-database-size-and-extreme-load/144277)

考虑将它们分解或改用聊天（我看到您最大的那个有 8.9k 条回复，明确是一个聊天室）。

---

<div class="post-metadata">

**Author:** ![Editor0732](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/editor0732/32/492698_2.png) [@Editor0732](https://meta.discourse.org/u/Editor0732)\
**Post date:** [2025年四月25日 21:59 UTC](https://meta.discourse.org/t/recommendation-for-number-of-workers-cores-x-2/363055/18 "2025-04-25T21:59:59Z")

</div>

我们社区的讨论文化是 megatopics，这甚至在我们开始使用 Discourse 之前就已经形成了，而聊天缺乏模糊的剧透和隐藏的细节功能。

---

<div class="post-metadata">

**Author:** ![Editor0732](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/editor0732/32/492698_2.png) [@Editor0732](https://meta.discourse.org/u/Editor0732)\
**Post date:** [2025年四月25日 22:00 UTC](https://meta.discourse.org/t/recommendation-for-number-of-workers-cores-x-2/363055/19 "2025-04-25T22:00:56Z")

</div>

> [@supermathie](#):
>
> 获取 PostgreSQL 使用情况指标

如何做到

---

<div class="post-metadata">

**Author:** ![supermathie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/supermathie/32/507518_2.png) [@supermathie](https://meta.discourse.org/u/supermathie)\
**Post date:** [2025年四月25日 22:18 UTC](https://meta.discourse.org/t/recommendation-for-number-of-workers-cores-x-2/363055/20 "2025-04-25T22:18:15Z")

</div>

我们使用 [https://github.com/prometheus-community/postgres\_exporter，但不确定是否有关于设置它的元指南。](https://github.com/prometheus-community/postgres_exporter%EF%BC%8C%E4%BD%86%E4%B8%8D%E7%A1%AE%E5%AE%9A%E6%98%AF%E5%90%A6%E6%9C%89%E5%85%B3%E4%BA%8E%E8%AE%BE%E7%BD%AE%E5%AE%83%E7%9A%84%E5%85%83%E6%8C%87%E5%8D%97%E3%80%82)

不过，鉴于您似乎已经设置了 Prometheus，看起来您知道自己在做什么。

---

<div class="post-metadata">

**Author:** ![Editor0732](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/editor0732/32/492698_2.png) [@Editor0732](https://meta.discourse.org/u/Editor0732)\
**Post date:** [2025年四月29日 13:16 UTC](https://meta.discourse.org/t/recommendation-for-number-of-workers-cores-x-2/363055/21 "2025-04-29T13:16:32Z")

</div>

![Various performance metrics, including page views, median performance, 90th percentile performance, HTTP requests per second, and memory usage, are visualized via graphs from 21:25 to 22:40. (由 AI 生成标题)](https://global.discourse-cdn.com/meta/original/4X/2/3/2/2328f61c0081a034de46ab440c96f78f7590eb81.png)  
now server ram is 16/32GB, UNICORN\_WORKERS: 12, db\_shared\_buffers: “4096MB”  
Because there is still RAM available and a few web request queued, I increased the UNICORN\_WORKERS to 24. In the afternoon today, the server suddenly shut down and after it was restarted, users flooded in immediately. This led to a very low number of Active Web Requests and a large number of queued requests. A few days ago, we observed that 24 UNICORN\_WORKERS could handle 150+ Active Web Requests, but only 30 Active Web Requests this afternoon. This is because we have just changed the domain, and there are many posts being rebaked by sidekiq. This has caused a lot of pressure on the server. What should we do?  
 ![The image shows a dashboard with statistics and options for different queues in a system, including processed items, failed items, busy time, enqueued data, retries, scheduled items, and dead items. (由 AI 生成标题)](https://global.discourse-cdn.com/meta/original/4X/d/e/0/de037a8e420f9e4ca6daf8ad3f4895c262705285.png)

 ![The image shows real-time monitoring data and performance metrics for a web application, including worker count, job handling capacity, database response times, and query metrics over a specified time period. (由 AI 生成标题)](https://global.discourse-cdn.com/meta/original/4X/5/9/4/59404229cab3cbe78a595f33378ff408db102f2c.png)  
 ![The image displays a comprehensive set of performance metrics for a web server, including successful HTTP requests, errors, page views, CPU usage, active web requests, and container memory usage, organized in a clear and detailed graphical format. (由 AI 生成标题)](https://global.discourse-cdn.com/meta/original/4X/6/1/c/61cc330054953ef54f7cef166bf70b56d9df4ef3.png)  
 ![The image displays various graphs and line charts related to server processing, memory usage, job durations, and scheduled tasks over a time span, including web and daemon processes, memory consumption, and job durations. (由 AI 生成标题)](https://global.discourse-cdn.com/meta/original/4X/a/a/7/aa7bd6ed755233d6fd1b54629e8923524e958f1c.png)
