# 2.5 second startup time for Java elasticsearch client on AWS Lambda

**URL:** <https://discuss.elastic.co/t/2-5-second-startup-time-for-java-elasticsearch-client-on-aws-lambda/177674>\
**Category:** Elasticsearch\
**Created:** [April 19, 2019, 10:30pm UTC](https://discuss.elastic.co/t/2-5-second-startup-time-for-java-elasticsearch-client-on-aws-lambda/177674 "2019-04-19T22:30:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![evdh](https://avatars.discourse-cdn.com/v4/letter/e/b2d939/32.png) [@evdh](https://discuss.elastic.co/u/evdh)\
**Post date:** [April 19, 2019, 10:30pm UTC](https://discuss.elastic.co/t/2-5-second-startup-time-for-java-elasticsearch-client-on-aws-lambda/177674/1 "2019-04-19T22:30:58Z")

</div>

We're using AWS Lambda (Java) and the elasticsearch client to connect to a hosted elasticsearch instance on AWS. I encounter a long wait on the first request of about 2.5 seconds (on top of a cold start). After that it is very quick. I can't really figure out where this delay is coming from and I'm trying to optimize it.

```auto
private void testPerformanceElasticSearch() throws Exception {
        log.info("1. Before testing elasticsearch client");
        AWS4Signer signer = new AWS4Signer();
        signer.setServiceName("es");
        signer.setRegionName("eu-west-1");
        HttpRequestInterceptor interceptor = new AWSRequestSigningApacheInterceptor("es", signer, new DefaultAWSCredentialsProviderChain());
        String endpoint = "https://" + Utils.getEnvironmentVariable("ELASTIC_SEARCH_ENDPOINT");
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient.builder(HttpHost.create(endpoint)).setHttpClientConfigCallback(hacb -> hacb.addInterceptorLast(interceptor)));
        log.info("2. After getting elasticsearch client");
        log.info("3. Before doing a elasticsearch query");
        log.info("4");
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        log.info("5");
        TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery("userId", "abc");
        log.info("6");
        boolQueryBuilder.must(termsQueryBuilder);
        log.info("7");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        log.info("8");
        searchSourceBuilder.query(boolQueryBuilder);
        log.info("9");
        SearchRequest searchRequest = new SearchRequest("users");
        log.info("10");
        searchRequest.source(searchSourceBuilder);
        log.info("11");
        restHighLevelClient.search(searchRequest);
        log.info("12");
        log.info("13. After testing elasticsearch");
}

```

And then I get logging like this; you can see between '5' and '6' there is more than a 2 second delay which I can't really place:

```auto
17:16:06.871INFO[PlacesPerformance] 1. Before testing elasticsearch client
17:16:06.932INFO[PlacesPerformance] 2. After getting elasticsearch client
17:16:06.933INFO[PlacesPerformance] 3. Before doing a elasticsearch query
17:16:06.935INFO[PlacesPerformance] 4
17:16:06.942INFO[PlacesPerformance] 5
17:16:09.179INFO[PlacesPerformance] 6
17:16:09.179INFO[PlacesPerformance] 7
17:16:09.181INFO[PlacesPerformance] 8
17:16:09.181INFO[PlacesPerformance] 9
17:16:09.183INFO[PlacesPerformance] 10
17:16:09.183INFO[PlacesPerformance] 11
17:16:09.362INFO[PlacesPerformance] 12
17:16:09.362INFO[PlacesPerformance] 13. After testing elasticsearch

```

I also tried it in a few different ways, because whenever I run the code in a lambda, I experience the 2.5 second delay when constructing the request (not even executing it). Locally, it works fine though. I tried the following:

```auto
1. Local against local elasticsearch. No delay.
2. Local against AWS elasticsearch. No delay.
3. Lambda with signing request. DELAY.
4. Lambda without signing request. DELAY.
5. Lambda with a 'match all' query. DELAY
6. Lambda with a http address. DELAY.
7. Lambda with a custom runtime. DELAY.
8. Lambda with standard Java 8 runtime. DELAY.

```

Any suggestions on how to improve this? Should I use standard rest calls instead maybe?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 23, 2019, 7:44am UTC](https://discuss.elastic.co/t/2-5-second-startup-time-for-java-elasticsearch-client-on-aws-lambda/177674/2 "2019-04-23T07:44:32Z")

</div>

May be there is an inherent cost of using Lambdas?  
What if you try to build the project with something like [https://quarkus.io/](https://quarkus.io/) ?

---

<div class="post-metadata">

**Author:** ![evdh](https://avatars.discourse-cdn.com/v4/letter/e/b2d939/32.png) [@evdh](https://discuss.elastic.co/u/evdh)\
**Post date:** [April 23, 2019, 10:12am UTC](https://discuss.elastic.co/t/2-5-second-startup-time-for-java-elasticsearch-client-on-aws-lambda/177674/3 "2019-04-23T10:12:51Z")

</div>

Thanks! That looks a very interesting project actually; have been looking into GraalVM and hope to be able to use that later on with a Custom Runtime on AWS lambdas. However, this is not yet available. But our complete current architecture is build on Serverless (not containers) and I can't change that around now. It must have to do something with the runtime environment in which the client is instantiated, as I don't have the delay locally either and I was hoping somebody might have some insight on why this happens on Lambdas and how I can optimize/prevent this delay.

---

<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 21, 2019, 10:12am UTC](https://discuss.elastic.co/t/2-5-second-startup-time-for-java-elasticsearch-client-on-aws-lambda/177674/4 "2019-05-21T10:12:56Z")

</div>

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