relabel_configs with the hashmod action to split a single job’s targets across multiple Prometheus servers.
Why shard?
- Large scrape jobs with many targets can increase CPU, memory, and scrape latency for a single Prometheus server.
- Sharding distributes scraping load across N Prometheus instances while keeping a single shared target list.
- hashmod: a relabel action that computes
hash(target) % modulusand stores the result in a label. - shard index: the integer (0..N-1) assigned to a Prometheus instance that determines which subset of targets it keeps.
__address__: the label containing the target address (host:port).
- Compute a hashmod of each target address and store it in a temporary label (commonly
__tmp_hashmod). - Keep only targets whose hashmod equals the shard index assigned to that Prometheus instance.
- Repeat the same scrape configuration on each instance, changing only the
keepregexto the instance’s shard index.
relabel_configs with hashmod.
Full common configuration (applies to all Prometheus instances)
- Use
modulus: NwhereNis the number of Prometheus instances (shards). - On each Prometheus instance, set the
keepregexto the instance’s shard index (0..N-1). - The first relabel writes the hashmod result into
__tmp_hashmod. The second relabel filters targets based on that value.
Best practices and considerations
- Set
modulusequal to the number of Prometheus instances. For example, usemodulus: 4for four shards. - Hash-based distribution is near-uniform with many targets. If you have few targets, distribution may be uneven.
- Keep the static target list identical on all Prometheus servers; only the relabel
keepregex differs. - If you add or remove Prometheus instances (change
modulus), hash assignments will change; expect some target movement between instances.
- Confirm shard behavior using Prometheus’s Status → Targets (Target Health) page for each instance. This page shows which targets each Prometheus server is scraping.
- Use the Prometheus UI and logs to verify that each instance scrapes only the expected subset of targets.
- Prometheus relabeling documentation
- Prometheus Status → Targets
- Prometheus best practices for scaling

When choosing the
modulus, make it equal to the number of Prometheus servers you plan to use. For large numbers of targets, hashmod yields an approximately even distribution; for a small number of targets the distribution may be uneven.