Sub-second queries
over events that just arrived.
Druid answers analytical, slice-and-dice queries over large volumes of events, and data is available for querying as soon as it arrives. It partitions data by time, stores each part in a columnar, indexed format, and processes each query in parallel across the cluster. Follow an event from arrival to query and see what happens when a server goes down.
Apache Druid is a real-time analytics database built for OLAP queries over large volumes of event-oriented data. Data is kept in datasources, partitioned by time and stored in columnar segments, served in parallel by the servers in the cluster.
Real time
Queryable as soon as it arrives
An event read from a stream, such as a Kafka topic, already answers queries before it moves to permanent storage. Batch loading follows the same path.
By time
Only the requested interval
Data is partitioned first by time, and a query reads only the parts that fall within the interval it asks for.
In parallel
One query, many servers
The Broker splits each query among the servers that hold the segments, and each one reads only the rows and columns the query needs.
On this page
- Watch an event reach a query
- Architecture
- Watch a query happen
- The name of a segment
- From datasource to column
- When something changes or fails
- Why Druid
- In the TDP editions
Watch an event reach a query
Press Play or step through. Underlined names lead to the Glossary.
1 The supervisor creates the tasks
A Kafka ingestion is controlled by a supervisor that runs on the Overlord. It creates the indexing tasks that read the topic and tracks their state throughout the ingestion.
In the Glossary: Overlord · Supervisor · Indexing task
- The supervisor creates the tasks. A Kafka ingestion is controlled by a supervisor that runs on the Overlord. It creates the indexing tasks that read the topic and tracks their state throughout the ingestion.
- Each task gets a Peon. The Overlord hands the tasks to the MiddleManagers. Each MiddleManager runs each task in its own Peon, a separate JVM, which isolates the task's resources and logs.
- The Peon reads the topic. The Peon reads events through Kafka partitions and offsets. Before building a segment, it asks the Overlord for the segment's identifier.
- The event already answers queries. As soon as it enters the segment being built, the event is already queryable: the segment is available, but not yet published. Meanwhile, the data is converted to the columnar format, indexed with bitmaps, and compressed.
- The segment is published. Once the segment is fully read, the Peon pushes it to deep storage and publishes it with a record in the metadata store. The Kafka offsets are committed in the same transaction as the segment metadata.
- The Coordinator finds the new segment. The Coordinator polls the metadata store periodically. When it finds a published segment that nobody serves, it picks a Historical and tells it to load the segment.
- The Historical takes over the segment. The Historical downloads the segment from deep storage to the segment cache, announces that it serves it, and starts answering for it. This is the handoff: the task, which was waiting for this moment, exits.
Architecture
Master servers govern, Query servers receive the queries, Data servers ingest and serve. Three external dependencies complete the cluster.
Druid services are organized into three types of server.
What the figure shows:
- The data sources are, in streaming, the Kafka topics and, in batch, files and SQL. The Peons read the streaming and batch sources.
- The clients are analytical applications and APIs. The Router is their entry point.
- The Master server brings together the Overlord and the Coordinator. The Overlord controls ingestion and distributes the ingestion tasks to the MiddleManagers. The supervisors run on it. The Coordinator manages data availability, distributes and balances the segments across the Historicals, and tracks the metadata store.
- The Data servers bring together the MiddleManager and the Historical. The MiddleManagers receive the tasks and run ingestion in their Peons, which send the segments to deep storage. The Historicals serve the data that is already published: they load the segments from deep storage into their segment cache and serve them.
- The Query server brings together the Router and the Broker. The Router is the single entry point and the web console. The Broker distributes the subqueries, sending them to the Historicals and the Peons, and combines the results.
- The metadata store sits outside Druid, with the records of the segments and the tasks.
- The deep storage sits outside Druid, with a copy of all segments.
- ZooKeeper sits outside Druid and handles service discovery and leader election.
Each service is configured and scaled independently, and the failure of one does not immediately affect the others.
Watch a query happen
A query enters through the Router, is split by the Broker, and comes back already aggregated.
1 The Broker already knows who serves what
The Broker discovers the Historicals and the streaming-ingestion Peons through ZooKeeper and asks each one, over HTTP, which segments it serves. With that, it keeps, for each datasource, a timeline of the segments and of who serves them.
In the Glossary: Broker · Historical · Peon · ZooKeeper · Ensemble · Datasource
- The Broker already knows who serves what. The Broker discovers the Historicals and the streaming-ingestion Peons through ZooKeeper and asks each one, over HTTP, which segments it serves. With that, it keeps, for each datasource, a timeline of the segments and of who serves them.
- The query enters through the Router. The client sends the query, in Druid SQL or the native language, to the Router, which forwards it to a Broker. An SQL query is planned on the Broker itself.
- The Broker prunes by time. The Broker identifies the segments that may have data for the query. The list is always pruned by time interval and can also be pruned by other attributes, depending on the datasource's partitioning. Per-segment results already in the Broker's cache are reused.
- One subquery for each server. The Broker sends a rewritten version of the query to each Historical and each Peon that serves the selected segments. Published segments are on the Historicals. Data that has just arrived is on the Peons.
- Each segment reads only what is needed. Within each segment, the indexes point to the rows that match the filters before any data is read. Then, only the rows and columns the query uses are read.
- The Broker merges the partial results. The servers return partial results to the Broker, which merges them into the final response and returns it to the client.
The name of a segment
Every segment has a four-part identifier. In the example, a segment of the clarity-cloud0 datasource, with one hour of data.
Click each part of the identifier.
clarity-cloud0— Datasource- The dataset the segment belongs to, equivalent to a table.
2018-05-21T16:00:00.000Z— Interval start- The start of the time chunk that contains the segment. The length of the interval is the segment granularity defined at ingestion.
2018-05-21T17:00:00.000Z— Interval end- The end of the same time chunk. Start and end together form the segment's time interval.
2018-05-21T15:56:09.909Z— Version- Generally, the moment the set of segments started. A higher version replaces the previous one in the same interval.
1— Partition- The partition number, unique for the same datasource, interval, and version. The first partition of a time chunk, number zero, omits this part of the identifier.
Segments of the same datasource, interval, and version together form the data for that interval. When data in an interval is overwritten, the new set carries the same datasource and interval identification, with a higher version.
From datasource to column
Druid data, from the broadest level to the most concrete: each level contains the next.
Level 1 of 4
Datasource
The dataset that is queried, like a table in a relational database, partitioned first by time.
- Primary timestampThe mandatory time column, stored as __time, by which the data is partitioned and ordered.
- Datasource. The dataset that is queried, like a table in a relational database, partitioned first by time.
- Primary timestamp. The mandatory time column, stored as __time, by which the data is partitioned and ordered.
- Time chunk. A time interval of the datasource, such as a day or an hour, depending on the granularity chosen at ingestion.
- Segment version. Distinguishes the sets of segments in the same interval, so that an overwrite replaces the data all at once.
- Segment. A single file, columnar, indexed, and compressed, with up to a few million rows of the time chunk.
- Rollup. The optional pre-aggregation at ingestion, which merges identical rows and reduces the stored volume.
- Column. The data of each column is kept in separate structures inside the segment, and the query reads only the columns it uses.
- Dimension. The column stored as is, for grouping and filtering, with a dictionary, a list of values, and bitmaps.
- Metric. The column stored in aggregated form, such as a sum or a count.
- Bitmap index. One bitmap per dimension value, which marks the rows that contain that value.
When something changes or fails
Published segments are immutable and always have a copy in deep storage. This is what makes it possible to replace data and servers without losing anything.
An overwrite creates new segments for the same interval, with a higher version. Druid loads the new ones without exposing them, switches all new queries to them at once, and drops the old ones shortly after. See Segment version.
Data not yet published can be discarded, and ingestion resumes from the last offsets committed to the metadata store. Because offsets and segments are committed in the same transaction, each event is published exactly once.
The Coordinator notices that the service has gone missing, treats its segments as dropped, and reassigns them to other Historicals, which download them from deep storage. With replicas, queries remain possible during recovery.
For a while, the Coordinator does not reassign the dropped segments. If the Historical comes back within that interval, it looks in its own segment cache, announces what it finds, and resumes serving, without moving anything in the cluster.
Even if all data servers are lost and re-provisioned, Druid rebuilds itself from the deep storage.
With more than one instance, the leader of each is elected by ZooKeeper, and the others stand by. The Router can forward management requests to the active Coordinator and Overlord.
Why Druid
Columnar
Only the query's columns
Column-oriented storage loads only the columns the query needs, each one optimized for its data type. See Columnar model.
Real time or batch
Data ready on arrival
Ingestion can be real-time or batch, and ingested data is available for querying immediately.
Time partitioning
Queries that read less
Data is partitioned first by time, and optionally by other fields, and queries access only the partitions in the requested interval.
Indexes
Fast filters
Compressed bitmap indexes allow fast filtering and searching across several columns at once.
Self-balancing
Servers come and go
The cluster grows or shrinks as servers are added or removed and rebalances itself in the background, with no downtime. When a server fails, the data is served by others until it is replaced.
Aggregation
Rollup and approximate algorithms
Pre-aggregation at ingestion reduces the stored data, and approximate algorithms for distinct count, ranking, histograms, and quantiles answer with bounded memory. The exact versions remain available.
Druid's architecture combines ideas from data warehouses, time series databases, and log search systems. It works best with event-oriented data and is often used as the database behind the interfaces of analytical applications and high-concurrency APIs that need fast aggregations.
In the TDP editions
| Edition | Version | Documentation |
|---|---|---|
| TDP Datacenter | 32.0.0 | TDP Datacenter components |
In TDP Datacenter
In TDP Datacenter, Druid is the DRUID service, installed and managed by Ambari.
DRUID_BROKER (master, one or more), DRUID_COORDINATOR (master, one or more), DRUID_HISTORICAL (slave, one or more), DRUID_MIDDLEMANAGER (slave, one or more), DRUID_OVERLORD (master, one or more) and DRUID_ROUTER (master, one or more). The DRUID_BROKER requires HDFS_CLIENT (HDFS). The DRUID_COORDINATOR requires HDFS_CLIENT (HDFS). The DRUID_HISTORICAL requires HDFS_CLIENT (HDFS). The DRUID_MIDDLEMANAGER requires HDFS_CLIENT (HDFS). The DRUID_OVERLORD requires HDFS_CLIENT (HDFS). The DRUID_ROUTER requires HDFS_CLIENT (HDFS). Component category.
Each Druid service has its own configuration in Ambari, from druid-coordinator to druid-router, and what applies to all of them is in druid-common.
The deep storage type is configurable in druid.storage.type, among local, S3, and HDFS, and the directory in druid.storage.storageDirectory.
The metadata store type, MySQL or PostgreSQL, is configurable in druid.metadata.storage.type, with the connection in druid.metadata.storage.connector.connectURI and the database name in database_name.
The connection to the ZooKeeper ensemble is configurable in druid.zk.service.host, the base znode of Druid in druid.zk.paths.base, and the path under which services announce themselves in druid.discovery.curator.path.
The segment cache locations are configurable in druid.segmentCache.locations, the directory in which the Historical records the segments it serves in druid.segmentCache.infoDir, and the volume of segments each Historical accepts in druid.server.maxSize.
Use of the results cache can be enabled on the Broker, in druid.broker.cache.useCache, and on the Historicals, in druid.historical.cache.useCache.
The interval between Coordinator cycles is configurable in druid.coordinator.period, and the Coordinator can also take on the Overlord role, in druid.coordinator.asOverlord.enabled. On the Overlord, the task execution mode is configurable in druid.indexer.runner.type, and where their state is stored, in druid.indexer.storage.type.
The maximum number of tasks per MiddleManager is configurable in druid.worker.capacity, the tasks' working directory in druid.indexer.task.baseTaskDir, and the resumption of tasks after a restart in druid.indexer.task.restoreTasksOnRestart.
Forwarding of management requests to the active Coordinator and Overlord can be enabled in druid.router.managementProxy.enabled.
The memory of each service is configurable in druid-env, such as druid.broker.jvm.heap.memory and druid.historical.jvm.heap.memory.
The extensions loaded by Druid are listed in druid.extensions.loadList.
The metrics destination, including Ambari Metrics, is configurable in druid.emitter, with the Ambari Metrics server in druid.emitter.ambari-metrics.hostname. The log directory is configurable in druid_log_dir, and rotation in druid-logrotate. See Observability in TDP Datacenter.
The HTTPS connector can be enabled in druid.enableTlsPort. With Kerberos, the Druid principal and keytab are configurable in druid.hadoop.security.kerberos.principal and druid.hadoop.security.kerberos.keytab, and the security extensions in druid.security.extensions.loadList. See Security in TDP Datacenter.
Trino queries Druid through the connector configurable in use.druid, OpenMetadata catalogs its metadata, Airflow integrates with it through the Druid provider, and NiFi, through the Druid components.
Next steps
TDP Datacenter
TDP Datacenter Architecture
The services of the cluster managed by Ambari and how they relate.
Sources
Apache Druid Apache Druid - Introduction Apache Druid - Architecture Apache Druid - Storage overview Apache Druid - Segments Apache Druid - Deep storage Apache Druid - Query processing Apache Druid - Ingestion overview Apache Druid - Apache Kafka ingestion Apache Druid - ZooKeeper