2

I want to use caching between c# Lambda calls, I understand if the Lambda starts from cold this wouldn't be possible, but if it is called many times within a short period can we use a cache.

I have tried by creating a static variable, but this seems to be reinitialised after each call?

Anyone have any ideas? Here is an example where I want to increase the TestCache variable by 1 each time, however this always returns 1.

using Amazon.Lambda.APIGatewayEvents;
using Amazon.Lambda.Core;
using System.Collections.Generic;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace MyTestLambda
{
    public class Global
    {
        private readonly Dictionary<string, string> allOriginHeaders = new Dictionary<string, string>
            {
                { "Access-Control-Allow-Origin", "*" }
            };

        public static int TestCache = 0;

        public APIGatewayProxyResponse Handler(APIGatewayProxyRequest apigProxyEvent)
        {
            TestCache += 1;

            return new APIGatewayProxyResponse
            {
                Body = TestCache.ToString(),
                StatusCode = 200,
                Headers = allOriginHeaders
            };
        }
    }
}
  • UPDATED *

I have realised that actually my code does cache, but there must be multiple instances created. In my case I was load testing throwing 500 requests at once, which all came back the same, however when I tested one by one doing it manually, then the numbers did increase. I am going to look at elastic caching as a better approach going forward as per the answer below.

2

3 Answers 3

5

Some form of caching can be achieved via AWS Lambda execution context:

After a Lambda function is executed, AWS Lambda maintains the execution context for some time in anticipation of another Lambda function invocation.

To use that, your variables must be declared outside of the function's handler:

Objects declared outside of the function's handler method remain initialized, providing additional optimization when the function is invoked again. For example, if your Lambda function establishes a database connection, instead of reestablishing the connection, the original connection is used in subsequent invocations.

However, the problem with using the execution context is that AWS does not provide any guarantee on how long you can use it:

When you write your Lambda function code, do not assume that AWS Lambda automatically reuses the execution context for subsequent function invocations.

Thus, the caching solution based on lambda execution context could work, but it would be limited in many ways. For proper caching solutions you would have to look outside of lambda, for example store your serialized objects in ElastiCache, DynamoDB or more.

I have tried by creating a static variable, but this seems to be reinitialised after each call?

You haven't provided any code, but a possible reason could be that your variables are initialized in the function handler. As stated above, your object/variables should be declared outside of the function handler to be reused.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer, I have added some code if if this helps for the last point?
@user3284707 Hi, I'm not to familiar with C# code. Maybe TestCache must be outside of the namespace?
1

If you're trying to store values I would advise against relying on static variables for caching, especially as you may have many MicroVMs for your Lambda running at the same time. They're great for reusability of handlers (i.e. the SDK clients, DB clients) but as a cache it might be more appropriate to use another service.

Depending on your use case the following options would be more appropriate:

  • Systems Manager Parameter Store - Use this option if you're setting the same property every time, you can then read the value during any cold start.
  • DynamoDB - Use this option if you have many flattened Key/Values that you might want to read/write to during Lambda invocation
  • ElastiCache (Redis) - Use this option if you're truly trying to create a cache of many objects.

If you use Systems Manager or DynamoDB you will just need to update your code to replace these values, whereas for ElastiCache you would need to configure you Lambda as part of a VPC.

Comments

0

You can store cached data in the ephemeral storage of the lambda. This storage will be wiped when the lambda container is shutdown.

A Lambda container will boot up lambda with your function for each request, and if you are for example fetching large data from DB that all other request can reuse. Store that in the ephemeral file storage in lambda.

Dont forget to have code that will check if the "cache" exists, and fetch it from db if it doesnt, and then store in file for next call.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.