Skip to content

Commit d27dffd

Browse files
committed
feat: implement problem 6
1 parent e7889e2 commit d27dffd

File tree

8 files changed

+2800
-0
lines changed

8 files changed

+2800
-0
lines changed
Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
# System Architecture Diagram
2+
3+
## Text-Based Architecture Overview
4+
5+
```
6+
[Users]
7+
|
8+
[CDN Layer]
9+
|
10+
[API Gateway]
11+
| | |
12+
+--------------+ | +--------------+
13+
| | |
14+
[Availability Zone 1] [Availability Zone 2] [Availability Zone 3]
15+
| | |
16+
+-------+-------+ +-------+-------+ +-------+-------+
17+
| | | | | | | | |
18+
[Score] [User] [Notif] [Score] [User] [Notif] [Score] [User] [Notif]
19+
Service Service Service Service Service Service Service Service Service
20+
| | | | | | | | |
21+
+-------+-------+--+-------+-------+--+-------+-------+
22+
| | |
23+
[Event Bus - Kafka Cluster]
24+
| | |
25+
[Redis Cache Cluster]
26+
| | |
27+
+-------+-------+--+-------+-------+--+-------+-------+
28+
| | | | | | | | |
29+
[Primary] [DB] [DB] [Read] [DB] [DB] [Read] [DB] [DB]
30+
Database Shard1 Shard2 Replica Shard3 Shard4 Replica Shard5 Shard6
31+
```
32+
33+
## Component Layers
34+
35+
### 1. Client Layer
36+
- **Users**: Web browsers, mobile apps
37+
- **Connections**: HTTPS, WebSocket
38+
39+
### 2. Edge Layer
40+
- **CDN**: Static asset caching, global distribution
41+
- **Caching**: 5-minute TTL for API responses
42+
43+
### 3. Gateway Layer
44+
- **API Gateway**: Single entry point, load balancing
45+
- **Features**: Rate limiting, authentication, routing
46+
- **Security**: JWT validation, DDoS protection
47+
48+
### 4. Service Layer (Microservices)
49+
50+
#### Score Service
51+
```
52+
Responsibilities:
53+
├── Score validation and updates
54+
├── Leaderboard computation
55+
├── Business rule enforcement
56+
└── Event publishing
57+
58+
Endpoints:
59+
├── POST /api/v1/scores
60+
├── GET /api/v1/leaderboard
61+
├── GET /api/v1/users/{id}/score
62+
└── GET /api/v1/health
63+
```
64+
65+
#### User Service
66+
```
67+
Responsibilities:
68+
├── User authentication
69+
├── Profile management
70+
├── Permission validation
71+
└── Session management
72+
73+
Endpoints:
74+
├── POST /api/v1/auth/login
75+
├── POST /api/v1/auth/refresh
76+
├── GET /api/v1/users/{id}
77+
└── PUT /api/v1/users/{id}
78+
```
79+
80+
#### Notification Service
81+
```
82+
Responsibilities:
83+
├── WebSocket connection management
84+
├── Real-time message broadcasting
85+
├── Event consumption from Kafka
86+
└── Connection pooling
87+
88+
Features:
89+
├── Auto-reconnection
90+
├── Message queuing
91+
├── Graceful degradation
92+
└── Load balancing
93+
```
94+
95+
### 5. Event Layer
96+
- **Apache Kafka**: Event streaming backbone
97+
- **Topics**: score_updates, user_actions, system_events
98+
- **Partitioning**: By user_id for ordered processing
99+
- **Replication**: 3 replicas across AZs
100+
101+
### 6. Cache Layer
102+
- **Redis Cluster**: In-memory data store
103+
- **Use Cases**: Session storage, leaderboard caching, rate limiting
104+
- **Configuration**: 6 nodes (3 primary, 3 replica)
105+
- **Eviction**: LRU policy with 1GB memory per node
106+
107+
### 7. Data Layer
108+
109+
#### Database Sharding Strategy
110+
```
111+
Shard Distribution:
112+
├── Shard 1: Users with hash(user_id) % 4 = 0
113+
├── Shard 2: Users with hash(user_id) % 4 = 1
114+
├── Shard 3: Users with hash(user_id) % 4 = 2
115+
└── Shard 4: Users with hash(user_id) % 4 = 3
116+
117+
Replication:
118+
├── Primary DB (AZ1): Write operations
119+
├── Read Replica (AZ2): Read operations, failover
120+
└── Read Replica (AZ3): Read operations, analytics
121+
```
122+
123+
#### Database Schema
124+
```sql
125+
-- Users table (sharded by user_id)
126+
CREATE TABLE users (
127+
id UUID PRIMARY KEY,
128+
username VARCHAR(50) UNIQUE NOT NULL,
129+
email VARCHAR(100) UNIQUE NOT NULL,
130+
created_at TIMESTAMP DEFAULT NOW(),
131+
updated_at TIMESTAMP DEFAULT NOW()
132+
);
133+
134+
-- Scores table (sharded by user_id)
135+
CREATE TABLE scores (
136+
user_id UUID PRIMARY KEY REFERENCES users(id),
137+
current_score INTEGER NOT NULL DEFAULT 0,
138+
last_updated TIMESTAMP DEFAULT NOW(),
139+
version INTEGER NOT NULL DEFAULT 1,
140+
INDEX idx_score_ranking (current_score DESC)
141+
);
142+
143+
-- Score history (for audit and analytics)
144+
CREATE TABLE score_history (
145+
id UUID PRIMARY KEY,
146+
user_id UUID NOT NULL,
147+
score_change INTEGER NOT NULL,
148+
action_type VARCHAR(50) NOT NULL,
149+
timestamp TIMESTAMP DEFAULT NOW(),
150+
session_id VARCHAR(100),
151+
ip_address INET
152+
);
153+
```
154+
155+
### 8. Monitoring Layer
156+
```
157+
Observability Stack:
158+
├── Metrics: Prometheus + Grafana
159+
├── Logging: ELK Stack (Elasticsearch, Logstash, Kibana)
160+
├── Tracing: Jaeger for distributed tracing
161+
└── Alerting: PagerDuty integration
162+
163+
Key Metrics:
164+
├── Service health and uptime
165+
├── Response times (p50, p95, p99)
166+
├── Error rates and types
167+
├── Active user connections
168+
├── Score update frequency
169+
└── Database performance
170+
```
171+
172+
## Data Flow Diagrams
173+
174+
### Score Update Flow
175+
```
176+
[User Action]
177+
↓ HTTPS POST
178+
[API Gateway]
179+
↓ Load Balance
180+
[Score Service]
181+
↓ Validate
182+
[Database Shard]
183+
↓ Success
184+
[Event Bus]
185+
↓ Publish
186+
[Notification Service]
187+
↓ WebSocket
188+
[Connected Clients]
189+
```
190+
191+
### Real-time Update Flow
192+
```
193+
[Score Change Event]
194+
↓ Kafka Topic
195+
[Notification Service]
196+
↓ Process Event
197+
[WebSocket Connections]
198+
↓ Broadcast
199+
[Frontend Apps]
200+
↓ Update UI
201+
[Leaderboard Display]
202+
```
203+
204+
### Authentication Flow
205+
```
206+
[Login Request]
207+
↓ Credentials
208+
[User Service]
209+
↓ Validate
210+
[JWT Generation]
211+
↓ Return Tokens
212+
[Client Storage]
213+
↓ Include in Headers
214+
[API Gateway]
215+
↓ Verify JWT
216+
[Service Access]
217+
```
218+
219+
## Security Architecture
220+
221+
### Defense in Depth
222+
```
223+
Layer 1: CDN/WAF Protection
224+
├── DDoS mitigation
225+
├── IP filtering
226+
├── Rate limiting
227+
└── SSL/TLS termination
228+
229+
Layer 2: API Gateway Security
230+
├── JWT token validation
231+
├── Request/response filtering
232+
├── Rate limiting per user
233+
└── Request size limits
234+
235+
Layer 3: Service-Level Security
236+
├── Input validation
237+
├── Business rule enforcement
238+
├── Audit logging
239+
└── Encrypted data storage
240+
241+
Layer 4: Database Security
242+
├── Connection encryption
243+
├── Row-level security
244+
├── Backup encryption
245+
└── Access control lists
246+
```
247+
248+
### Threat Mitigation
249+
```
250+
Score Tampering Prevention:
251+
├── JWT-based authentication
252+
├── Request signing with HMAC
253+
├── Idempotency keys
254+
├── Rate limiting (10 updates/minute)
255+
├── Business rule validation
256+
├── Anomaly detection
257+
└── Comprehensive audit trails
258+
259+
Session Security:
260+
├── Secure HTTP-only cookies
261+
├── CSRF protection
262+
├── Session rotation
263+
├── Concurrent session limits
264+
└── Device fingerprinting
265+
```
266+
267+
This architecture provides a comprehensive, scalable, and secure foundation for the scoreboard system while maintaining clear separation of concerns and enabling future enhancements.

0 commit comments

Comments
 (0)