18 - Database Structure

Overview

OpenAlgo uses 5 separate databases for data isolation, performance optimization, and specialized use cases. This separation prevents contention and allows each database to be optimized for its specific workload.

Architecture Diagram

┌──────────────────────────────────────────────────────────────────────────────┐
│                         Database Architecture                                 │
└──────────────────────────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────────────────────────┐
│                           5 Separate Databases                               │
│                                                                              │
│  ┌─────────────────┐  ┌─────────────────┐  ┌─────────────────┐             │
│  │   openalgo.db   │  │    logs.db      │  │   latency.db    │             │
│  │   (Main DB)     │  │   (Traffic)     │  │  (Performance)  │             │
│  │                 │  │                 │  │                 │             │
│  │  - Users        │  │  - traffic_logs │  │  - order_latency│             │
│  │  - Auth tokens  │  │  - ip_bans      │  │                 │             │
│  │  - API keys     │  │  - error_404    │  │  Metrics:       │             │
│  │  - Settings     │  │  - api_tracker  │  │  - RTT          │             │
│  │  - Orders       │  │                 │  │  - Overhead     │             │
│  │  - Strategies   │  │                 │  │  - Percentiles  │             │
│  └─────────────────┘  └─────────────────┘  └─────────────────┘             │
│                                                                              │
│  ┌─────────────────┐  ┌─────────────────┐                                   │
│  │   sandbox.db    │  │ historify.duckdb│                                   │
│  │  (Paper Trade)  │  │ (Market Data)   │                                   │
│  │                 │  │                 │                                   │
│  │  - Virtual ₹1Cr │  │  - OHLCV data   │                                   │
│  │  - Positions    │  │  - Watchlists   │                                   │
│  │  - Holdings     │  │                 │                                   │
│  │  - Trades       │  │  DuckDB format  │                                   │
│  │  - Daily P&L    │  │  (columnar)     │                                   │
│  └─────────────────┘  └─────────────────┘                                   │
└─────────────────────────────────────────────────────────────────────────────┘

Database 1: Main Database (openalgo.db)

Location

Core Tables

users

auth

api_keys

settings

strategies

flow_workflows

pending_orders (Action Center)

Database 2: Logs Database (logs.db)

Location

Tables

traffic_logs

ip_bans

error_404_tracker

invalid_api_key_tracker

Database 3: Latency Database (latency.db)

Location

Table: order_latency

Metrics Tracked

Metric
Description

rtt_ms

Network round-trip to broker

validation_ms

Request validation time

response_ms

Response processing time

overhead_ms

Total OpenAlgo overhead

P50, P90, P95, P99

Latency percentiles

Database 4: Sandbox Database (sandbox.db)

Location

Purpose

Isolated paper trading with ₹1 Crore virtual capital.

Tables

sandbox_orders

sandbox_positions

sandbox_funds

Sandbox Configuration

Config Key
Default
Description

starting_capital

₹1,00,00,000

Initial capital

equity_mis_leverage

5x

Intraday equity leverage

futures_leverage

10x

F&O leverage

nse_bse_square_off

15:15

Auto square-off time

mcx_square_off

23:30

MCX square-off time

Database 5: Historical Data (historify.duckdb)

Location

Format

DuckDB (columnar, analytics-optimized)

Table: market_data

Connection Pooling

SQLite Configuration

PostgreSQL Configuration (Production)

Security Features

Encryption

Data Type
Method

Passwords

Argon2 + pepper

API keys

Argon2 hash + Fernet encrypt

Auth tokens

Fernet (AES-128 CBC)

SMTP passwords

Fernet

Caching Strategy

Cache
TTL
Purpose

Auth tokens

Session expiry

Fast auth lookup

Verified API keys

10 hours

Reduce hashing

Invalid API keys

5 minutes

Block brute force

Settings

1 hour

Config cache

Strategies

5-10 minutes

Webhook lookup

Database Relationships

Key Files Reference

File
Purpose

database/user_db.py

User table

database/auth_db.py

Auth and API keys

database/settings_db.py

Settings table

database/strategy_db.py

Strategies

database/flow_db.py

Flow workflows

database/action_center_db.py

Pending orders

database/traffic_db.py

Logs database

database/latency_db.py

Latency metrics

database/sandbox_db.py

Sandbox tables

database/historify_db.py

DuckDB historical

Last updated