
Defining and Executing a Prepared Query
Prepared Queries are stored in Consul’s catalog by calling the/v1/query API endpoint:
my-query is available as:
- DNS:
my-query.query.consul - HTTP API:
GET /v1/query/<QueryID>/execute
DNS queries to
*.query.consul support standard A/AAAA record lookups and SRV records for service resolution.Filtering Services: A Dealership Analogy
Imagine you visit a car dealership with thousands of vehicles. You only want:- A car (not a truck)
- Painted red
- The latest model

- Health: only passing health checks
- Service: e.g.,
web-app - Tags: e.g.,
v6.4
Example: web-app-v64 Prepared Query
Save the following JSON to web-app-v64-query.json:
web-app-v64.query.consul or call:
Multi-Datacenter Failover Policies
In federated environments, you may want your queries to automatically fail over to other datacenters if no local instances are healthy.
Failover Policy Types

Static Failover Policy
Consul tries the local datacenter first, thendc2, thendc3.
Dynamic Failover Policy
Consul measures round-trip time and queries the two nearest datacenters if the local ones have no healthy instances.
Hybrid Failover Policy
In a hybrid policy, each datacenter is queried at most once per execution.
Comparing Service Discovery Methods
Consul supports three main ways to locate services: