10

在 PHP 中编写批处理器时(如,它显然必须是 cron-ed),使用 Gearman 和简单地在 Redis 中存储要处理的数据之间有什么实际区别?

到目前为止,我的观察是,虽然 Gearman 能够实时推送工作,因为 PHP 代码只能间隔运行,但在 Redis 中使用定期调度的命令似乎或多或少是等效的。

此外,似乎使用 Gearman 通过将其绑定到 Gearman 库的调度生命周期给应用程序增加了不必要的复杂性。

综上所述,假设批处理器不会持续运行,假设 Gearman+PHP 与 Redis+PHP 相比没有任何优势是正确的吗?

4

2 回答 2

19

Gearman is a distributed job server, Redis is a distributed store. So it is bit like comparing apples to oranges.

Now, it is possible to implement Gearman-like features with Redis (based on the list data type for instance), but it is a do-it-yourself approach. While the principle is simple, the devil is in the details.

The best Redis distributed queue implementations are for Ruby (Resque) and Python (Celery, RQ). There is a port of Resque for PHP:

https://github.com/chrisboulton/php-resque

There are important points to consider when comparing Gearman to a Redis-based implementation:

  • Gearman jobs notify their completion to the client, and can be synchronous or asynchronous. If you do not implement something specific, a Redis queue will only support asynchronous jobs without completion notification.

  • High-availability of the broker. Gearman proposes an off-the-shelf strategy. Redis does not. While you can configure master-slave replication, and use Redis Sentinel, Redis HA is not a simple problem.

  • Persistency. Gearman supports in-memory queues, but also some persistent backends (MySQL, Drizzle, sqlite, PostgreSQL). Redis proposes various persistency options, but none of them is as reliable as a transactional engine like MySQL or PostgreSQL.

  • Vertical scalability. While Redis is very efficient, it is a single-threaded process. Gearmand is a multi-threaded process, that can probably scale better (considering a single process).

Implementing a Redis-based distributed job system is fun and interesting, but if you need something working quickly, Gearman is your best bet.

于 2013-08-16T16:26:28.833 回答
4

除了 Didier 的回答之外,Gearman 还可以提供合并功能,例如,如果在工作人员完成工作之前,一堆客户端都发出了相同的请求,它可以将工作的响应发送回所有客户端。

来自维基百科:

Gearman 对客户发送的工作执行合并。如果两个或多个客户端要求在同一工作主体上完成工作,或者通过看到正在发送相同的块或通过使用客户端发送的唯一值,它将合并工作以便只有一个工人用过的。它专门这样做是为了避免缓存命中失败常见的群体问题

这在 Redis 中实现起来要复杂得多。

于 2014-10-09T19:12:45.343 回答