# README
Data Converter Sample
This sample workflow demonstrates how to use custom data converters in Cadence workflows with compression capabilities. The data converter is responsible for serializing and deserializing workflow inputs, outputs, and activity parameters, with the added benefit of data compression to save storage space and bandwidth.
Sample Description
The sample implements a custom compressed JSON data converter that:
- Serializes workflow inputs and activity parameters to JSON format
- Compresses the JSON data using gzip compression to reduce size
- Decompresses and deserializes workflow outputs and activity results from JSON format
- Provides significant storage and bandwidth savings for large payloads
- Demonstrates advanced data converter patterns for production use cases
- Shows real-time compression statistics and size comparisons
The sample includes two workflows:
- Simple Workflow: Processes a basic
MyPayload
struct - Large Payload Workflow: Processes a complex
LargePayload
with nested objects, arrays, and extensive data to demonstrate compression benefits
All data is automatically compressed during serialization and decompressed during deserialization, with compression statistics displayed at runtime.
Key Components
- Custom Data Converter:
compressedJSONDataConverter
implements theencoded.DataConverter
interface with gzip compression - Simple Workflow:
dataConverterWorkflow
demonstrates basic payload processing with compression - Large Payload Workflow:
largeDataConverterWorkflow
demonstrates processing complex data structures with compression - Activities:
dataConverterActivity
andlargeDataConverterActivity
process different payload types - Large Payload Generator:
CreateLargePayload()
creates realistic complex data for compression demonstration - Compression Statistics:
GetPayloadSizeInfo()
shows before/after compression metrics - Tests: Includes unit tests for both simple and large payload workflows
- Compression: Automatic gzip compression/decompression for all workflow data
Steps to Run Sample
-
You need a cadence service running. See details in cmd/samples/README.md
-
Run the following command to start the worker:
./bin/dataconverter -m worker
-
Run the following command to execute the workflow:
./bin/dataconverter -m trigger
You should see:
- Compression statistics showing original vs compressed data sizes
- Workflow logs showing the processing of large payloads
- Activity execution logs with payload information
- Final workflow completion with compression benefits noted
Customization
To implement your own data converter with compression or other features:
- Create a struct that implements the
encoded.DataConverter
interface - Implement the
ToData
method for serialization and compression - Implement the
FromData
method for decompression and deserialization - Register the converter in the worker options
This pattern is useful when you need to:
- Reduce storage costs and bandwidth usage with compression
- Use specific serialization formats for performance or compatibility
- Add encryption/decryption to workflow data
- Implement custom compression algorithms (LZ4, Snappy, etc.)
- Support legacy data formats
- Add data validation or transformation during serialization
Performance Benefits
The compressed data converter provides:
- Storage Savings: Typically 60-80% reduction in data size for JSON payloads
- Bandwidth Reduction: Lower network transfer costs and faster data transmission
- Cost Optimization: Reduced storage costs in Cadence history
- Scalability: Better performance with large payloads