A distributed temporal database allowing queries of any historical state with millisecond precision using CRDTs for multi-master replication and Raft consensus for strong consistency.
- Bitemporal Data Model: Query data as it was known at any point in time (valid time) and when it was recorded (transaction time)
- CRDT-based Replication: Conflict-free multi-master replication for high availability
- Raft Consensus: Strong consistency guarantees across distributed nodes
- Millisecond Precision: Query historical states with millisecond-level accuracy
- RESTful API: Simple HTTP API for all operations
- CLI Client: Command-line tool for easy interaction
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Chrono-DB Cluster β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β ββββββββββββ ββββββββββββ ββββββββββββ β
β β Node 1 βββββββΊβ Node 2 βββββββΊβ Node 3 β β
β β Leader β β Follower β β Follower β β
β ββββββ¬ββββββ ββββββ¬ββββββ ββββββ¬ββββββ β
β β β β β
β ββββββΌββββββββββββββββββΌβββββββββββββββββββΌβββββ β
β β Raft Consensus Layer β β
β β (Leader Election & Log Replication) β β
β βββββββββββββββββββββ¬ββββββββββββββββββββββββββββ β
β β β
β βββββββββββββββββββββΌββββββββββββββββββββββββββββ β
β β CRDT Store β β
β β (GCounter, LWW-Register) β β
β βββββββββββββββββββββ¬ββββββββββββββββββββββββββββ β
β β β
β βββββββββββββββββββββΌββββββββββββββββββββββββββββ β
β β Bitemporal Engine β β
β β (Valid Time & Transaction Time) β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ
βββββββββββββββββ
β REST API β
β (Port 8080) β
βββββββββββββββββ
- Go 1.19 or higher
- Git
git clone https://github.com/SnakeEye-sudo/Chrono-DB.git
cd Chrono-DBgo mod init github.com/SnakeEye-sudo/Chrono-DB
go mod tidygo build -o chrono-db main.go db_engine.go crdt.go raft.go api_server.gogo build -o chrono-client client.go./chrono-db -node=node1 -http=8080 -raft=9000 -data=./datacurl http://localhost:8080/Expected response:
{
"service": "Chrono-DB",
"version": "1.0.0",
"description": "Distributed temporal database with CRDT and Raft consensus"
}Endpoint: POST /api/v1/insert
curl -X POST http://localhost:8080/api/v1/insert \
-H "Content-Type: application/json" \
-d '{
"key": "user:1001",
"value": {
"name": "Alice Johnson",
"email": "alice@example.com",
"balance": 5000
},
"valid_start": "2024-01-01T00:00:00Z",
"valid_end": "9999-12-31T23:59:59Z"
}'Response:
{
"status": "success",
"key": "user:1001"
}Endpoint: GET /api/v1/query?key={key}
curl "http://localhost:8080/api/v1/query?key=user:1001"Response:
{
"found": true,
"key": "user:1001",
"value": {
"name": "Alice Johnson",
"email": "alice@example.com",
"balance": 5000
}
}Endpoint: GET /api/v1/temporal?key={key}&as_of={timestamp}&valid_time={timestamp}
curl "http://localhost:8080/api/v1/temporal?key=product:SKU-001&valid_time=2024-03-15T00:00:00Z"Response:
{
"found": true,
"key": "product:SKU-001",
"value": {
"name": "Laptop Pro 15",
"price": 1299.99
},
"as_of": "2024-10-23T14:30:00Z",
"valid_time": "2024-03-15T00:00:00Z"
}Endpoint: GET /api/v1/history?key={key}
curl "http://localhost:8080/api/v1/history?key=product:SKU-001"Response:
{
"key": "product:SKU-001",
"history": [
{
"key": "product:SKU-001",
"value": {"price": 1299.99},
"valid_time_start": "2024-02-01T00:00:00Z",
"valid_time_end": "2024-06-30T23:59:59Z",
"transaction_time": "2024-02-01T10:30:00Z"
},
{
"key": "product:SKU-001",
"value": {"price": 1199.99},
"valid_time_start": "2024-07-01T00:00:00Z",
"valid_time_end": "9999-12-31T23:59:59Z",
"transaction_time": "2024-07-01T08:00:00Z"
}
]
}Endpoint: GET /api/v1/status
curl http://localhost:8080/api/v1/statusResponse:
{
"node_id": "node1",
"raft_state": "leader",
"raft_term": 1,
"timestamp": "2024-10-23T14:30:00Z"
}Increment Counter:
curl -X POST "http://localhost:8080/api/v1/crdt/counter?key=page_views"Get Counter Value:
curl "http://localhost:8080/api/v1/crdt/counter?key=page_views"./chrono-client insert user:1002 '{"name":"Bob Smith","email":"bob@example.com"}'./chrono-client query user:1002./chrono-client history product:SKU-001./chrono-client status./chrono-client -url=http://localhost:8081 query user:1001./chrono-db -node=node1 -http=8080 -raft=9000 -data=./data/node1./chrono-db -node=node2 -http=8081 -raft=9001 -data=./data/node2 -join=localhost:9000./chrono-db -node=node3 -http=8082 -raft=9002 -data=./data/node3 -join=localhost:9000Each node will:
- Sync with the leader
- Participate in consensus
- Replicate data via CRDT
Load test data from testdata.json:
# Insert test records
curl -X POST http://localhost:8080/api/v1/insert \
-H "Content-Type: application/json" \
-d @testdata.jsonRun API tests:
# Test 1: Query current user data
curl "http://localhost:8080/api/v1/query?key=user:1001"
# Test 2: Query historical product price
curl "http://localhost:8080/api/v1/temporal?key=product:SKU-001&valid_time=2024-03-15T00:00:00Z"
# Test 3: Get full history
curl "http://localhost:8080/api/v1/history?key=product:SKU-001"- Track account balances over time
- Audit trail for all transactions
- Regulatory compliance with historical queries
- Price history tracking
- Inventory snapshots at any point in time
- Order status evolution
- Track configuration changes
- Rollback to any previous state
- Audit who changed what and when
- Patient history tracking
- Treatment timeline
- Compliance with data retention policies
See example_config.json for cluster configuration options:
- Node settings: ID, ports, data directory
- Raft parameters: Election timeout, heartbeat interval
- Database options: History retention, compaction
- API limits: Timeouts, request size limits
- Valid Time: When the fact was true in reality
- Transaction Time: When the fact was recorded in the database
This allows queries like:
- "What did we know about X on date Y?"
- "What was the actual value of X on date Y?"
- "Show me all changes to X"
- GCounter: Grow-only counter for distributed counting
- LWW-Register: Last-Writer-Wins register for conflict resolution
- Leader election with randomized timeouts
- Log replication with commit index tracking
- Automatic failover on leader failure
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License - feel free to use this project for learning and development.
- Inspired by bitemporal database concepts
- CRDT research from various academic papers
- Raft consensus algorithm by Diego Ongaro and John Ousterhout
Built with β€οΈ for distributed systems enthusiasts