Optimal Segment Size
For Druid to operate well under heavy query load, it is important for the segment file size to be within the recommended range of 300-700 MB. The time interval is configurable in thesegmentGranularity parameter of the granularitySpec.
Segment File Structure
Segment files are columnar: the data for each column is laid out in separate data structures. By storing each column separately, Druid decreases query latency by scanning only those columns actually needed for a query.Column Types
There are three basic column types:Timestamp
Stores the time dimension of your data
Dimensions
Support filtering and group-by operations
Metrics
Numeric values for aggregation
Dimension Column Data Structures
Dimension columns support filter and group-by operations, so each dimension requires three data structures:Dictionary
Maps values (always treated as strings) to integer IDs, allowing compact representation of the list and bitmap values
Example: Page Column Data Structures
The bitmap is different from the dictionary and list: the dictionary and list grow linearly with the size of the data, but the size of the bitmap section is the product of data size and column cardinality. There is one bitmap per separate column value.
Handling Null Values
String columns always store the null value if present in any row as id 0, the first position in the value dictionary and an associated entry in the bitmap value indexes. Numeric columns also store a null value bitmap index to indicate the null valued rows, which is used to null check aggregations and for filter matching null values.Segments with Different Schemas
Druid segments for the same datasource may have different schemas. If a string column (dimension) exists in one segment but not another, queries that involve both segments still work:- Default mode: Queries for the segment without the dimension behave as if the dimension contains only blank values
- SQL-compatible mode: Queries for the segment without the dimension behave as if the dimension contains only null values
Multi-Value Columns
A multi-value column allows a single row to contain multiple strings for a column. You can think of it as an array of strings.If a row has more than one value for a column, its entry in the list is an array of values. Additionally, a row with n values in the list has n non-zero valued entries in bitmaps.
Compression
Druid uses LZ4 by default to compress blocks of values for string, long, float, and double columns. Druid uses Roaring to compress bitmaps for string columns and numeric null values. Druid also supports Concise bitmap compression. For string column bitmaps, the differences between using Roaring and Concise are most pronounced for high cardinality columns. In this case, Roaring is substantially faster on filters that match many values.Segment Identification
Segment identifiers typically contain:- Segment datasource
- Interval start time (ISO 8601 format)
- Interval end time (ISO 8601 format)
- Version information
- Partition number (if sharded beyond time range)
datasource_intervalStart_intervalEnd_version_partitionNum
Segment ID Examples
Multiple segments for the same interval:Sharding
Multiple segments can exist for a single time interval and datasource. These segments form a block for an interval.Segment Components
A segment contains several files:version.bin
version.bin
4 bytes representing the current segment version as an integer. For example, for v9 segments the version is
0x0, 0x0, 0x0, 0x9.meta.smoosh
meta.smoosh
A file containing metadata (filenames and offsets) about the contents of the other
smoosh files.XXXXX.smoosh
XXXXX.smoosh
Smoosh (
.smoosh) files contain concatenated binary data. This file consolidation reduces the number of file descriptors that must be open when accessing data. The files are 2 GB or less in size to remain within the limit of a memory-mapped ByteBuffer in Java.Smoosh files contain:- Individual files for each column in the data, including one for the
__timecolumn - An
index.drdfile that contains additional segment metadata
In the codebase, segments have an internal format version. The current segment format version is v9.
Implications of Updating Segments
Druid uses versioning to manage updates to create a form of multi-version concurrency control (MVCC). When segments are updated:v2segments are loaded into the cluster as soon as they are built- They replace
v1segments for the period of time the segments overlap - Before
v2segments are completely loaded, the cluster may contain a mixture ofv1andv2segments - Queries may hit a mixture of
v1andv2segments during the transition