Be careful with behaviors that overlap

Rule order is important because of the possibility of creating overlapping rules. Rule order is most important when the behaviors overlap.

As the partner, you are responsible for managing rules to prevent unexpected behavior. Essentially, this means that you need to do both of the following:

  • Keep track of the match conditions within a given “rules” collection.
  • Remember that, the more restrictive a set of match conditions is, the later it should appear in the collection.

For example, if there is a generic match for a “jpg” file extension that defines a time to live (TTL) of seven days, and another match that includes both a path of “/images/*” and a file extension match for “jpg” that defines a TTL of one day, the latter rule should exist after the first in the configuration. Putting the less complex rule later in the list would override all previous matches and the related behaviors. In this example, if the order were reversed the Akamai server would use a seven day TTL, regardless of whether or not the “jpg” object was requested using the “/images/*” path.

Below is an example of a properly-constructed rules collection for the above scenario:

{
    "rules" : [
        {
            "matches" : [
                {
                    "name" : "url-extension",
                    "value" : "jpg"
                }
            ],
            "behaviors" : [
                {
                    "name" : "caching",
                    "type" : "fixed"
                    "string" : "7d"
                }
            ]
        },
        {
            "matches" : [
                {
                    "name" : "url-extension",
                    "value" : "jpg"
                },
                {
                    "name" : "url-wildcard",
                    "value" : "/images/*"
                }
            ],
            "behaviors" : [
                {
                    "name" : "caching",
                    "type" : "fixed",
                    "string" : "1d"
                }
            ]
        }
    ]
}
Note: If there is no overlap in behavior, then the order is irrelevant.

Consider the case where the “/images/*” path and “jpg” file extension matches are used to define a TTL, but another rule has a match for “jpg,” that applies a geographic blacklist behavior for, say, North Korea. These two rules do not share behaviors. So, the less restrictive rule is safe to place later in the list.

Below is an example in which the rule order does not matter, because there is no overlap in behavior:

{ 
    "rules" : [
        {
            "matches" : [
                {
                    "name" : "url-extension",
                    "value" : "jpg"
                }
            ],
            "behaviors" : [
                {
                    "name" : "geo-blacklist",
                    "type" : "country",
                    "value" : "KP"
                }
            ]
        },
        {
            "matches" : [
                {
                    "name" : "url-extension",
                    "value" : "jpg"
                },
                {
                    "name" : "url-wildcard",
                    "value" : "/images/*"
                }
            ],
            "behaviors" : [
                {
                    "name" : "caching",
                    "type" : "fixed"
                    "string" : "1d"
                }
            ]
        }
    ]
}

As the partner, you also need to consider the case when multiple behaviors are applied with overlapping matches. When this is the case, it is generally safe to put the most restrictive matches last in the rules collection, regardless of whether there are overlapping behaviors.