Hazelcast
Data Backup
Map Configuration
Database Management
Data Storage

Hazelcast Map Configuration For Data Backup

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

In the realm of distributed computing, data redundancy and backup are critical for ensuring high availability and resilience against data loss. Hazelcast, an open-source in-memory data grid, provides robust mechanisms for data backup through its distributed map feature, commonly known as IMap. This article delves into the configuration options for data backup within a Hazelcast map, providing technical explanations and practical examples.

Understanding Hazelcast's Map Backup

Hazelcast IMap is designed to hold data in-memory which makes data access extremely fast. However, the volatile nature of memory necessitates mechanisms to prevent data loss. Hazelcast addresses this by allowing data in maps to be stored across multiple nodes in a cluster. This means that if one node fails, the data is still available from another node.

By default, each entry in a Hazelcast map is stored in one member and optionally backed up on one or more other members, depending on the configuration. These backups can be synchronous or asynchronous.

Synchronous vs Asynchronous Backup

Hazelcast provides two modes of backup: synchronous and asynchronous.

  • Synchronous backups ensure that a map entry update operation does not complete until the data is safely stored in the primary member and the backup members. While this mode guarantees strong consistency, it can lead to higher latencies for write operations.
  • Asynchronous backups, on the other hand, return control to the calling operation as soon as the data is updated on the primary member, while the backup is updated in the background. This can improve write performance but at the cost of potential consistency issues during node failure.

Configuration Examples

The backup configuration in Hazelcast can be set programmatically or via XML/JSON/YAML configuration files. Here is an example using Java:

java
1import com.hazelcast.config.Config;
2import com.hazelcast.config.MapConfig;
3
4public class HazelcastConfiguration {
5    public static void main(String[] args) {
6        Config config = new Config();
7        MapConfig mapConfig = new MapConfig("my-distributed-map");
8        mapConfig.setBackupCount(2);
9        mapConfig.setAsyncBackupCount(1);
10        config.addMapConfig(mapConfig);
11        Hazelcast.newHazelcastInstance(config);
12    }
13}

In the above example, setBackupCount(2) configures the map to have 2 synchronous backups, and setAsyncBackupCount(1) configures an additional asynchronous backup.

Backup Count and Replication Factor

Key to Hazelcast's backup configuration is the understanding of the backup count and the total number of replicas (or the replication factor). The replication factor includes the primary replica plus the backup replicas. For example, with a backup count of 1, the total number of replicas is 2 (1 primary + 1 backup).

Table: Backup Configuration Parameters

ParameterDescriptionTypical Values
backupCountNumber of synchronous backups. Ensures data safety at the cost of write performance.0 to 7, where 7 means 7 synchronous backups.
asyncBackupCountNumber of asynchronous backups. Enhances write performance with a slight risk during failures.0 to 7, asynchronous backups do not block caller.
readBackupDataWhether to allow reading from backup data. This can balance the read load but might show stale data.true/false

Tuning Backup Configuration

The choice between synchronous and asynchronous backups often depends on the specific requirements of the application regarding consistency, latency, and throughput. For applications that require strong consistency and can tolerate higher write latency, synchronous backups are preferable. Conversely, for applications prioritizing write speed over consistency, asynchronous backups may be more suitable.

Furthermore, Hazelcast allows you to fine-tune how backups are handled upon node failure. For instance, the configuration might include details on how backups are promoted or how data integrity is verified during a network partition.

Conclusion

Configuring data backups in Hazelcast involves crucial decisions about synchronous and asynchronous backups, as well as the number of backups to create. Each of these configurations impacts system performance and consistency differently, requiring a careful evaluation based on specific application needs. Ultimately, the flexibility provided by Hazelcast's backup configurations allows for tailored solutions to achieve the desired balance between data safety and performance.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.