Crate organization
Thecrates/ directory contains all Rust code, with each crate serving a specific purpose in the system.
Main binary
local_backend
- Path:
crates/local_backend/ - Binary:
convex-local-backend - Purpose: Main application server and entry point
- Starts the HTTP/WebSocket server using Axum
- Initializes the runtime and all subsystems
- Provides routing for API endpoints
- Serves the dashboard UI
- Handles admin operations
- Manages deployment state
Core infrastructure crates
runtime
- Path:
crates/runtime/ - Purpose: Async runtime abstraction layer
- Task spawning and scheduling
- Timers and timeouts
- Thread pool management
- Testing with simulated time
ProdRuntime) wraps Tokio, while testing uses a simulated runtime for deterministic tests.
common
- Path:
crates/common/ - Purpose: Shared utilities and primitives
- Error handling utilities
- Logging and tracing setup
- Knobs (configuration constants)
- HTTP client abstractions
- Common traits and types
errors
- Path:
crates/errors/ - Purpose: Error type definitions
- User errors (invalid input, etc.)
- System errors (internal failures)
- Transient errors (retryable failures)
metrics
- Path:
crates/metrics/ - Purpose: Observability and monitoring
- Prometheus metrics integration
- Custom metric types (counters, histograms, gauges)
- Performance tracking
- Resource usage monitoring
Data layer crates
database
- Path:
crates/database/ - Purpose: Core database engine
- Transaction implementation with ACID guarantees
- Table and schema registry
- Snapshot isolation and MVCC
- Query execution engine
- Subscription tracking for reactivity
- Index coordination
- Read and write set tracking
transaction.rs: Transaction implementationdatabase.rs: Main database structtable_registry.rs: Table metadata managementschema_registry.rs: Schema validationsubscription.rs: Reactive subscription handlingquery.rs: Query execution
storage
- Path:
crates/storage/ - Purpose: Persistence abstraction
- Trait-based storage interface
- Streaming read/write operations
- Transaction log support
- Snapshot management
value
- Path:
crates/value/ - Purpose: Core data type system
ConvexValue: Enum of all supported typesConvexObject: Map of field names to valuesConvexArray: Array of values- Document IDs and table names
- Field paths and names
- Serialization/deserialization
- Size tracking for resource limits
model
- Path:
crates/model/ - Purpose: Domain model and business logic
- Tables and documents
- Indexes and queries
- Schemas and validators
- Deployments and environments
- User-defined functions
- Configuration structures
Indexing and search crates
indexing
- Path:
crates/indexing/ - Purpose: Index abstraction and implementation
- B-tree index structures
- Index registry and metadata
- Range query support
- Index maintenance coordination
search
- Path:
crates/search/ - Purpose: Full-text and vector search
- Text search indexes
- Vector similarity search
- Search index building and maintenance
- Query parsing and execution
text_search
- Path:
crates/text_search/ - Purpose: Text search specifics
- Tokenization and stemming
- BM25 scoring
- Fuzzy matching
- Prefix search
vector
- Path:
crates/vector/ - Purpose: Vector operations and types
- Vector type definitions
- Distance metrics (cosine, euclidean, dot product)
- Vector validation
- Integration with Qdrant segment library
Function execution crates
isolate
- Path:
crates/isolate/ - Purpose: JavaScript isolate runtime
- V8 isolate management
- JavaScript to Rust bridge
- Syscall implementation
- Module loading and resolution
- Resource limits and timeouts
- Built-in API implementations
function_runner
- Path:
crates/function_runner/ - Purpose: Function execution orchestration
- Function scheduling and queuing
- Compilation caching
- Execution context setup
- Transaction coordination
- Result collection
udf
- Path:
crates/udf/ - Purpose: UDF types and utilities
- Function types (query, mutation, action, http)
- Function arguments and return types
- Validation and conversion
- Path resolution
node_executor
- Path:
crates/node_executor/ - Purpose: Node.js execution for actions
- Process management
- IPC communication
- Streaming responses
- Error handling
Application layer crates
application
- Path:
crates/application/ - Purpose: High-level application logic
- Deployment management
- Configuration handling
- Schema validation and inference
- Export/import operations
- Push deployments
- Component isolation
authentication
- Path:
crates/authentication/ - Purpose: Authentication and authorization
- JWT token validation
- OAuth integration
- Admin key verification
- Identity management
- Session handling
sync
- Path:
crates/sync/ - Purpose: Client synchronization protocol
- WebSocket connection management
- Query subscription tracking
- Change notification
- State synchronization
Persistence backend crates
sqlite
- Path:
crates/sqlite/ - Purpose: SQLite storage backend
- Transaction support
- Efficient querying
- Local file storage
- Default for self-hosted deployments
postgres
- Path:
crates/postgres/ - Purpose: PostgreSQL storage backend
- Connection pooling
- Async operations
- Scalable storage
mysql
- Path:
crates/mysql/ - Purpose: MySQL storage backend
- Compatible with MySQL and MariaDB
- Connection management
- Async query execution
Utility crates
pb and pb_build
- Paths:
crates/pb/,crates/pb_build/ - Purpose: Protocol Buffers support
- Protobuf definitions
- Code generation
- Wire format compatibility
keybroker
- Path:
crates/keybroker/ - Purpose: Encryption key management
- Key derivation
- Key rotation
- Secure storage
file_storage
- Path:
crates/file_storage/ - Purpose: File upload and storage
- File metadata
- Content-addressed storage
- Upload/download operations
- Integration with S3
events
- Path:
crates/events/ - Purpose: Event tracking and analytics
- System events
- Usage metrics
- Audit logs
usage_tracking
- Path:
crates/usage_tracking/ - Purpose: Resource usage monitoring
- Bandwidth usage
- Storage consumption
- Function execution time
- Database operations
Dependency architecture
Workspace dependencies
TheCargo.toml at the root defines all dependencies with version pinning:
Key external dependencies
- tokio: Async runtime foundation
- axum: HTTP server framework
- deno_core: V8 JavaScript runtime
- tantivy: Full-text search engine
- serde/serde_json: Serialization
- anyhow: Error handling
- tracing: Structured logging
Dependency graph highlights
Build system
Workspace setup
The workspace uses Cargo’s workspace feature:Build profiles
- dev: Fast compilation, debug symbols
- release: Full optimization, panic=abort
- slim-release: Stripped debug info for smaller binaries
Testing
Each crate has its own tests:testing feature flag enables test utilities:
Development workflow
Building and running:Key architectural patterns
Trait-based abstraction
Core abstractions use traits:Runtime: Async runtime operationsPersistence: Storage backend operationsTransaction: Database transaction interface
Async/await throughout
All I/O operations are async:- Database queries
- HTTP requests
- File operations
- Function execution
Strong typing
Rust’s type system enforces:- Compile-time correctness
- Memory safety
- Thread safety
- Error handling
Testing culture
Extensive testing:- Unit tests in each module
- Integration tests in
tests/ - Property-based testing with proptest
- Benchmark tests for performance
Performance considerations
Async I/O
Tokio provides:- Efficient task scheduling
- Non-blocking I/O
- Work-stealing scheduler
Memory management
- Zero-copy operations where possible
- Careful use of cloning
- Reference counting for shared data
- Bump allocation for transactions
Concurrency
- Lock-free data structures where possible
- Minimal lock contention
- Async operations avoid blocking
- Isolated execution contexts