

# Fair-share scheduling policies
<a name="job_scheduling"></a>

The AWS Batch scheduler evaluates when, where, and how to run jobs that are submitted to a job queue. If you don’t specify a scheduling policy when you create a job queue, the AWS Batch job scheduler defaults to a first-in, first-out (FIFO) strategy. A FIFO strategy might cause important jobs to get “stuck” behind jobs that were submitted earlier. By specifying a different scheduling policy, you can allocate compute resources according to your specific needs.

**Note**  
If you want to schedule the specific order that jobs are run in, use the `[dependsOn](https://docs.aws.amazon.com/batch/latest/APIReference/API_SubmitJob.html#Batch-SubmitJob-request-dependsOn)` parameter in [SubmitJob](https://docs.aws.amazon.com/batch/latest/APIReference/API_SubmitJob.html) to specify the dependencies for each job.

If you create a scheduling policy and attach it to a job queue, fair-share scheduling is turned on. If the job queue has a scheduling policy, the scheduling policy determines the order that jobs are run in. For more information, see [Use fair-share scheduling policies to assign share identifiers](scheduling-policies.md).

**Topics**
+ [Use share identifiers to identify workloads](share-identifiers.md)
+ [Use fair-share scheduling policies to assign share identifiers](scheduling-policies.md)
+ [Use fair-share scheduling to help schedule jobs](fair-share-scheduling.md)
+ [Tutorial: Create a fair-share scheduling policy](create-scheduling-policy.md)
+ [Reference: Fair-share scheduling policy template](scheduling-policy-template.md)

# Use share identifiers to identify workloads
<a name="share-identifiers"></a>

You can use share identifiers to tag jobs and differentiate between users and workloads. The AWS Batch scheduler tracks usage for each share identifier by using the `(T * weightFactor)` formula, where *`T`* is the vCPU usage over time. The scheduler picks jobs with the lowest usage from the share identifier. You can use a share identifier without overriding it.

**Note**  
Share identifiers are unique within a job queue and are not aggregated across job queues. 

You can set fair-share scheduling priority to configure the order that jobs are run in on a share identifier. Jobs with a higher scheduling priority are scheduled first. If you don’t specify a fair-share scheduling policy, all jobs that are submitted to the job queue are scheduled in FIFO order. When you submit a job, you can’t specify a share identifier or fair-share scheduling priority.

**Note**  
Attached compute resources are allocated equally among all share identifiers unless explicitly overridden.

# Use fair-share scheduling policies to assign share identifiers
<a name="scheduling-policies"></a>

You can use fair-share scheduling policies to configure how compute resources in a job queue are allocated between users or workloads. Using fair-share scheduling policies, you can assign different share identifiers to workloads or users. AWS Batch assigns each share identifier a percentage of the total resources that are available during a period of time. 

The fair-share percentage is calculated using the `shareDecaySeconds` and `shareDistribution` values. You can add time to the fair-share analysis by assigning a share decay time to the policy. Adding time gives more weight to time and less to the defined weight. You can hold compute resources in reserve for share identifiers that aren't active by specifying a compute reservation. For more information, see [SchedulingPolicyDetail](https://docs.aws.amazon.com/batch/latest/APIReference/API_SchedulingPolicyDetail.html).

# Use fair-share scheduling to help schedule jobs
<a name="fair-share-scheduling"></a>

Fair-share scheduling provides a set of controls to help schedule jobs. For more information about scheduling policy parameters, see [SchedulingPolicyDetail](https://docs.aws.amazon.com/batch/latest/APIReference/API_SchedulingPolicyDetail.html). 
+ **Share decay seconds –** The period of time (in seconds) that the AWS Batch scheduler uses to calculate a fair-share percentage for each share identifier. A value of zero indicates that only current usage is measured. A longer decay time gives more weight to time.
**Note**  
The period of time for decay is calculated as: *`shareDecaySeconds + OrderMinutes`* where *`OrderMinutes `*is the time in the order in minutes.
+ **Compute reservation –** Prevents jobs in a single share identifier from using up all the resources that are attached to the job queue. The reserved ratio is `(computeReservation/100)^ActiveFairShares` where `ActiveFairShares` is the number of active share identifiers.
**Note**  
If a share identifier has jobs in a `SUBMITTED`, `PENDING`, `RUNNABLE`, `STARTING`, or `RUNNING` state, it's considered an active share identifier. After the period of time for decay expires, a share identifier is considered inactive.
+ **Weight factor –** The weight factor for the share identifier. The default value is 1. A lower value lets jobs from the share identifier run or gives additional runtime to the share identifier. For example, jobs that use a share identifier with a weight factor of 0.125 (1/8) are assigned eight times the compute resources of jobs that use a share identifier with a weight factor of 1.
**Note**  
You only need to define this attribute when you need to update the default weight factor of 1.

When the job queue is active and processing jobs, you can review a list of the first 100 `RUNNABLE` jobs through the Job queue snapshot. For more information, see [View a job queue in AWS Batch](job_queue_viewing_status.md). 

# Tutorial: Create a fair-share scheduling policy
<a name="create-scheduling-policy"></a>

Before you can create a job queue with a scheduling policy, you must create a scheduling policy. When you create a fair-share scheduling policy, you associate one or more share identifiers or share identifier prefixes with weights for the queue and optionally assign a decay period and compute reservation to the policy.

**To create a fair-share scheduling policy**

1. Open the AWS Batch console at [https://console.aws.amazon.com/batch/](https://console.aws.amazon.com/batch/).

1. From the navigation bar, select the Region to use.

1. In the navigation pane, choose **Scheduling policies**, **Create**.

1. For **Name**, enter a unique name for your scheduling policy. Up to 128 letters (uppercase and lowercase), numbers, hyphens, and underscores are allowed.

1. (Optional) For **Share decay seconds**, enter an integer value for the fair-share scheduling policy's share decay time. A longer share decay time will use considerably more compute resource usage over a longer time when scheduling jobs. This can allow jobs using a share identifier to temporarily use more compute resources than the weight for that share identifier would allow if that share identifier had not recently been using compute resources.

1. (Optional) For **Compute reservation**, enter an integer value for the fair-share scheduling policy's compute reservation. The compute reservation will hold some vCPUs in reserve to be used for share identifiers that are not currently active.

   The reserved ratio is `(computeReservation/100)^ActiveFairShares` where *ActiveFairShares* is the number of active share identifiers.

   For example, a `computeReservation` value of 50 indicates that AWS Batch should reserve 50% of the maximum available VCPU if there is only one share identifier, 25% if there are two share identifiers, and 12.5% if there are three share identifiers. A `computeReservation` value of 25 indicates that AWS Batch should reserve 25% of the maximum available VCPU if there is only one share identifier, 6.25% if there are two share identifiers, and 1.56% if there are three share identifiers.

1. In the **Share attributes** section, you can specify the share identifier and weight for each share identifier to associate with the fair-share scheduling policy.

   1. Choose **Add share identifier**.

   1. For **Share identifier**, specify the share identifier. If the string ends with '\$1', this becomes a share identifier prefix used to match share identifiers for jobs. All of the share identifiers and share identifier prefixes in a scheduling policy must be unique and cannot overlap. For example, you can't have share identifiers prefix 'UserA\$1' and share identifier 'UserA1' in the same fair-share scheduling policy.

   1. For **Weight factor**, specify the relative weight for the share identifier. The default value is 1.0. A lower value has a higher priority for compute resources. If a share identifier prefix is used, jobs with share identifiers that start with the prefix will share the weight factor. This effectively increases the weight factor for those jobs, lowering their individual priority but maintaining the same weight factor for the share identifier prefix.

1. (Optional) In the **Tags** section, you can specify the key and value for each tag to associate with the scheduling policy. For more information, see [Tag your AWS Batch resources](using-tags.md).

1. Choose **Submit** to finish and create your scheduling policy.

# Reference: Fair-share scheduling policy template
<a name="scheduling-policy-template"></a>

An empty fair-share scheduling policy template is shown below. You can use this template to create your scheduling policy which can then be saved to a file and used with the AWS CLI `--cli-input-json` option. For more information about these parameters, see [CreateSchedulingPolicy](https://docs.aws.amazon.com/batch/latest/APIReference/API_CreateSchedulingPolicy.html) in the *AWS Batch API Reference*.

**Note**  
You can generate a job queue template with the following AWS CLI command.  

```
$ aws batch create-scheduling-policy --generate-cli-skeleton
```

```
{
    "name": "",
    "fairsharePolicy": {
        "shareDecaySeconds": 0,
        "computeReservation": 0,
        "shareDistribution": [
            {
                "shareIdentifier": "",
                "weightFactor": 0.0
            }
        ]
    },
    "tags": {
        "KeyName": ""
    }
}
```