Skip to content

feat(idempotency): add automatic priming for DynamoDB persistence store#2376

Open
dcabib wants to merge 3 commits intoaws-powertools:mainfrom
dcabib:feat/idempotency-automatic-priming
Open

feat(idempotency): add automatic priming for DynamoDB persistence store#2376
dcabib wants to merge 3 commits intoaws-powertools:mainfrom
dcabib:feat/idempotency-automatic-priming

Conversation

@dcabib
Copy link

@dcabib dcabib commented Feb 2, 2026

Description

Implements automatic class preloading via CRaC hooks for the powertools-idempotency-dynamodb module to improve SnapStart cold start performance.

This PR adds automatic priming support to complement the existing invoke priming that was implemented in #1861.

Changes

  • ✅ Add ClassPreLoader.preloadClasses() call to DynamoDBPersistenceStore.beforeCheckpoint()
  • ✅ Generate and include classesloaded.txt with 8,726 classes
  • ✅ Add powertools-common dependency for ClassPreLoader
  • ✅ Add Maven profile generate-classesloaded-file for regenerating the classes file
  • ✅ Add unit tests for CRaC hooks (beforeCheckpoint() and afterRestore())
  • ✅ Update Priming.md with idempotency-dynamodb example
  • ✅ Add null check and logging for defensive error handling

Implementation Details

This implementation combines both priming strategies:

  1. Invoke priming (already existed): Exercises DynamoDB SDK paths with PUT/GET/DELETE operations
  2. Automatic priming (new): Preloads 8,726 classes from classesloaded.txt during SnapStart checkpoint

The combined approach ensures optimal cold start performance by:

  • Warming up DynamoDB client connection pools
  • Preloading all necessary classes into memory
  • Exercising critical code paths before the snapshot is taken

Pattern Consistency

Follows the same pattern as implemented in:

Key differences justified for persistence layer:

  • Exception handling: Wraps priming in try-catch to ensure checkpoint never fails (critical for persistence operations)
  • Null check: Guards against disabled idempotency scenario
  • Logging: Logs exceptions for debugging while maintaining fail-safe behavior

Testing

  • ✅ Unit tests pass: testBeforeCheckpointDoesNotThrowException() and testAfterRestoreDoesNotThrowException()
  • ✅ Example application builds successfully
  • ✅ All existing tests continue to pass

Verification

To verify this works with SnapStart:

  1. Deploy a Lambda function with SnapStart enabled using idempotency-dynamodb
  2. Check CloudWatch Logs for priming execution during INIT phase
  3. Observe reduced cold start times compared to non-primed deployments

Related Issues

Fixes #1997

Maintainer Confirmation

As confirmed by @phipag in #1997 (comment):

"Option 1 sounds good. Only add it in -dynamodb for now."


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Implements automatic class preloading via CRaC hooks for the
idempotency-dynamodb module to improve SnapStart cold start performance.

Changes:
- Add ClassPreLoader.preloadClasses() call to DynamoDBPersistenceStore.beforeCheckpoint()
- Generate and include classesloaded.txt with 8,726 classes
- Add powertools-common dependency for ClassPreLoader
- Add Maven profile generate-classesloaded-file
- Add unit tests for CRaC hooks
- Update Priming.md with idempotency-dynamodb example
- Add null check and logging for defensive error handling

This combines both invoke priming (warming DynamoDB SDK paths)
and automatic priming (preloading classes) for optimal performance.

Follows the same pattern as implemented in powertools-metrics (aws-powertools#1861),
powertools-tracing (aws-powertools#2345), and powertools-kafka (aws-powertools#2145).

Fixes aws-powertools#1997

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Copy link
Contributor

@phipag phipag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @dcabib,

just a small comment but otherwise good work!

// This is unexpected but we must continue without any interruption

// Automatic priming - preload classes from classesloaded.txt
ClassPreLoader.preloadClasses();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better to move the preloading of classes as the first statement in this block in case the invoke priming fails we will still have pre-loaded the classes.

Per review feedback from @phipag: ensures class preloading completes
even if DynamoDB invoke priming operations fail.
@dcabib
Copy link
Author

dcabib commented Feb 3, 2026

Done! 👍

@phipag
Copy link
Contributor

phipag commented Feb 3, 2026

Thanks @dcabib!

I noticed the GraalVM native tests fail now: https://github.com/aws-powertools/powertools-lambda-java/actions/runs/21632501577/job/62352262341?pr=2376.

This is because we are now using mockito to mock the CRaC context. similar to the other PRs, for example the metrics one you need to set the mockito-subclass mock maker in pom.xml. THis is the only mock maker compatible with GraalVM native tests.

- Add mockito-subclass dependency to idempotency-dynamodb module in both
  generate-graalvm-files and graalvm-native profiles
- Update CRaC priming tests to pass null context instead of mocking to
  avoid GraalVM reflection configuration requirements
@dcabib
Copy link
Author

dcabib commented Feb 4, 2026

Done! 👍 (hope it's ok right now)

@sonarqubecloud
Copy link

sonarqubecloud bot commented Feb 4, 2026

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature request: Priming for powertools-idempotency

2 participants