Implementing Backup and Recovery
1. Understanding Backup Strategies
| Strategy | Description | Use Case | Notes |
|---|---|---|---|
| Full Backup | Backs up entire Kafka cluster data including all topics, partitions, and configurations. | Disaster recovery, migration to new cluster | Time-consuming; requires significant storage space |
| Incremental Backup | Backs up only the data that has changed since last backup. | Regular backups with minimal overhead | Requires tracking of changes; more complex to implement |
| Snapshot Backup | Uses Kafka's built-in snapshot mechanism to capture state at a point in time. | Point-in-time recovery | Requires Kafka version 2.5+; leverages log compaction |
| Replica-Based Backup | Relies on Kafka's replication factor to maintain copies across brokers. | High availability, fault tolerance | Does not protect against cluster-level failures; only data loss within brokers |
Example: Full Backup Command Using Kafka Tools
kafka-run-class.sh kafka.tools.BackupProducer \
--broker-list "localhost:9092" \
--topics "topic1,topic2" \
--output-dir "/backup/kafka"
2. Backing Up Topic Data
| Property | Description | Default Value | Notes |
|---|---|---|---|
| log.dirs | Directory where topic logs are stored | /tmp/kafka-logs | Can be a comma-separated list of directories |
| log.retention.hours | How long to retain logs before deletion | 168 (7 days) | Configurable in hours or milliseconds |
| log.cleanup.policy | Policy for cleaning up logs (delete or compact) | delete | Can be set to compact or delete |
Example: Configuring Log Retention
{
"log.retention.hours": 168,
"log.cleanup.policy": "delete",
"log.retention.bytes": -1
}
confluent backup or custom scripts that replicate topic data to another storage system.
Backup Strategy Summary
- Use
log.dirsto define storage paths for topic logs - Configure
log.retention.hoursto control data lifecycle - Implement external tools or scripts for full topic backup and restore
3. Backing Up Consumer Offsets
Consumer offsets in Kafka are managed by the __consumer_offsets topic, which is automatically created and managed by Kafka. To back up consumer offsets, you can either:
- Export the offsets using Kafka tools like
kafka-run-class.shwith theorg.apache.kafka.tools.ExportConsumerOffsetsclass. - Use the
kafka-consumer-groups.shcommand-line tool to describe and reset consumer groups.
Example: Exporting Consumer Offsets
kafka-run-class.sh org.apache.kafka.tools.ExportConsumerOffsets \
--bootstrap-server localhost:9092 \
--group my-group \
--output-file consumer-offsets.json
Example: Importing Consumer Offsets
kafka-run-class.sh org.apache.kafka.tools.ImportConsumerOffsets \
--bootstrap-server localhost:9092 \
--group my-group \
--input-file consumer-offsets.json
| Option | Description | Notes |
|---|---|---|
| --bootstrap-server | Bootstrap server address | Required for connecting to Kafka cluster |
| --group | Consumer group ID | Used to identify the consumer group |
| --output-file | Output file path | JSON file to store offset data |
| --input-file | Input file path | JSON file to restore offset data |
4. Backing Up Schemas
Schema backups in Kafka are essential for maintaining data integrity and ensuring that schema evolution can be safely managed. While Kafka itself does not store schemas directly, it integrates with schema registries like Confluent Schema Registry to manage and store schemas.
| Schema Registry API Endpoint | Description | Notes |
|---|---|---|
| /schemas | Retrieve all schemas | GET request to list all registered schemas |
| /schemas/ids/{id} | Get schema by ID | Retrieve a specific schema using its unique identifier |
| /subjects | List all subjects | GET request to list all schema subjects |
| /subjects/{subject}/versions | List schema versions for a subject | GET request to list all versions of a specific schema subject |
5. Backing Up Configuration
| Configuration Type | Description | Backup Method | Notes |
|---|---|---|---|
| Broker Configs | Settings for individual Kafka brokers | Export to JSON/YAML files | Include server.properties and any custom configs |
| Topic Configs | Settings for topics like retention, partitions | Use kafka-configs.sh CLI |
Can be backed up with --describe flag |
| Consumer Group Configs | Offsets and consumer group settings | Use kafka-consumer-groups.sh |
Offsets stored in internal __consumer_offsets topic |
Example: Backing Up Topic Configurations
kafka-configs.sh --bootstrap-server localhost:9092 --entity-type topics --entity-name my-topic --describe --underlying
6. Implementing Point-in-Time Recovery
Point-in-Time Recovery (PITR) in Kafka allows you to restore a consumer group's offsets to a specific point in time, enabling recovery from data loss or corruption. This feature is particularly useful for maintaining data consistency and ensuring that consumers can reprocess messages from a known good state.
| Configuration | Description | Default Value | Notes |
|---|---|---|---|
| log.flush.interval.messages | Number of messages after which the log is flushed to disk | 10000 |
Controls how frequently messages are persisted to disk, affecting recovery speed |
| log.flush.interval.ms | Interval in milliseconds after which the log is flushed to disk | 900000 |
Ensures data durability even if log.flush.interval.messages is not reached |
| offsets.retention.minutes | Retention period for consumer group offsets in minutes | 1440 |
Defines how long offsets are retained before being eligible for deletion |
| offsets.retention.check.interval.ms | How often to check for expired offsets | 600000 |
Frequency of offset retention cleanup cycles |
Example: Configuring Point-in-Time Recovery
{log.flush.interval.messages: 10000,log.flush.interval.ms: 900000,offsets.retention.minutes: 1440}
offsets.retention.minutes based on your recovery window requirements.
Key Considerations for PITR
- Ensure
log.flush.interval.msis set to a reasonable value for durability - Configure
offsets.retention.minutesto align with your recovery window - Monitor disk usage to prevent log retention from consuming excessive storage
- Use
kafka-consumer-groups.shto inspect and reset consumer group offsets for PITR
7. Restoring from Backup
Example: Restore a Kafka Topic from Backup
kafka-topics.sh --create --topic restored-topic \
--bootstrap-server localhost:9092 \
--replication-factor 3 \
--partitions 12
kafka-streams-application --restore-from-backup \
--backup-path /backups/topic-backup-2025-01-15 \
--topic restored-topic
| Step | Description | Command |
|---|---|---|
| 1. Prepare Topic | Create topic with same configuration as backup | kafka-topics.sh --create --topic restored-topic --bootstrap-server localhost:9092 --replication-factor 3 --partitions 12 |
| 2. Restore Data | Use backup tool to restore messages to topic | kafka-streams-application --restore-from-backup --backup-path /backups/topic-backup-2025-01-15 --topic restored-topic |
8. Recovering Consumer Groups
| Property | Description | Default Value |
|---|---|---|
| group.id | Unique identifier for the consumer group | None |
| session.timeout.ms | Timeout for consumer session | 45000 |
| heartbeat.interval.ms | Interval between heartbeats | 3000 |
| auto.offset.reset | What to do when no offset is found | latest |
Example: Consumer Group Recovery Configuration
const consumer = new KafkaConsumer({
bootstrapServers: 'localhost:9092',
groupId: 'my-group-id',
sessionTimeout: 60000,
heartbeatInterval: 5000,
autoOffsetReset: 'earliest'
});
group.id remains consistent to maintain offset tracking.
Recovery Steps
- Stop all consumers in the group
- Reset offsets to a known safe point or use
earliestfor reprocessing - Restart consumers with the same
group.id - Monitor for any lag or errors in processing
9. Testing Recovery Procedures
Example: Basic Recovery Test Setup
# Create a test topic with multiple partitions
bin/kafka-topics.sh --create --topic test-recovery-topic \
--partitions 3 --replication-factor 2 \
--bootstrap-server localhost:9092
# Produce test messages
bin/kafka-console-producer.sh --topic test-recovery-topic \
--bootstrap-server localhost:9092 <<EOF
Message 1
Message 2
Message 3
EOF
# Verify messages are produced
bin/kafka-console-consumer.sh --topic test-recovery-topic \
--from-beginning --bootstrap-server localhost:9092
| Recovery Test | Description | Command | Status |
|---|---|---|---|
| Topic Recreation | Verify topic can be recreated after deletion | bin/kafka-topics.sh --delete --topic test-recovery-topic |
NEW |
| Consumer Group Recovery | Test consumer group state restoration | bin/kafka-consumer-groups.sh --describe --group test-group |
DEPRECATED |
| Broker Failover | Simulate broker failure and recovery | bin/kafka-server-stop.sh |
EXPERIMENTAL |
Recovery Test Procedure
- Stop one or more Kafka brokers
- Verify consumer groups are rebalanced
- Restart stopped brokers
- Confirm data consistency across partitions
- Validate consumer lag is minimal
10. Automating Backup Process
Example: Kafka Backup Script
#!/bin/bash
# Backup Kafka topics and configurations
BACKUP_DIR="/opt/kafka/backups"
DATE=$(date +%Y%m%d_%H%M%S)
# Create backup directory
mkdir -p $BACKUP_DIR/$DATE
# Backup topic configurations
kafka-configs.sh --bootstrap-server localhost:9092 --entity-type topics --describe --all > $BACKUP_DIR/$DATE/topic_configs.txt
# Backup consumer groups
kafka-consumer-groups.sh --bootstrap-server localhost:9092 --describe --all > $BACKUP_DIR/$DATE/consumer_groups.txt
# Backup topic data using kafka-run-class.sh
kafka-run-class.sh kafka.tools.Exporter --topic-list "my-topic" --output-dir $BACKUP_DIR/$DATE/topic_data
echo "Backup completed: $BACKUP_DIR/$DATE"
| Component | Description | Automation Tool |
|---|---|---|
| kafka-configs.sh | Exports topic configurations and broker settings | bash |
| kafka-consumer-groups.sh | Exports consumer group metadata and offsets | bash |
| kafka-run-class.sh | Runs Kafka tools like Exporter for topic data backup | bash |