Files
pos-system/docs/en/skills/performance-optimization.md
Ho Ngoc Hai 2640b351c3 Enhance documentation with detailed diagrams and structured flows
- Added request/response flow diagrams to api-design and api-gateway-advanced skills for better visualization of processes.
- Introduced configuration loading flow in configuration-management skill to clarify the configuration process.
- Included error propagation flow in error-handling-patterns skill to illustrate error handling across layers.
- Enhanced various skills with additional diagrams to improve understanding of complex concepts.

These updates aim to provide clearer guidance and improve the overall documentation experience for developers.
2026-01-01 23:22:54 +07:00

159 lines
4.8 KiB
Markdown

---
name: performance-optimization
description: Performance optimization patterns for GoodGo microservices including database query optimization, memory leak detection, profiling, connection pooling, and caching strategies.
---
# Performance Optimization Patterns
## When to Use This Skill
Use this skill when:
- Optimizing database queries
- Detecting and fixing memory leaks
- Profiling application performance
- Optimizing connection pooling
- Improving caching strategies
- Identifying N+1 query problems
## Performance Optimization Workflow
The performance optimization process follows a systematic approach to identify, analyze, and resolve performance bottlenecks.
```mermaid
flowchart TD
Start([Start Optimization]) --> Identify[Identify Performance Issue]
Identify --> Monitor[Monitor Metrics]
Monitor --> Profiling[Profile Application]
Profiling --> Analyze[Analyze Results]
Analyze --> IdentifyBottleneck{Identify Bottleneck}
IdentifyBottleneck -->|Database| OptimizeDB[Optimize Queries]
IdentifyBottleneck -->|Memory| OptimizeMem[Fix Memory Leaks]
IdentifyBottleneck -->|Network| OptimizeNet[Optimize Connections]
IdentifyBottleneck -->|Cache| OptimizeCache[Improve Caching]
OptimizeDB --> Implement[Implement Optimization]
OptimizeMem --> Implement
OptimizeNet --> Implement
OptimizeCache --> Implement
Implement --> Test[Test Changes]
Test --> Verify{Performance Improved?}
Verify -->|Yes| Monitor
Verify -->|No| Analyze
Monitor --> Threshold{Meets Targets?}
Threshold -->|Yes| Complete([Optimization Complete])
Threshold -->|No| Profiling
```
## Query Optimization Flow
Database query optimization is a critical aspect of performance. This flow shows how to systematically optimize queries.
```mermaid
flowchart TD
Start([Query Performance Issue]) --> Analyze[Analyze Query Performance]
Analyze --> CheckIndexes[Check Indexes]
CheckIndexes --> Explain[Run EXPLAIN ANALYZE]
Explain --> IdentifyIssues{Identify Issues}
IdentifyIssues -->|N+1 Queries| FixN1[Use Include/Join]
IdentifyIssues -->|Missing Index| AddIndex[Add Database Index]
IdentifyIssues -->|Slow Query| OptimizeQuery[Rewrite Query]
IdentifyIssues -->|Unbounded| AddPagination[Add Pagination]
FixN1 --> VerifyQuery[Verify Query Performance]
AddIndex --> VerifyQuery
OptimizeQuery --> VerifyQuery
AddPagination --> VerifyQuery
VerifyQuery --> TestQuery{Query Time < 50ms?}
TestQuery -->|Yes| Complete([Optimization Complete])
TestQuery -->|No| Analyze
style FixN1 fill:#e1f5e1
style AddIndex fill:#e1f5e1
style OptimizeQuery fill:#e1f5e1
style AddPagination fill:#e1f5e1
```
## Profiling Process
The profiling process helps identify performance bottlenecks through systematic data collection and analysis.
```mermaid
flowchart TD
Start([Start Profiling]) --> Setup[Setup Profiling Tools]
Setup --> ChooseTool{Choose Profiling Type}
ChooseTool -->|CPU| CPUProf[CPU Profiling]
ChooseTool -->|Memory| MemProf[Memory Profiling]
ChooseTool -->|Database| DBProf[Database Query Profiling]
CPUProf --> CollectCPU[Collect CPU Metrics]
MemProf --> CollectMem[Collect Memory Metrics]
DBProf --> CollectDB[Collect Query Metrics]
CollectCPU --> Analyze[Analyze Profiling Data]
CollectMem --> Analyze
CollectDB --> Analyze
Analyze --> IdentifyHotspots[Identify Hotspots]
IdentifyHotspots --> Prioritize[Prioritize Issues]
Prioritize --> Optimize[Optimize Critical Paths]
Optimize --> ReProfile[Re-run Profiling]
ReProfile --> Compare{Performance Improved?}
Compare -->|Yes| Complete([Profiling Complete])
Compare -->|No| IdentifyHotspots
style CPUProf fill:#e3f2fd
style MemProf fill:#e3f2fd
style DBProf fill:#e3f2fd
```
## Key Patterns
### Database Query Optimization
```typescript
// Avoid N+1 queries
// Bad: Multiple queries
for (const user of users) {
user.orders = await orderRepository.findByUserId(user.id);
}
// Good: Single query with join
const users = await userRepository.findAll({
include: { orders: true },
});
```
### Memory Profiling
```typescript
// Monitor memory usage
const profiler = new MemoryProfiler();
profiler.start(); // Monitor every minute
```
### Batch Operations
```typescript
// Batch database operations
await batchOperations.batchCreate(items, 100); // Process 100 at a time
```
## Best Practices
1. Use indexes, avoid N+1 queries
2. Monitor memory usage, detect leaks
3. Cache frequently accessed data
4. Configure connection pools appropriately
5. Profile regularly to identify bottlenecks
## Resources
- [Caching Patterns](./caching-patterns.md)
- [Observability & Monitoring](./observability-monitoring.md)
- Skill Source: `.cursor/skills/performance-optimization/SKILL.md`