Hazelcast 简明教程

Hazelcast - Common Pitfalls & Performance Tips

Hazelcast Queue on single machine

Hazelcast 队列存储在单个成员上(以及不同机器上的备份)。这实际上意味着队列可以容纳一台机器可以容纳的任意多项。因此,队列容量不会通过添加更多成员而扩展。向队列中加载机器可以处理的数据量之外的数据会导致机器崩溃。

Hazelcast queues are stored on a single member (along with a backup on different machines). This effectively means the queue can hold as many items which can be accommodated on a single machine. So, the queue capacity does not scale by adding more members. Loading more data than what a machine can handle in a queue can cause the machine to crash.

Using Map’s set method instead of put

如果我们使用 IMap 的 put(key, newValue),Hazelcast 会返回旧值。这意味着,反序列化会花费额外的计算和时间。其中还包括从网络发送的更多数据。相反,如果我们对旧值不感兴趣,我们可以使用返回 void 的 set(key, value)。

If we use IMap’s put(key, newValue), Hazelcast returns the oldValue. This means, extra computation and time is spent in deserialization. This also includes more data sent from the network. Instead, if we are not interested in the oldValue, we can use set(key, value) which returns void.

让我们了解如何存储和注入对 Hazelcast 结构的引用。以下代码创建一个名为“stock”的地图,并在一个位置添加芒果,在另一个位置添加苹果。

Let’s see how to store and inject references to Hazelcast structures. The following code creates a map of the name "stock" and adds Mango at one place and Apple at another.

//initialize hazelcast instance
HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance();

// create a map
IMap<String, String> hzStockTemp = hazelcast.getMap("stock");
hzStock.put("Mango", "4");

IMap<String, String> hzStockTemp2 = hazelcast.getMap("stock");
hzStock.put("Apple", "3");

然而,这里的问题在于我们使用了 getMap(“stock”) 两次。虽然在单个节点环境中此调用看起来无害,但它在集群环境中会造成缓慢。函数调用 getMap() 涉及到与集群的其他成员之间的网络往返。

However, the problem here is that we are using getMap("stock") twice. Although this call seems harmless in a single node environment, it creates slowness in a clustered environment. The function call getMap() involves network round trips to other members of the cluster.

因此,建议我们将对映射的引用存储在本地,并在操作映射时使用引用。例如 −

So, it is recommended that we store the reference to the map locally and use the referencing while operating on the map. For example −

// create a map
IMap<String, String> hzStock = hazelcast.getMap("stock");
hzStock.put("Mango", "4");
hzStock.put("Apple", "3");

Hazelcast uses serialized data for object comparison

正如我们在早期的示例中所看到的,非常重要的一点是,Hazelcast 在比较键时不使用反序列化对象。因此,它无法访问我们在 equals/hashCode 方法中编写的代码。根据 Hazelcast,如果两个 Java 对象的所有属性值相同,那么键是相等的。

As we have seen in the earlier examples, it is very critical to note that Hazelcast does not use deserialize objects while comparing keys. So, it does not have access to the code written in our equals/hashCode method. According to Hazelcast, keys are equal if the value to all the attributes of two Java objects is the same.

Use monitoring

在大型分布式系统中,监视起着非常重要的作用。使用 REST API 和 JMX 进行监视对于采取主动措施而不是采取被动措施非常重要。

In a large-scale distributed system, monitoring plays a very important role. Using REST API and JMX for monitoring is very important for taking proactive measures instead of being reactive.

Homogeneous cluster

Hazelcast 假设所有机器是相等的,即所有机器具有相同的资源。但是,如果我们的集群包含一台功能较弱的机器,例如内存较少、CPU 功能较弱等,那么如果计算发生在那台机器上,可能会造成缓慢。最糟糕的是,较弱的机器可能会耗尽资源,从而导致级联故障。因此,Hazelcast 成员必须具有相等的资源能力。

Hazelcast assumes all the machines are equal, i.e., all the machines have same resources. But if our cluster contains a less powerful machine, for example, less memory, lesser CPU power, etc., then it can create slowness if the computation happens on that machine. Worst, the weaker machine can run out of resources causing cascading failures. So, it is necessary that Hazelcast members have equal resource power.