Overview
The DynamoDB persistence adapter provides distributed locks, circuit breaker state management, caching, and checkpoint support using AWS DynamoDB as the backend. It uses a single-table design with native TTL support for automatic expiration.Installation
Features
- Distributed Locks: Atomic locks using conditional writes
- Circuit Breaker State: Persistent failure tracking
- Caching: Serverless cache with DynamoDB TTL
- Checkpoints: Full checkpoint support for task recovery
- Idempotency: Prevent duplicate operations (via
DynamoDBIdempotencyAdapter) - Single-Table Design: All data in one table with partition/sort keys
- Native TTL: Automatic expiration via DynamoDB TTL attribute
- Serverless: No infrastructure to manage
Basic Usage
Configuration
DynamoDB Document Client from @aws-sdk/lib-dynamodb
Name of the DynamoDB table to use (table must be pre-created)
Configuration options
Table Schema (Single-Table Design)
You must create the DynamoDB table with the following schema:Key Patterns
- Locks:
pk = LOCK#{prefix}lock:{key},sk = METADATA - Circuit Breaker:
pk = CB#{prefix}circuit:{key},sk = METADATA - Cache:
pk = CACHE#{prefix}cache:{key},sk = METADATA - Checkpoints:
pk = CHECKPOINT#{prefix}{taskId}:{sequence},sk = METADATA
Optional Global Secondary Index
For efficientkeys() queries on cache items, create a GSI:
Lock Provider Methods
acquire(key, ttl, owner?)
Acquires a distributed lock using DynamoDB conditional writes. Automatically expires via TTL.Lock identifier
Time-to-live in milliseconds
Optional lock owner identifier (auto-generated if not provided)
Promise<LockHandle | null> - Lock handle or null if already locked (ConditionalCheckFailedException)
extend(key, ttl, owner)
Extends the TTL of an existing lock.Lock identifier
New time-to-live in milliseconds
Lock owner identifier
Promise<boolean> - True if extended successfully
forceRelease(key)
Forces release of a lock regardless of owner.Lock identifier
Cache Provider Methods
get(key)
Retrieves a cached value. Checks expiration client-side (DynamoDB TTL is eventually consistent).Cache key
Promise<T | null> - Cached value or null if not found/expired
set(key, value, ttl?)
Stores a value in the cache.Cache key
Value to cache (DynamoDB supports complex objects)
Time-to-live in milliseconds (optional)
delete(key)
Removes a cached value.Cache key
has(key)
Checks if a key exists and is not expired.Cache key
Promise<boolean>
clear()
Clears all cached values (scans and deletes in batches).keys(pattern?)
Lists all cache keys matching an optional pattern (requires GSI).Optional regex pattern
Promise<string[]> - Array of matching keys
Checkpoint Provider Methods
save(checkpoint)
Saves a checkpoint for task recovery.Checkpoint data including taskId, sequence, progress, and data
load(checkpointId)
Loads a specific checkpoint by ID (uses Scan).Checkpoint identifier
Promise<Checkpoint<T> | undefined>
loadLatest(taskId)
Loads the most recent checkpoint for a task (requires GSI).Task identifier
Promise<Checkpoint<T> | undefined>
list(taskId)
Lists all checkpoints for a task (requires GSI).Task identifier
Promise<Checkpoint<unknown>[]>
cleanup(taskId, keepCount)
Deletes old checkpoints, keeping only the most recent N.Task identifier
Number of checkpoints to keep
deleteAll(taskId)
Deletes all checkpoints for a task.Task identifier
CloudFormation Template
Connection Example
Best Practices
Lock acquisition uses conditional writes (
attribute_not_exists(pk) OR expiresAt < :now) which are atomic and strongly consistent.Performance Considerations
- Single-table design minimizes RCU/WCU consumption
- TTL automatically deletes expired items (no cost)
- Use on-demand billing for variable workloads
- Consider provisioned capacity + auto-scaling for sustained high throughput
- GSI required for
keys()and checkpoint queries - Batch operations for checkpoint cleanup
Cost Optimization
- Use TTL for automatic cleanup (no write cost)
- Enable point-in-time recovery for production
- Use DynamoDB Streams for audit logging (optional)
- Monitor with CloudWatch metrics
- Consider DynamoDB Global Tables for multi-region HA
Item Structure Examples
Lock Item
Circuit Breaker Item
Cache Item
Related
MongoDB Adapter
MongoDB-based persistence
Redis Adapter
Redis-based persistence