Skip to content

How to reduce the cold start time when using MySQL with Lambda?

I am trying to deploy a REST API with Java, using AWS Lambda, API Gateway and Amazon RDS (MySQL). Below is my Lambda class

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.aaaa.beans.AccountingType;
import java.util.ArrayList;
import java.util.List;
import static java.util.Collections.list;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

 * @author User
public class GetAllAccountTypesLambda {

   static final String DB_URL = "jdbc:mysql://************";
   static final String USER = "*****";
   static final String PASS = "*****";
   static final String QUERY = "SELECT * from accounting_type";
   static Connection conn = null;
   public GetAllAccountTypesLambda()
      try {
         conn = DriverManager.getConnection(DB_URL, USER, PASS);
      } catch (Exception e) {

   public APIGatewayProxyResponseEvent getAllAccountTypes(APIGatewayProxyResponseEvent request)
         throws JsonProcessingException, ClassNotFoundException {
      AccountingType acc = new AccountingType();
      ObjectMapper objectMapper = new ObjectMapper();
      List<AccountingType> list = new ArrayList<>();

      try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(QUERY);) {
         // Extract data from result set

         while ( {
            // Retrieve by column name

      } catch (SQLException e) {

      String writeValueAsString = objectMapper.writeValueAsString(list);
      return new APIGatewayProxyResponseEvent().withStatusCode(200).withBody(writeValueAsString);


My pom file

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">
    <name>aaa REST API</name>

        <!-- -->


Now, checkout the loading time tracked from X Ray, which we usually call as cold start

enter image description here

It looks like the init process takes time. When further observed, I noticed most of the time is taken by the MySQL Connector trying to initialize. If we remove that, this will be done in 500ms to 600ms.

How can I make sure this starts much faster, probably in milliseconds? Anything to do with the MySQL connector?



Don’t optimize blindly for cold starts. If your Lambda is going to be used in an application that is used regularly (e.g. more than 1 request per 10 seconds), the cold starts could be only 0.1% of the calls, while the other 99.9% will be processed by a warmed up lambda.

If your application is not used so much, you could still force this by e.g. sending a ping to the Lambda every 5 minutes, forcing it to keep warm.

See Is it possible to keep an AWS Lambda function warm? for one way to do it. A Cloud Guru did a more extended version of it (for a javascript Lambda):

The pinging does increase Amazon’s overhead cost for a low-use Lambda, since they have to reserve memory for it for X hours, and they can only charge you for a couple of seconds of actual runtime for each ping. But apparently, that’s a cost they are willing to take.

5 People found this is helpful