# Too many dynamic script compilations within one minute

**URL:** <https://discuss.elastic.co/t/too-many-dynamic-script-compilations-within-one-minute/128956>\
**Category:** Elasticsearch\
**Created:** [April 21, 2018, 6:51am UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-within-one-minute/128956 "2018-04-21T06:51:23Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![idmanish](https://avatars.discourse-cdn.com/v4/letter/i/d2c977/32.png) [@idmanish](https://discuss.elastic.co/u/idmanish)\
**Post date:** [April 21, 2018, 6:51am UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-within-one-minute/128956/1 "2018-04-21T06:51:23Z")

</div>

I am facing "max\_compilations\_per\_minute" error.

`TransportError(500, '{"error":{"root_cause":[{"type":"circuit_breaking_exception","reason":"[script] Too many dynamic script compilations within one minute, max: [15/min]; please use on-disk, indexed, or scripts with parameters instead; this limit can be changed by the [script.max_compilations_per_minute] setting","bytes_wanted":0,"bytes_limit":0},`

This is mainly happening due to the painless script we are using in in search queries registered as percolator.

Since we are using regex in our queries Indexed or parameter script is not an option.

Re-indexing of data is not an option for us.  
How can we avoid this circuit break exception happening mainly due to "max\_compilations\_per\_minute".

Alternative or worst solution:-  
Is there any max limit for setting "max\_compilations\_per\_minute" recommended

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [April 22, 2018, 4:23am UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-within-one-minute/128956/2 "2018-04-22T04:23:44Z")

</div>

> [@idmanish](#):
>
> Since we are using regex in our queries Indexed or parameter script is not an option.

I am not following what you are saying here. Mind giving an example of your search with script in which indexed and parameter can't be used?

---

<div class="post-metadata">

**Author:** ![idmanish](https://avatars.discourse-cdn.com/v4/letter/i/d2c977/32.png) [@idmanish](https://discuss.elastic.co/u/idmanish)\
**Post date:** [April 23, 2018, 10:54am UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-within-one-minute/128956/3 "2018-04-23T10:54:34Z")

</div>

Here is one of sample query.

```
   {
   "query": {
      "bool": {
         "must": [
            {
               "query_string": {
                  "query": "a*",
                  "quote_analyzer": "standard",
                  "allow_leading_wildcard": "true",
                  "analyze_wildcard": "true",
                  "default_operator": "AND",
                  "phrase_slop": 1
               }
            },
            {
               "script": {
                  "script": {
                     "source": "Pattern p = /\\s?a\\S*?/i; p.matcher(params._source.summary ?: '').find();",
                     "lang": "painless"
                  }
               }
            }
         ]
      }
   }
}
```

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [April 23, 2018, 11:16am UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-within-one-minute/128956/4 "2018-04-23T11:16:57Z")

</div>

Instead of using the native regex syntax, use `Pattern. compile` which accepts the pattern as a string that could come from a parameter. For instance, `Pattern. compile(params.regex, "i")`. Then you can use this as a stored script.

---

<div class="post-metadata">

**Author:** ![anoopvalluthadam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anoopvalluthadam/32/30286_2.png) [@anoopvalluthadam](https://discuss.elastic.co/u/anoopvalluthadam)\
**Post date:** [April 24, 2018, 1:33am UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-within-one-minute/128956/5 "2018-04-24T01:33:26Z")

</div>

Did you mean something like this?

```
{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                "query_string": {
                  "query": "a*",
                  "quote_analyzer": "standard",
                  "allow_leading_wildcard": "true",
                  "analyze_wildcard": "true",
                  "default_operator": "AND",
                  "phrase_slop": 1
                }
              },
              {
                "script": {
                  "script": {
                    "lang": "painless",
                    "source": """Pattern p = Pattern.compile(params.param1) ;p.matcher(doc['headline'].value ?: '').find();""",
                    "params": {
                      "param1": "/\\s?a\\S*?/i"
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![anoopvalluthadam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anoopvalluthadam/32/30286_2.png) [@anoopvalluthadam](https://discuss.elastic.co/u/anoopvalluthadam)\
**Post date:** [April 24, 2018, 1:34am UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-within-one-minute/128956/6 "2018-04-24T01:34:40Z")

</div>

by the way, can you share the official documentation about this `Pattern. compile(params.regex, "i")`

Documentation says:

> Regular expression constants are directly supported. To ensure fast performance, this is the only mechanism for creating patterns. Regular expressions are always constants and compiled efficiently a single time.

---

<div class="post-metadata">

**Author:** ![idmanish](https://avatars.discourse-cdn.com/v4/letter/i/d2c977/32.png) [@idmanish](https://discuss.elastic.co/u/idmanish)\
**Post date:** [April 24, 2018, 1:34am UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-within-one-minute/128956/7 "2018-04-24T01:34:42Z")

</div>

It seems Pattern. compile is not supported in painless script.

[https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-syntax.html#patterns](https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-syntax.html#patterns)  
Or I am missing something.  
I am using version 5.6

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [April 24, 2018, 2:17am UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-within-one-minute/128956/8 "2018-04-24T02:17:25Z")

</div>

Sorry for dropping the ball here. Indeed `Pattern.compile` is not supported, which makes it technically impossible to create a stored painless index with a variable `Pattern`, like the way I have described.

There is a performance concern in blocking the `Pattern.compile`, since it makes the regex compilation dynamic. So I can think of a couple of alternatives:

1. If you have a known and small number of patterns, then you can create a stored script that knows all of them (and selects on using a variable), or maybe many stored scripts, which one for each pattern.
2. If you don't have a known set of patterns or the list is just too big, then consider implementing your script as a [native script](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/modules-scripting-engine.html) and use some internal LRU caching when dynamically compiling the pattern.

---

<div class="post-metadata">

**Author:** ![anoopvalluthadam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anoopvalluthadam/32/30286_2.png) [@anoopvalluthadam](https://discuss.elastic.co/u/anoopvalluthadam)\
**Post date:** [April 24, 2018, 2:40am UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-within-one-minute/128956/9 "2018-04-24T02:40:46Z")

</div>

> [@thiago](#):
>
> If you don't have a known set of patterns or the list is just too big, then consider implementing your script as a native script and use some internal LRU caching when dynamically compiling the pattern.

we are left with [native script](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/modules-scripting-engine.html) since we have a huge set of wildcard queries(obviously now it will be regex script).

So, you suggest that in our case use [native script](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/modules-scripting-engine.html) and increase the LRU cache.

what if we increase `script.cache.max_size` and `script.max_compilations_per_minute` - will that help if we have more than 100K (increases but not in huge numbers) of dynamic scripts?

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [April 24, 2018, 2:56am UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-within-one-minute/128956/10 "2018-04-24T02:56:12Z")

</div>

You can certainly increase `script.max_compilations_per_minute` so Elasticsearch would allow you running hundreds of thousands of scripts per minutes but that also means a potential huge CPU spike that will hurt performance.

I suggest that you benchmark and verify if you can live with the high CPU usage (if any at all). If that does not works for you, then I suggest that you follow the native script with LRU strategy.

---

<div class="post-metadata">

**Author:** ![anoopvalluthadam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anoopvalluthadam/32/30286_2.png) [@anoopvalluthadam](https://discuss.elastic.co/u/anoopvalluthadam)\
**Post date:** [April 24, 2018, 2:59am UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-within-one-minute/128956/11 "2018-04-24T02:59:31Z")

</div>

@thiago Thanks for your input.

---

<div class="post-metadata">

**Author:** ![anoopvalluthadam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anoopvalluthadam/32/30286_2.png) [@anoopvalluthadam](https://discuss.elastic.co/u/anoopvalluthadam)\
**Post date:** [April 25, 2018, 4:45am UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-within-one-minute/128956/12 "2018-04-25T04:45:20Z")

</div>

> [@thiago](#):
>
> If you have a known and small number of patterns, then you can create a stored script that knows all of them (and selects on using a variable), or maybe many stored scripts, which one for each pattern.

Stored script:

```
POST _scripts/536672d3cface3ddbb63666bf1b6030f
{
  "script": {
      "lang": "painless",
      "source": "Pattern p = /\\s?a\\S*?/i; p.matcher(params._source.summary ?: '').find();"
   }
}

```

I am using the ID of the stored script in the query like

```
{
   "query": {
      "bool": {
         "must": [
            {
               "bool": {
                  "must": [
                     {
                        "query_string": {
                           "query": "a*",
                           "quote_analyzer": "standard",
                           "allow_leading_wildcard": "true",
                           "analyze_wildcard": "true",
                           "default_operator": "AND",
                           "phrase_slop": 1
                        }
                     },
                     {
                        "script": {
                           "script": {
                              "id": "536672d3cface3ddbb63666bf1b6030f"
                           }
                        }
                     }
                  ]
               }
            }
         ]
      }
   }
}

```

All these queries are stored in a percolator. If a document comes, it will percolated. That . time it fails with `[script] Too many dynamic script compilations within one minute, max`

any solution?

Even if the scripts are stored in the index, will it `execute` each and every time?

I hope these settings are for cluster wide, I thought of making two or more indexes and do the percolation so that per minute compilation will for each and every index. Since documentation does not say anything about caching much like where it resides how Elasticsearch is using that, I am little confused here!

---

<div class="post-metadata">

**Author:** ![Hamza\_BENNANI](https://avatars.discourse-cdn.com/v4/letter/h/bc8723/32.png) [@Hamza\_BENNANI](https://discuss.elastic.co/u/Hamza_BENNANI)\
**Post date:** [April 25, 2018, 4:25pm UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-within-one-minute/128956/13 "2018-04-25T16:25:03Z")

</div>

You can increase `script.max_compilations_per_minute`. Here is an example which increases the parameter to 100

```
PUT _cluster/settings
{
    "transient" : {
        "script.max_compilations_per_minute" : 100
    }
}

```

Also, If you are using hard-coding values try to use named parameters instead. Scripts containing hard-coding values have to be recompiled every time the values changes. For more details see [https://www.elastic.co/guide/en/elasticsearch/reference/5.5/modules-scripting-using.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/modules-scripting-using.html)

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [April 25, 2018, 9:44pm UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-within-one-minute/128956/14 "2018-04-25T21:44:31Z")

</div>

The query you have presented should **not** be the source of the `Too many dynamic script compilations within one minute...` error that you are getting now. It is something else.

By using a script id like that the script is not dynamically compiled anymore (it is compiled only once, when it was registered using the `_script` API) so there is no reason to trigger the `script.max_compilations_per_minute` threshold.

---

<div class="post-metadata">

**Author:** ![anoopvalluthadam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/anoopvalluthadam/32/30286_2.png) [@anoopvalluthadam](https://discuss.elastic.co/u/anoopvalluthadam)\
**Post date:** [April 26, 2018, 3:55am UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-within-one-minute/128956/15 "2018-04-26T03:55:33Z")

</div>

> [@thiago](#):
>
> he query you have presented should not be the source of the Too many dynamic script compilations within one minute... error that you are getting now. It is something else.

Yeah, after you told that it is compiling and storing, it is obvious that when we search in percolator it should not make any issue.

These are the scenarios:

1. Registration API: it will register 1000s of scripted query in `percolator` index. Sometimes this is failing saying that `5000/min` is reached. Make sense because we are registering(indexing) and while indexing it will compile. So it might be crossing the limit of execution.
2. If that is the case percolator has 100K of queries and all are compiled since we are using `scripted query`. So when we search in `percolator` it should not throw this error of per minute execution issue. Suppose if it is making this issue you are saying that it might be the issue in the script, not about the compilation limit, isn't it? Sometimes I have seen that if there is any issue in the script it will throw this same error and not only me lots of people complained about this before. Anyway wrong error message from Elasticsearch.

---

<div class="post-metadata">

**Author:** ![thiago](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/thiago/32/32096_2.png) [@thiago](https://discuss.elastic.co/u/thiago)\
**Post date:** [April 30, 2018, 3:47am UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-within-one-minute/128956/16 "2018-04-30T03:47:06Z")

</div>

Indeed the error you are seeing should because you are hitting the limits when registering the scripts. In this case, if this registration flood happens on a low frequency, then it is fine to bump the limit to a very high number so you don't hit the limit when it happens.

Regarding your second comment, I am not aware of any issues that makes Elasticsearch throw the limit error while shadowing another underlying issue. If Yiu can provide me an example or a way to reproduce it would be great.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 28, 2018, 3:47am UTC](https://discuss.elastic.co/t/too-many-dynamic-script-compilations-within-one-minute/128956/17 "2018-05-28T03:47:15Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
