The Kalpi Tech API is a high-performance stock technical analysis API built using FastAPI, designed to serve technical indicators with tiered access control and enterprise-level scalability.
- Clear separation of concerns
- Layered architecture (presentation, business logic, data access)
- Dependency injection for testability
- Polars for high-performance data processing
- Redis caching for frequently accessed data
- Asynchronous processing where applicable
- Horizontal scaling capability
- Stateless design
- Efficient database queries
- JWT-based authentication
- API key support
- Rate limiting per tier
- Input validation and sanitization
graph TB
Client[Client Applications]
LB[Load Balancer]
API[FastAPI Application]
Auth[Authentication Service]
Cache[Redis Cache]
DB[(PostgreSQL)]
Data[(Stock Data)]
Client --> LB
LB --> API
API --> Auth
API --> Cache
API --> DB
API --> Data
subgraph "API Layer"
API --> Routers[API Routers]
Routers --> Services[Business Services]
Services --> Models[Data Models]
end
subgraph "Services Layer"
Services --> DataSvc[Data Service]
Services --> CacheSvc[Cache Service]
Services --> RateSvc[Rate Limit Service]
end
- Responsibility: Application lifecycle, middleware, error handling
- Key Features:
- Lifespan management for startup/shutdown
- Global exception handling
- CORS configuration
- Health check endpoint
- Indicators Router: Technical indicator endpoints
- Auth Router: Authentication and user management
- Validation: Pydantic models for request/response validation
- Error Handling: HTTP status codes and structured error responses
- Responsibility: Stock data management and filtering
- Key Features:
- Polars-based data loading from Parquet files
- Symbol and date range filtering
- Tier-based data access validation
- Memory-efficient data operations
- Responsibility: Redis-based caching
- Key Features:
- Intelligent cache key generation
- TTL-based expiration
- Cache invalidation strategies
- Fallback handling for cache failures
- Responsibility: API rate limiting per user tier
- Key Features:
- Daily request counting
- Tier-based limits
- Automatic cleanup of old entries
- Real-time limit checking
- User: Authentication and profile data
- APIKey: API key management
- RequestLog: Request auditing
- Relationships: Foreign key constraints
- Connection Management: SQLAlchemy engine and session management
- Transaction Handling: Proper session lifecycle
- Error Handling: Database-specific error handling
- JWT Token Management: Creation, validation, expiration
- Password Hashing: bcrypt-based secure hashing
- API Key Management: Generation and validation
- Dependency Injection: FastAPI dependencies for route protection
sequenceDiagram
participant C as Client
participant API as FastAPI
participant Auth as Auth Service
participant Rate as Rate Limiter
participant Cache as Redis Cache
participant Data as Data Service
participant Calc as Indicators
C->>API: Request with Auth
API->>Auth: Validate Token/API Key
Auth-->>API: User Info
API->>Rate: Check Rate Limit
Rate-->>API: Rate Limit Status
API->>Cache: Check Cache
Cache-->>API: Cache Miss
API->>Data: Get Stock Data
Data-->>API: Filtered Data
API->>Calc: Calculate Indicator
Calc-->>API: Results
API->>Cache: Store Results
API-->>C: JSON Response
- Approach: Load entire dataset into memory at startup
- Rationale:
- Faster query performance
- Reduced I/O operations
- Predictable latency
- Trade-offs: Higher memory usage vs. query performance
-
On-demand loading: Load data per request
- Pros: Lower memory usage
- Cons: Higher latency, more I/O
-
Chunked loading: Load data in chunks
- Pros: Balanced memory usage
- Cons: Complex cache management
-
Database storage: Store data in PostgreSQL
- Pros: ACID compliance, complex queries
- Cons: Higher latency, more complex setup
- No server-side session storage
- JWT tokens for authentication
- Redis for shared state
- Multiple API instances behind load balancer
- Session affinity not required
- Health checks for instance management
- Separate read/write operations
- Read replicas for heavy read operations
- Master-slave configuration
- SQLAlchemy connection pooling
- Connection reuse optimization
- Proper connection lifecycle management
- Multiple Redis instances
- Data sharding across nodes
- High availability with sentinel
- Cache-aside pattern
- Write-through caching for critical data
- Cache invalidation strategies
Internet -> Load Balancer -> API Gateway -> FastAPI App
↓
Authentication Layer
↓
Authorization Layer
↓
Business Logic
- JWT with short expiration times
- Secure secret key management
- Token refresh mechanism
- Automatic validation
- Type checking
- Custom validators
- Sanitization
- SQLAlchemy ORM usage
- Parameterized queries
- Input sanitization
- API gateway level
- Application level
- Database level
- Rate limiting per IP
- Progressive delays
- Temporary blocking
- Lazy evaluation
- Columnar storage
- Vectorized operations
- Memory efficiency
# Polars - More efficient
df.filter(
(pl.col("symbol") == "AAPL") &
(pl.col("date") >= start_date)
).select(["date", "close"])
# Pandas - Less efficient
df[(df["symbol"] == "AAPL") &
(df["date"] >= start_date)][["date", "close"]]- Hierarchical structure:
indicator:symbol:params - Deterministic key generation
- Cache invalidation support
- Short TTL for volatile data (30 minutes)
- Longer TTL for historical data (24 hours)
- Configurable per indicator type
- Primary keys on all tables
- Composite indexes for common queries
- Partial indexes for filtered queries
- Eager loading for related data
- Pagination for large result sets
- Query result caching
- JSON format logs
- Correlation IDs
- Request/response logging
- Error stack traces
- DEBUG: Development debugging
- INFO: General information
- WARNING: Potential issues
- ERROR: Error conditions
- CRITICAL: System failures
- Request rate and latency
- Cache hit/miss ratios
- Database query performance
- Error rates by endpoint
- Memory and CPU usage
- Database connectivity
- Cache availability
- Data loading status
- External service health
- Service downtime
- High error rates
- Database connection failures
- Cache service unavailable
- High response times
- Low cache hit rates
- Memory usage thresholds
- Rate limit violations
- Consistent environments
- Easy deployment
- Resource isolation
- Scalability
- Development stage with debug tools
- Production stage optimized for size
- Security scanning integration
- Service definitions
- Volume management
- Network configuration
- Environment variables
- Pod management
- Service discovery
- Load balancing
- Auto-scaling
- Code checkout
- Dependency installation
- Unit tests
- Integration tests
- Docker image build
- Security scanning
- Deployment
- Blue-green deployment
- Rolling updates
- Canary releases
- Rollback capabilities
- Split monolith into services
- Service mesh implementation
- Inter-service communication
- Distributed tracing
- Message queues for async processing
- Event sourcing for audit trails
- CQRS pattern implementation
- Real-time data updates
- Custom indicator support
- Technical analysis patterns
- Machine learning indicators
- Backtesting capabilities
- WebSocket support
- Live data streaming
- Real-time notifications
- Market data integration
- Tenant isolation
- Resource quotas
- Custom configurations
- White-label solutions
- Usage analytics
- Performance monitoring
- Business intelligence
- Predictive analytics
The Kalpi Tech API architecture provides a solid foundation for a scalable, secure, and high-performance technical analysis platform. The modular design allows for easy maintenance and extension, while the performance optimizations ensure fast response times even under heavy load.
The architecture supports the current requirements while providing a path for future enhancements and scaling needs. The combination of modern technologies, best practices, and thoughtful design decisions creates a robust platform for stock technical analysis services.