Auto-Sync
Configure automatic background synchronization for telemetry events
Overview
Auto-sync automatically synchronizes your telemetry events to the server in the background, eliminating the need for manual .sync() calls.
Auto-sync is enabled by default when you configure sync credentials.
Quick Start
Configuration
Sync Interval
Configure how often events are synced (default: 60 seconds):
Sync on Shutdown
Control whether a final sync happens when dropping the telemetry instance (default: true):
Disable Auto-Sync
If you prefer manual control:
How It Works
Background Task
When auto-sync is enabled, telemetry-kit spawns a background tokio task that:
- Waits for the configured interval
- Retrieves unsynced events from SQLite
- Sends them to the server in batches
- Marks successfully synced events
- Repeats
Thread Safety
The auto-sync implementation is thread-safe:
- Uses
Arc<Mutex<AutoSyncTask>>for safe concurrent access - Storage operations protected by
RwLock - Atomic bool for shutdown signaling
Graceful Shutdown
Call .shutdown() for graceful cleanup:
This will:
- Stop the background task
- Perform a final sync (if
sync_on_shutdownis true) - Wait for the task to complete
The Drop implementation will stop the background task automatically, but it cannot perform async operations. For guaranteed final sync, call .shutdown() explicitly.
Advanced Configuration
Full Configuration Example
Custom Sync Configuration
For more control over sync behavior, use SyncConfig:
Best Practices
Interval Selection
Choose your sync interval based on your needs:
- High-frequency apps (CLI tools): 5-15 seconds
- Normal apps: 60 seconds (default)
- Low-priority telemetry: 300 seconds (5 minutes)
Handling Failures
Auto-sync includes automatic retry with exponential backoff:
- Failed sync attempts are automatically retried
- Retry count tracked per event
- Events persist until successfully synced
- No data loss even with network issues
DO_NOT_TRACK
Auto-sync respects the DO_NOT_TRACK environment variable:
Examples
Example 1: CLI Application
Example 2: Long-Running Service
Example 3: Conditional Auto-Sync
Troubleshooting
Events Not Syncing
- Check auto-sync is enabled:
- Verify credentials are correct:
-
Check network connectivity to the server
-
View unsynced events:
High Memory Usage
If you're generating events faster than they can sync:
- Increase sync interval:
- Increase batch size:
- Implement event sampling for high-volume scenarios
Performance
Overhead
- Memory: ~1KB per background task
- CPU: Negligible at rest (task sleeps between intervals)
- Network: Batched requests minimize overhead
Benchmarks
| Interval | Events/sec | Memory | CPU |
|---|---|---|---|
| 5s | 1000 | <5MB | <1% |
| 60s | 1000 | <5MB | <0.1% |
| 300s | 1000 | <5MB | <0.1% |
See Also
- CLI Tool - Manage auto-sync from the command line
- API Reference - Complete API documentation
- Examples - Working code examples