|
| 1 | +# Readonly ClickHouse for API Endpoints |
| 2 | + |
| 3 | +This document explains how to configure and use a readonly ClickHouse instance for user-facing API endpoints while keeping the main orchestration flow unchanged. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The readonly ClickHouse feature allows you to: |
| 8 | +- Use a separate, read-only ClickHouse instance for API queries |
| 9 | +- Keep the main ClickHouse instance for orchestration (indexing, committing, etc.) |
| 10 | +- Scale read and write operations independently |
| 11 | +- Improve API performance by using dedicated read replicas |
| 12 | + |
| 13 | +## Configuration |
| 14 | + |
| 15 | +### Environment Variables |
| 16 | + |
| 17 | +Set the following environment variables to enable readonly ClickHouse: |
| 18 | + |
| 19 | +```bash |
| 20 | +export CLICKHOUSE_HOST_READONLY="readonly-clickhouse.example.com" |
| 21 | +export CLICKHOUSE_PORT_READONLY=9000 |
| 22 | +export CLICKHOUSE_USER_READONLY="readonly_user" |
| 23 | +export CLICKHOUSE_PASSWORD_READONLY="readonly_password" |
| 24 | +export CLICKHOUSE_DATABASE_READONLY="insight_readonly" |
| 25 | +``` |
| 26 | + |
| 27 | +### Configuration File |
| 28 | + |
| 29 | +You can also set these values in your configuration file: |
| 30 | + |
| 31 | +```yaml |
| 32 | +storage: |
| 33 | + main: |
| 34 | + clickhouse: |
| 35 | + # Main ClickHouse configuration (for orchestration) |
| 36 | + host: "localhost" |
| 37 | + port: 9000 |
| 38 | + username: "default" |
| 39 | + password: "password" |
| 40 | + database: "insight" |
| 41 | + |
| 42 | + # Readonly ClickHouse configuration (for API endpoints) |
| 43 | + readonlyHost: "readonly-clickhouse.example.com" |
| 44 | + readonlyPort: 9000 |
| 45 | + readonlyUsername: "readonly_user" |
| 46 | + readonlyPassword: "readonly_password" |
| 47 | + readonlyDatabase: "insight_readonly" |
| 48 | +``` |
| 49 | +
|
| 50 | +## How It Works |
| 51 | +
|
| 52 | +### Automatic Detection |
| 53 | +
|
| 54 | +The system automatically detects if readonly configuration is available: |
| 55 | +
|
| 56 | +1. **With readonly config**: API endpoints use the readonly ClickHouse instance |
| 57 | +2. **Without readonly config**: API endpoints fall back to the main ClickHouse instance |
| 58 | +
|
| 59 | +### Affected Endpoints |
| 60 | +
|
| 61 | +The following API endpoints will use the readonly ClickHouse instance when configured: |
| 62 | +
|
| 63 | +- `GET /{chainId}/blocks` - Block queries |
| 64 | +- `GET /{chainId}/transactions` - Transaction queries |
| 65 | +- `GET /{chainId}/events` - Event/log queries |
| 66 | +- `GET /{chainId}/transfers` - Token transfer queries |
| 67 | +- `GET /{chainId}/balances/{owner}` - Token balance queries |
| 68 | +- `GET /{chainId}/holders/{address}` - Token holder queries |
| 69 | +- `GET /{chainId}/search/{input}` - Search queries |
| 70 | + |
| 71 | +### Unaffected Operations |
| 72 | + |
| 73 | +The following operations continue to use the main ClickHouse instance: |
| 74 | + |
| 75 | +- Block indexing and polling |
| 76 | +- Transaction processing |
| 77 | +- Event/log processing |
| 78 | +- Staging data operations |
| 79 | +- Orchestration flow (committer, failure recovery, etc.) |
| 80 | + |
| 81 | +## Implementation Details |
| 82 | + |
| 83 | +### Readonly Connector |
| 84 | + |
| 85 | +The `ClickHouseReadonlyConnector` implements the same interface as the main connector but: |
| 86 | + |
| 87 | +- Only allows read operations |
| 88 | +- Panics on write operations (ensuring readonly behavior) |
| 89 | +- Uses readonly connection parameters |
| 90 | +- Falls back to main config if readonly config is incomplete |
| 91 | + |
| 92 | +### Storage Factory |
| 93 | + |
| 94 | +The `NewReadonlyConnector` function creates readonly connectors: |
| 95 | + |
| 96 | +```go |
| 97 | +// For API endpoints (readonly if configured) |
| 98 | +storage.NewReadonlyConnector[storage.IMainStorage](&config.Cfg.Storage.Main) |
| 99 | +
|
| 100 | +// For orchestration (always main connector) |
| 101 | +storage.NewConnector[storage.IMainStorage](&config.Cfg.Storage.Main) |
| 102 | +``` |
| 103 | + |
| 104 | +## Setup Instructions |
| 105 | + |
| 106 | +### 1. Create Readonly ClickHouse Instance |
| 107 | + |
| 108 | +Set up a ClickHouse replica or read-only instance: |
| 109 | + |
| 110 | +```sql |
| 111 | +-- On your readonly ClickHouse instance |
| 112 | +CREATE DATABASE insight_readonly; |
| 113 | +-- Grant readonly permissions to readonly_user |
| 114 | +GRANT SELECT ON insight_readonly.* TO readonly_user; |
| 115 | +``` |
| 116 | + |
| 117 | +### 2. Set Environment Variables |
| 118 | + |
| 119 | +```bash |
| 120 | +export CLICKHOUSE_HOST_READONLY="your-readonly-host" |
| 121 | +export CLICKHOUSE_PORT_READONLY=9000 |
| 122 | +export CLICKHOUSE_USER_READONLY="readonly_user" |
| 123 | +export CLICKHOUSE_PASSWORD_READONLY="readonly_password" |
| 124 | +export CLICKHOUSE_DATABASE_READONLY="insight_readonly" |
| 125 | +``` |
| 126 | + |
| 127 | +### 3. Restart API Server |
| 128 | + |
| 129 | +Restart your API server to pick up the new configuration: |
| 130 | + |
| 131 | +```bash |
| 132 | +./insight api |
| 133 | +``` |
| 134 | + |
| 135 | +### 4. Verify Configuration |
| 136 | + |
| 137 | +Check the logs to confirm which connector is being used: |
| 138 | + |
| 139 | +``` |
| 140 | +INFO Using readonly ClickHouse connector for API endpoints |
| 141 | +``` |
| 142 | + |
| 143 | +## Monitoring and Troubleshooting |
| 144 | + |
| 145 | +### Log Messages |
| 146 | + |
| 147 | +- **"Using readonly ClickHouse connector for API endpoints"** - Readonly mode active |
| 148 | +- **"Using regular ClickHouse connector for API endpoints"** - Fallback to main connector |
| 149 | + |
| 150 | +### Common Issues |
| 151 | + |
| 152 | +1. **Connection refused**: Check readonly ClickHouse host/port |
| 153 | +2. **Authentication failed**: Verify readonly username/password |
| 154 | +3. **Database not found**: Ensure readonly database exists |
| 155 | +4. **Permission denied**: Grant SELECT permissions to readonly user |
| 156 | + |
| 157 | +### Testing |
| 158 | + |
| 159 | +Test the readonly connection: |
| 160 | + |
| 161 | +```bash |
| 162 | +# Test readonly connection |
| 163 | +clickhouse-client --host=readonly-host --port=9000 --user=readonly_user --password=readonly_password --database=insight_readonly -q "SELECT 1" |
| 164 | +``` |
| 165 | + |
| 166 | +## Performance Considerations |
| 167 | + |
| 168 | +### Read Replicas |
| 169 | + |
| 170 | +- Use ClickHouse read replicas for better performance |
| 171 | +- Consider geographic distribution for global users |
| 172 | +- Monitor replica lag to ensure data consistency |
| 173 | + |
| 174 | +### Connection Pooling |
| 175 | + |
| 176 | +The readonly connector uses the same connection pool settings as the main connector: |
| 177 | + |
| 178 | +```yaml |
| 179 | +maxOpenConns: 100 |
| 180 | +maxIdleConns: 10 |
| 181 | +``` |
| 182 | + |
| 183 | +### Query Optimization |
| 184 | + |
| 185 | +- Readonly instances can be optimized for query performance |
| 186 | +- Consider different ClickHouse settings for readonly vs. write instances |
| 187 | +- Use materialized views on readonly instances for complex queries |
| 188 | + |
| 189 | +## Security |
| 190 | + |
| 191 | +### Network Security |
| 192 | + |
| 193 | +- Restrict readonly ClickHouse to internal networks |
| 194 | +- Use VPN or private subnets for readonly access |
| 195 | +- Consider ClickHouse's built-in network security features |
| 196 | + |
| 197 | +### User Permissions |
| 198 | + |
| 199 | +- Create dedicated readonly user with minimal permissions |
| 200 | +- Grant only SELECT permissions on required tables |
| 201 | +- Regularly rotate readonly user passwords |
| 202 | + |
| 203 | +### Data Access |
| 204 | + |
| 205 | +- Readonly users cannot modify data |
| 206 | +- No risk of accidental data corruption |
| 207 | +- Audit logs show readonly access patterns |
| 208 | + |
| 209 | +## Migration |
| 210 | + |
| 211 | +### From Single Instance |
| 212 | + |
| 213 | +1. Set up readonly ClickHouse instance |
| 214 | +2. Configure environment variables |
| 215 | +3. Restart API server |
| 216 | +4. Monitor performance and errors |
| 217 | +5. Gradually migrate more endpoints if needed |
| 218 | + |
| 219 | +### Rollback |
| 220 | + |
| 221 | +To rollback to main ClickHouse: |
| 222 | + |
| 223 | +1. Remove readonly environment variables |
| 224 | +2. Restart API server |
| 225 | +3. API endpoints will automatically use main connector |
| 226 | + |
| 227 | +## Support |
| 228 | + |
| 229 | +For issues or questions about the readonly ClickHouse feature: |
| 230 | + |
| 231 | +1. Check the logs for error messages |
| 232 | +2. Verify ClickHouse connectivity |
| 233 | +3. Review configuration parameters |
| 234 | +4. Check ClickHouse server logs |
| 235 | +5. Contact the development team |
0 commit comments