Automatic compaction
The Coordinator indexing period, , controls the frequency of compaction tasks. The default indexing period is 30 minutes, meaning that the Coordinator first checks for segments to compact at most 30 minutes from when auto-compaction is enabled. This time period affects other Coordinator duties including merge and conversion tasks. To configure the auto-compaction time period without interfering with indexingPeriod
, see .
At every invocation of auto-compaction, the Coordinator initiates a segment search to determine eligible segments to compact. When there are eligible segments to compact, the Coordinator issues compaction tasks based on available worker capacity. If a compaction task takes longer than the indexing period, the Coordinator waits for it to finish before resuming the period for segment search.
As a best practice, you should set up auto-compaction for all Druid datasources. You can run compaction tasks manually for cases where you want to allocate more system resources. For example, you may choose to run multiple compaction tasks in parallel to compact an existing datasource for the first time. See for additional details and use cases.
This topic guides you through setting up automatic compaction for your Druid cluster. See the examples for common use cases for automatic compaction.
You can enable automatic compaction for a datasource using the web console or programmatically via an API. This process differs for manual compaction tasks, which can be submitted from the Tasks view of the web console or the .
Use the web console to enable automatic compaction for a datasource as follows.
- Click Datasources in the top-level navigation.
- In the Compaction column, click the edit icon for the datasource to compact.
- In the Compaction config dialog, configure the auto-compaction settings. The dialog offers a form view as well as a JSON view. Editing the form updates the JSON specification, and editing the JSON updates the form field, if present. Form fields not present in the JSON indicate default values. You may add additional properties to the JSON for auto-compaction settings not displayed in the form. See for supported settings for auto-compaction.
- Refresh the Datasources view. The Compaction column for the datasource changes from “Not enabled” to “Awaiting first run.”
The following screenshot shows the compaction config dialog for a datasource with auto-compaction enabled.
To disable auto-compaction for a datasource, click Delete from the Compaction config dialog. Druid does not retain your auto-compaction configuration.
Compaction configuration API
To disable auto-compaction for a datasource, send a to /druid/coordinator/v1/config/compaction/{dataSource}
. Replace {dataSource}
with the name of the datasource for which to disable auto-compaction. For example:
curl --location --request DELETE 'http://localhost:8081/druid/coordinator/v1/config/compaction/wikipedia'
You can configure automatic compaction dynamically without restarting Druid. The automatic compaction system uses the following syntax:
Most fields in the auto-compaction configuration correlate to a typical . The following properties only apply to auto-compaction:
skipOffsetFromLatest
taskPriority
taskContext
Since the automatic compaction system provides a management layer on top of manual compaction tasks, the auto-compaction configuration does not include task-specific properties found in a typical Druid ingestion spec. The following properties are automatically set by the Coordinator:
type
: Set tocompact
.id
: Generated using the task type, datasource name, interval, and timestamp. The task ID is prefixed withcoordinator-issued
.context
: Set according to the user-providedtaskContext
.
For more details on each of the specs in an auto-compaction configuration, see Automatic compaction dynamic configuration.
Compaction tasks may be interrupted when they interfere with ingestion. For example, this occurs when an ingestion task needs to write data to a segment for a time interval locked for compaction. If there are continuous failures that prevent compaction from making progress, consider one of the following strategies:
- Set
skipOffsetFromLatest
to reduce the chance of conflicts between ingestion and compaction. See more details in this section below. - Increase the priority value of compaction tasks relative to ingestion tasks. Only recommended for advanced users. This approach can cause ingestion jobs to fail or lag. To change the priority of compaction tasks, set
taskPriority
to the desired priority value in the auto-compaction configuration. For details on the priority values of different task types, see Lock priority.
The Coordinator compacts segments from newest to oldest. In the auto-compaction configuration, you can set a time period, relative to the end time of the most recent segment, for segments that should not be compacted. Assign this value to . Note that this offset is not relative to the current time but to the latest segment time. For example, if you want to skip over segments from five days prior to the end time of the most recent segment, assign "skipOffsetFromLatest": "P5D"
.
To set skipOffsetFromLatest
, consider how frequently you expect the stream to receive late arriving data. If your stream only occasionally receives late arriving data, the auto-compaction system robustly compacts your data even though data is ingested outside the skipOffsetFromLatest
window. For most realtime streaming ingestion use cases, it is reasonable to set skipOffsetFromLatest
to a few hours or a day.
Set frequency of compaction runs
druid.coordinator.dutyGroups=["compaction"]
druid.coordinator.compaction.duties=["compactSegments"]
druid.coordinator.compaction.period=PT60S
After the Coordinator has initiated auto-compaction, you can view compaction statistics for the datasource, including the number of bytes, segments, and intervals already compacted and those awaiting compaction. The Coordinator also reports the total bytes, segments, and intervals not eligible for compaction in accordance with its .
In the web console, the Datasources view displays auto-compaction statistics. The Tasks view shows the task information for compaction tasks that were triggered by the automatic compaction system.
To get statistics by API, send a GET request to /druid/coordinator/v1/compaction/status
. To filter the results to a particular datasource, pass the datasource name as a query parameter to the request—for example, /druid/coordinator/v1/compaction/status?dataSource=wikipedia
.
The following examples demonstrate potential use cases in which auto-compaction may improve your Druid performance. See more details in Compaction strategies. The examples in this section do not change the underlying data.
You have a stream set up to ingest data with HOUR
segment granularity into the wikistream
datasource. You notice that your Druid segments are smaller than the recommended segment size of 5 million rows per segment. You wish to automatically compact segments to DAY
granularity while leaving the latest week of data not compacted because your stream consistently receives data within that time period.
The following auto-compaction configuration compacts existing HOUR
segments into DAY
segments while leaving the latest week of data not compacted:
Update partitioning scheme
For your wikipedia
datasource, you want to optimize segment access when regularly ingesting data without compromising compute time when querying the data. Your ingestion spec for batch append uses dynamic partitioning to optimize for write-time operations, while your stream ingestion partitioning is configured by the stream service. You want to implement auto-compaction to reorganize the data with a suitable read-time partitioning using . Based on the dimensions frequently accessed in queries, you wish to partition on the following dimensions: , countryName
, namespace
.
The following auto-compaction configuration compacts updates the wikipedia
segments to use multi-dimension range partitioning:
{
"tuningConfig": {
"partitionsSpec": {
"type": "range",
"partitionDimensions": [
"channel",
"countryName",
"namespace"
],
"targetRowsPerSegment": 5000000
}
}
}
- for an overview of compaction and how to set up manual compaction in Druid.
- Segment optimization for guidance on evaluating and optimizing Druid segment size.