Sigmagit Architecture β
System architecture and design decisions for Sigmagit.
ποΈ High-Level Architecture β
Monorepo Structure β
sigmagitv2/
βββ apps/
β βββ web/ # TanStack Start web application
β βββ mobile/ # Expo React Native mobile app
β βββ api/ # Hono API server (Bun)
β βββ discord-bot/ # Discord integration bot
βββ packages/
β βββ db/ # Drizzle ORM schema
β βββ lib/ # Shared utilities
β βββ hooks/ # React Query hooks
βββ docs/ # DocumentationTechnology Stack β
- Runtime: Bun 1.3.5+
- Language: TypeScript
- Frontend: React 19, TanStack Start, TailwindCSS
- Backend: Hono, Bun
- Mobile: React Native, Expo
- Database: PostgreSQL, Drizzle ORM
- Git: isomorphic-git
- Storage: S3-compatible or local filesystem
- Auth: better-auth
- Email: Resend or Nodemailer (SMTP)
- Caching: Redis (optional)
- Real-time: WebSocket + Redis Pub/Sub (optional)
π Data Flow β
Request Flow β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Browser / Mobile App β
ββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββ
β β β β
βΌ β β β
βΌ β β β
ββββββββ΄βββββββ β β
β βββββββ β β β
β βHono β β β β β
β ββββββββ β β β β
β βDrizzle β β β β
β β β β β β
β βββββββββ΄ββ΄ββ΄ββ΄β΄β
β β β β β
β β β β β
β β β β β
β β β β β
β β β β β
β β β β β β
β β β β β β
β β β β β β
β β β β β β
β β β β β β
β β β β β
β β β β β β β
βββββββββββββββββ΄ββ΄ββ΄ββ΄ββ΄β΄ββ
ββββββββββββββββββββββ΄ββ΄ββ΄ββ΄βοΏ½ββ΄ββ΄βοΏ½ββ΄β΄ββ΄βοΏ½β΄β
β β β β β
β β β β β β
β β β β β β β
β β β β β β β
β β β β β β
β β β β β β β
ββββββββββββββββ΄ββ΄ββ΄βοΏ½ββ΄ββ΄ββ΄β΄β΄β΄β΄β
β β β β β β β β
β β β β β β β
β β β β β β β βUser Authentication Flow β
Login
- User enters credentials in web/mobile app
- Credentials sent to
/auth/sign-in - better-auth authenticates
- Session created and stored in database
- Session cookie returned
Protected Route Access
- User makes request to protected route
- Middleware validates session cookie
- Auth context available to handlers
Session Validation
- Middleware checks session in database
- User object loaded and attached to context
ποΈ Database Schema β
Tables β
sql
users # User accounts
sessions # User sessions
repositories # Git repositories
stars # Repository stars
forks # Repository forks
issues # Issues
pull_requests # Pull requests
pr_labels # PR labels
pr_assignees # PR assignees
pr_reviewers # PR reviews
pr_comments # PR comments
issue_labels # Issue labels
issue_assignees # Issue assignees
issue_comments # Issue comments
issue_reactions # Issue reactions
notifications # User notifications
repo_branch_metadata # Branch metadata cache
discord_links # Discord account links
link_tokens # Discord link verification tokensRelationships β
- Repositories β Users (owner, forkers)
- Issues β Repositories, Users (author, assignees)
- Pull Requests β Repositories (baseRepo, headRepo)
- Comments β Issues, Pull Requests
- Stars β Repositories, Users
- Reactions β Issues, Pull Requests, Comments
π Caching Strategy β
Redis Caching (Optional) β
Cache Keys β
sigmagit:session:{userId}
sigmagit:gitObject:{type}:{oid}
sigmagit:refs:{userId}:{repo}:{branch}
sigmagit:branches:{userId}:{repo}
sigmagit:tree:{userId}:{repo}:{branch}:{path}
sigmagit:file:{userId}:{repo}:{branch}:{path}Cache TTL β
- Sessions: 1 hour
- Git objects: 24 hours
- Refs: 5 minutes
- Branches: 5 minutes
- Trees: 30 minutes
- Files: 1 hour
- Commits: 10 minutes
Cache Invalidation β
- Automatic invalidation on repository updates
- Manual invalidation on git push
- Cache pattern deletion for repository deletion
π― Git Storage Architecture β
Storage Backends β
S3 Storage (Recommended)
- Scalable object storage
- Used for production deployments
- Automatic multipart upload for large files
Local Storage (Development)
- Filesystem-based storage
- No external dependencies
- Used for development and self-hosting
Git Object Storage β
Structure:
repos/{owner}/{repo}/objects/{prefix}/{suffix}/{object}
repos/{owner}/{repo}/refs/{ref-name}
repos/{owner}/{repo}/{ref}/heads/{branch-name}
repos/{owner}/{repo}/HEADStorage Abstraction β
Interface: StorageBackend with methods:
get(key)- Retrieve object as Bufferput(key, body, contentType?)- Store objectdelete(key)- Delete objectexists(key)- Check if object existslist(prefix)- List objects with prefixdeletePrefix(prefix)- Delete all objects with prefixcopyPrefix(source, target)- Copy objects between prefixesgetStream(key)- Get object as ReadableStream
Implementations** β
S3StorageBackend- AWS S3 SDKLocalStorageBackend- Bun filesystem
Memory-Safe Operations β
- Stream large files instead of loading into memory
- Batch operations with size limits
- Proper error handling and retries
- Resource cleanup on errors
π‘ Real-Time Updates β
WebSocket Architecture β
Server-Sent Events β
- Issue Events: New issue, close, reopen, edit
- Pull Request Events: New PR, close, merge
- Push Events: New commits
- Star Events: Star/unstar repository
- Fork Events: Repository forked
WebSocket Flow β
ββββββββββββββββββ
β Client (Web/Mobile) β
βββββββ¬βββββββββββββββββ
β β
β ββββ΄βββββββ β
β β WebSocket β β
β ββββ΄βββ΄βββ΄ββ β
β β
β βββββββββ΄βββββββ΄β β
β β β β β β
β β β β β β β
β β β β β β β β
ββββ΄β΄ββ΄βββ΄βββ΄βββ΄ββ΄ββββ΄ββServer-Sent Events β
- Push Notifications: Send to WebSocket clients
- Webhook Notifications: Send to Discord webhooks
Redis Pub/Sub (Optional) β
Channels:
sigmagit:user:{userId}:notifications
sigmagit:repo:{owner}:{repo}:notifications
sigmagit:discord:{discordId}:notificationsMessage Format β
json
{
"type": "issue" | "pull_request" | "push" | "star" | "fork",
"repository": {
"owner": "string",
"name": "string"
},
"data": { ... }
}π Security β
Authentication β
- better-auth session-based authentication
- Cookie-based session storage
- Secure session token encryption
- Rate limiting on auth endpoints
Authorization β
Repository access control (public/private)
Branch protection rules (TBD)
Fork access control
Data Protection β
SQL injection prevention via Drizzle ORM
Input validation on all endpoints
XSS protection in web app
Secret management
Environment variables for sensitive data
Secure webhook signatures
API Security β
- CORS configuration
- Request size limits
- Rate limiting per endpoint
- Git push size limits (100MB)
- Maximum object count (50,000)
π Performance Optimizations β
Memory Management β
- Git operations in batches
- Streaming large files
- Periodic garbage collection
- Request size limits
- Memory threshold monitoring (80% heap)
- Connection pooling
Caching β
- Redis caching with TTL
- HTTP response caching headers
- Database query optimization with indexes
Database β
- Connection pooling
- Optimized queries with proper indexes
- Batch operations for efficiency
- Prepared statements for repeated queries
Git Operations β
- Optimized git operations
- Object caching with Redis
- Parallel processing where possible
- Delta resolution with depth limits (max 100)
Storage Operations β
- Batch S3 operations
- Multipart uploads for large files
- Connection reuse with proper cleanup
WebSocket β
- Connection cleanup for stale connections
- 30-minute timeout
- Periodic cleanup job
π§ Scalability β
Horizontal Scaling β
- Stateless API server design
- Shared storage (S3 or database)
- Shared cache (Redis)
- Stateless web/mobile apps
Vertical Scaling β
- Database connection pooling
- API server horizontal scaling
- Git operations can be distributed across instances
Load Balancing β
- Round-robin for API requests
- Session affinity for websocket connections
- Sticky sessions for web/mobile
π Monitoring & Observability β
Application Metrics β
Health Checks β
/healthendpoint - Server health status- Database connection health
- Storage backend health
- Redis connection health
Logging β
- Structured logging with timestamps
- Request/response logging
- Error logging with context
- Performance metrics logging
Metrics Collection β
- API request count and latency
- Database query performance
- Git operation duration
- Storage operation duration
- WebSocket connection metrics
- Error rate by type
Distributed Tracing (Future) β
- OpenTelemetry or similar
- Request ID propagation
- Distributed log aggregation
- Performance dashboard integration
π Deployment Architecture β
Production Setup β
API Server β
bash
# Environment variables required
- DATABASE_URL, BETTER_AUTH_SECRET
# Optional: REDIS_URL, STORAGE_TYPE, EMAIL_PROVIDER, DISCORD_WEBHOOK_URL
# Deployment options
- Bun runtime optimized for production
# Auto-scaling (cloud provider)
- Load balancer configurationWeb App β
bash
# Static asset serving
# Environment-specific configuration
- CDN integration (optional)
# Rate limitingMobile App β
bash
# App Store deployment
```bash
expo submit:ios
expo submit:androidDatabase β
bash
# Connection pool sizing
# Read replica configuration
- Write replica configurationStorage β
bash
# S3 configuration
# Multi-region replication (optional)
# Lifecycle policiesMonitoring β
bash
# Application Performance Monitoring (APM)
# Database query performance monitoring
# Storage operation monitoring
# Error rate and alertingπ API Gateway (Future) β
Considerations β
- Rate limiting per endpoint
- Request routing based on API path
- Request transformation
- API versioning strategy
- Circuit breaker pattern
π Service Communication β
Microservices Communication β
Currently: Monolithic API server with clear boundaries
Future: Separate services β
- API server split by domain
- Event-driven communication
- Message queue (Redis Streams)
Service Discovery β
- Service registry (Consul/etcd)
π§ Configuration Management β
Environment-Based Configuration β
All configuration via environment variables
bash
# Required
DATABASE_URL=
BETTER_AUTH_SECRET=
# Optional
STORAGE_TYPE=
S3_ENDPOINT=
REDIS_URL=
EMAIL_PROVIDER=
DISCORD_WEBHOOK_URL=Configuration Loading β
- Type-safe configuration with validation
- Fallback to defaults
- Runtime validation
Secrets Management β
- Environment variables for sensitive data
- No hardcoded secrets in code
- GitHub Secrets or equivalent for deployment
π State Management β
API Server β
- Stateless where possible
- Session storage via database
Web App β
- Server state via TanStack Store
- Local persistence via TanStack Store
Mobile App β
- Local state via Zustand
- Persistence via AsyncStorage
Discord Bot β
- No persistent state required
- Each request is independent
Cache Invalidation β
- Automatic invalidation on data changes
- Tag-based cache invalidation
- Time-based expiration (TTL)
π Error Handling β
Global Error Handler β
- Catch-all error handler in API server
- Structured error responses
- Error logging with context
Application Error Boundaries β
- React error boundaries (web)
- Global error handler (mobile)
- Error reporting to monitoring service
Error Classification β
- Database errors (connection, query, constraint)
- Storage errors (not found, access denied)
- Git errors (invalid ref, merge conflict)
- API errors (validation, authentication)
Error Recovery β
- Automatic retry with exponential backoff
- Circuit breaker pattern for external services
- Fallback to alternative storage backend if S3 unavailable
- Graceful degradation on high load
π Load Balancing Strategy β
API Server β
Session affinity β
- WebSocket connection affinity
- Database read replica routing
- Consistent hashing for storage access
Web App β
- Session-based routing
- Static asset CDN routing
Mobile App β
- No specific load balancing needed
Discord Bot β
- No load balancing needed (single instance)
π Data Consistency β
Transaction Management β
- Database transactions for multi-step operations
- Isolation levels for data consistency
Git Operations β
- Atomic ref updates
- Conflict detection and resolution
- Lock-free operations where possible
Cache Invalidation β
- Immediate invalidation on writes
- Tag-based invalidation strategy
Eventual Consistency β
- WebSocket notifications may be delayed
- Webhooks may arrive out of order
- Background sync processes for consistency
π οΈ Backup & Recovery β
Database Backups β
- Scheduled dumps (pg_dump)
- Point-in-time recovery (WAL)
- Archive old data (partitioning)
Git Backups β
- Repository snapshots
- Full backup before destructive operations
- Incremental backups for efficiency
Storage Backups β
- S3 versioning (object versioning)
- Replication to backup region
- Backup retention policy
Disaster Recovery β
- Database restore from backup
- S3 restore from backups
- Git repository recovery from backup
Recovery Testing β
- Regular disaster recovery drills
- Failover testing
- Data integrity verification
π¦ Development Workflow β
Setup β
- Clone repository
- Install dependencies:
bun install - Configure environment:
cp .env.example .env - Run database migrations:
bun run db:push - Start API server:
bun run dev:api - Start other services as needed
Code Review β
- PR reviews for code quality
- TypeScript strict mode enforcement
- Security review for sensitive changes
Testing Strategy β
- Unit tests for business logic
- Integration tests for API endpoints
- E2E tests for critical paths
- Load testing for performance
- End-to-end tests for user flows
Git Workflow β
- Create feature branch from
main - Make changes in feature branch
- Submit PR for review
- Request code review
- Update based on feedback
- Merge to
mainafter approval
Deployment Workflow β
- Create PR from
developtomain - Ensure tests pass and linting succeeds
- Review and approve in PR
- Merge to
main - Deploy to staging environment
- Smoke test in staging
- Deploy to production
Built with Bun, Hono, React, PostgreSQL, Redis