| Crates.io | dataflow-rs |
| lib.rs | dataflow-rs |
| version | 2.0.3 |
| created_at | 2025-04-12 05:00:22.171674+00 |
| updated_at | 2025-10-09 03:55:29.152201+00 |
| description | A lightweight, rule-driven workflow engine for building powerful data processing pipelines and nanoservices in Rust. Extend it with your custom tasks to create robust, maintainable services. |
| homepage | https://github.com/GoPlasmatic/dataflow-rs |
| repository | https://github.com/GoPlasmatic/dataflow-rs |
| max_upload_size | |
| id | 1630630 |
| size | 263,860 |
A high-performance workflow engine for building data processing pipelines in Rust with zero-overhead JSONLogic evaluation.
Dataflow-rs is a Rust library for creating high-performance data processing pipelines with pre-compiled JSONLogic and zero runtime overhead. It features a modular architecture that separates compilation from execution, ensuring predictable low-latency performance. With built-in multi-threading support through ThreadedEngine and high-performance parallel processing via RayonEngine, it provides excellent vertical scaling capabilities. Whether you're building REST APIs, processing Kafka streams, or creating sophisticated data transformation pipelines, Dataflow-rs provides enterprise-grade performance with minimal complexity.
Here's a quick example to get you up and running.
Cargo.toml[dependencies]
dataflow-rs = "1.0.8"
serde_json = "1.0"
Workflows are defined in JSON and consist of a series of tasks.
{
"id": "data_processor",
"name": "Data Processor",
"tasks": [
{
"id": "transform_data",
"function": {
"name": "map",
"input": {
"mappings": [
{
"path": "data.user_name",
"logic": { "var": "temp_data.name" }
},
{
"path": "data.user_email",
"logic": { "var": "temp_data.email" }
}
]
}
}
}
]
}
use dataflow_rs::{Engine, Workflow};
use dataflow_rs::engine::message::Message;
use serde_json::json;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Define workflows
let workflow_json = r#"{ ... }"#; // Your workflow JSON from above
let workflow = Workflow::from_json(workflow_json)?;
// Create engine with workflows (immutable after creation)
let mut engine = Engine::new(
vec![workflow], // Workflows to compile and cache
None, // Custom functions (optional)
None, // Retry config (optional)
);
// Process a single message
let mut message = Message::new(&json!({}));
engine.process_message(&mut message)?;
println!("✅ Processed result: {}", serde_json::to_string_pretty(&message.data)?);
Ok(())
}
For high-performance multi-threaded processing, use the built-in ThreadedEngine:
use dataflow_rs::{ThreadedEngine, Workflow};
use dataflow_rs::engine::message::Message;
use serde_json::json;
use std::sync::Arc;
use std::thread;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Define your workflows
let workflow_json = r#"{ ... }"#; // Your workflow JSON
let workflow = Workflow::from_json(workflow_json)?;
// Create ThreadedEngine with 4 worker threads
let engine = Arc::new(ThreadedEngine::new(
vec![workflow], // Workflows
None, // Custom functions (optional)
None, // Retry config (optional)
4, // Number of worker threads
));
// Process messages concurrently from multiple client threads
let mut handles = Vec::new();
for i in 0..1000 {
let engine = Arc::clone(&engine);
let handle = thread::spawn(move || {
let message = Message::new(&json!({"id": i}));
engine.process_message_sync(message)
});
handles.push(handle);
}
// Wait for all messages to complete
for handle in handles {
handle.join().unwrap()?;
}
println!("✅ Processed 1000 messages with ThreadedEngine!");
Ok(())
}
The ThreadedEngine provides:
For CPU-intensive workloads requiring maximum parallelism, use RayonEngine:
use dataflow_rs::{RayonEngine, Workflow, Message};
use serde_json::json;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Define your workflows
let workflow_json = r#"{ ... }"#; // Your workflow JSON
let workflow = Workflow::from_json(workflow_json)?;
// Create RayonEngine (uses all CPU cores by default)
let engine = RayonEngine::new(
vec![workflow], // Workflows
None, // Custom functions (optional)
None, // Retry config (optional)
);
// Process batch of messages in parallel
let messages: Vec<Message> = (0..10000)
.map(|i| Message::new(&json!({"id": i})))
.collect();
let results = engine.process_batch(messages);
// Handle results
for result in results {
match result {
Ok(message) => println!("Processed: {:?}", message.data),
Err(e) => eprintln!("Error: {:?}", e),
}
}
println!("✅ Processed 10000 messages with RayonEngine!");
Ok(())
}
The RayonEngine provides:
Use Engine when:
Use ThreadedEngine when:
Use RayonEngine when:
The v3.0 architecture focuses on simplicity and performance through clear separation of concerns:
Dataflow-rs achieves optimal performance through architectural improvements:
Run the included benchmarks to test performance on your hardware:
cargo run --example benchmark # Comprehensive comparison
cargo run --example threaded_benchmark # ThreadedEngine performance
cargo run --example rayon_benchmark # RayonEngine performance
The benchmarks compare single-threaded Engine vs multi-threaded ThreadedEngine vs parallel RayonEngine with various configurations, providing detailed performance metrics and scaling analysis.
You can extend the engine with your own custom logic by implementing the AsyncFunctionHandler trait:
use async_trait::async_trait;
use dataflow_rs::engine::{AsyncFunctionHandler, FunctionConfig, error::Result, message::{Change, Message}};
use datalogic_rs::DataLogic;
use serde_json::{json, Value};
use std::collections::HashMap;
use std::sync::Arc;
pub struct MyCustomFunction;
#[async_trait]
impl AsyncFunctionHandler for MyCustomFunction {
async fn execute(
&self,
message: &mut Message,
config: &FunctionConfig,
datalogic: Arc<DataLogic>,
) -> Result<(usize, Vec<Change>)> {
// Your custom logic here (can be async or sync)
println!("Hello from a custom async function!");
// Modify message data
message.data["processed"] = json!(true);
// Return status code and changes for audit trail
Ok((200, vec![Change {
path: Arc::from("data.processed"),
old_value: Arc::new(json!(null)),
new_value: Arc::new(json!(true)),
}]))
}
}
// Register when creating the engine:
let mut custom_functions = HashMap::new();
custom_functions.insert(
"my_custom_function".to_string(),
Box::new(MyCustomFunction) as Box<dyn AsyncFunctionHandler + Send + Sync>
);
let engine = Engine::new(
workflows,
Some(custom_functions), // Custom async functions
);
We welcome contributions! Feel free to fork the repository, make your changes, and submit a pull request. Please make sure to add tests for any new features.
Dataflow-rs is developed by the team at Plasmatic. We're passionate about building open-source tools for data processing.
This project is licensed under the Apache License, Version 2.0. See the LICENSE file for more details.