Java ReplatformingPerformance - SnapStart2 min

Learning Goals

  • Learn how Lambda SnapStart improves first invoke latency

SnapStart

For latency-sensitive functions where most latency is caused by cold starts, Lambda SnapStart for Java can improve startup performance by up to 10x. With SnapStart, Lambda initializes your function when you create a Lambda Version. Lambda then takes a snapshot of the entire environment and stores it in a multi-tiered cache. When a function needs to create a new execution environment, it does so by restoring the snapshot, this results in much faster start-up times. There is no additional cost for using SnapStart.

The Starting up faster with AWS Lambda SnapStart workshop gives you a step-by-step guide of how SnapStart addresses Java function performance, how to enable SnapStart with IaC, and how to customize your SnapStart behavior with runtime hooks.

Stale TCP connections

Your function will be snapshotted once the initialization phase is complete. If in that phase you create TCP connections, for example, to Redis, Postgres or MySQL, then the snapshot will keep this state. When your function is restored on another host, which could also be days or weeks later, then there will be no TCP connection. The error handling of your client library will most likely handle this stale connection and recreate it.

You can proactively manage this by using SnapStart runtime hooks. They allow you to run code just before and just after the snapshotting process. You can then write a hook to close any open TCP connections made by client libraries you are using.

import org.crac.Core;
import org.crac.Resource;

public class Example implements RequestHandler<Object, Object>, Resource {

    public Example() {
        Core.getGlobalContext().register(this);
    }
    
    @Override
    public Object handleRequest(Object input, Context context) {
        
    }
    
    @Override
    public void beforeCheckpoint(org.crac.Context<? extends Resource> context) throws Exception {
        // Close any open connections
    }
    
    @Override
    public void afterRestore(org.crac.Context<? extends Resource> context) throws Exception {
        // Restart connections
    }
}

Created by:

Mark Sailes
Mark SailesSenior Specialist SA, Serverless at AWS and Java fan.