Chỉ số | Giá trị |
---|---|
Throughput tối đa | 50,000 yêu cầu/giây |
Thời gian phản hồi trung bình | 350ms |
95th percentile response | 850ms |
Sử dụng CPU lúc cao điểm | 85-95% |
Bộ nhớ heap dùng | 75% |
Kết nối DB tối đa | Đạt tới giới hạn pool 100 |
Thread pool | Bị quá tải thường xuyên |
@Servicepublic class ProductService { @Autowired private ReactiveProductRepository repository; public Mono<Product> getProductById(Long id) { return repository.findById(id) .switchIfEmpty(Mono.error(new ProductNotFoundException(id))); }}
@RestController@RequestMapping("/api/products")public class ProductController { @Autowired private ProductService service;
@GetMapping("/{id}") public Mono<ResponseEntity<Product>> getProduct(@PathVariable Long id) { return service.getProductById(id) .map(ResponseEntity::ok) .defaultIfEmpty(ResponseEntity.notFound().build()); }}
@Query("SELECT o FROM Order o WHERE o.userId = :userId AND o.status = :status AND o.createdDate BETWEEN :start AND :end ORDER BY o.createdDate DESC")List<Order> findUserOrdersInDateRange( @Param("userId") Long userId, @Param("status") OrderStatus status, @Param("start") LocalDate start, @Param("end") LocalDate end);
@OneToMany(mappedBy = "order", fetch = FetchType.EAGER)@BatchSize(size = 30)private Set<OrderItem> items;
spring: datasource: hikari: maximum-pool-size: 30 minimum-idle: 10 idle-timeout: 30000 connection-timeout: 2000 max-lifetime: 1800000
@EnableCaching@Configurationpublic class CacheConfig { @Bean public RedisCacheManager cacheManager(RedisConnectionFactory connectionFactory) { RedisCacheConfiguration cacheConfig = RedisCacheConfiguration.defaultCacheConfig() .entryTtl(Duration.ofMinutes(10)) .disableCachingNullValues();
return RedisCacheManager.builder(connectionFactory) .cacheDefaults(cacheConfig) .withCacheConfiguration("products", RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofMinutes(5))) .withCacheConfiguration("categories", RedisCacheConfiguration.defaultCacheConfig().entryTtl(Duration.ofHours(1))) .build(); }}
@Cacheable(value = "products", key = "#id")public Mono<Product> getProductById(Long id){ return repository.findById(id) .switchIfEmpty(Mono.error(new ProductNotFoundException(id)));}
@CacheEvict(value = "products", key = "#product.id")public Mono<Product> updateProduct(Product product){ return repository.save(product);}
@Beanpublic ObjectMapper objectMapper() { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new AfterburnerModule()); mapper.setSerializationInclusion(Include.NON_NULL); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); return mapper;}
spring: reactor: netty: worker: count: 16 connection: provider: pool: max-connections: 10000 acquire-timeout: 5000
server: tomcat: threads: max: 200 min-spare: 20 max-connections: 8192 accept-count: 100 connection-timeout: 2000
FROM openjdk:17-slimCOPY target/myapp.jar app.jarENV JAVA_OPTS="-XX:+UseG1GC -XX:MaxGCPauseMillis=100 -XX:+ParallelRefProcEnabled"ENTRYPOINT exec java $JAVA_OPTS -jar /app.jar
apiVersion: autoscaling/v2kind: HorizontalPodAutoscalermetadata: name: myapp-hpaspec: scaleTargetRef: apiVersion: apps/v1 kind: Deployment name: myapp minReplicas: 5 maxReplicas: 20 metrics: - type: Resource resource: name: cpu target: type: Utilization averageUtilization: 70
apiVersion: networking.istio.io/v1alpha3kind: VirtualServicemetadata: name: myapp-vsspec: hosts: - myapp-service http: - route: - destination: host: myapp-service retries: attempts: 3 perTryTimeout: 2s timeout: 5s
Chỉ số | Ban đầu | Sau tối ưu |
---|---|---|
Throughput tối đa | 50,000 req/s | 1,200,000 req/s |
Thời gian phản hồi trung bình | 350ms | 85ms |
95th percentile | 850ms | 120ms |
CPU peak sử dụng | 85-95% | 60-70% |
Memory heap | 75% | 50% |
Query database | Bình thường | Giảm 70% (caching) |
Hiệu quả threading | Bình thường | Tăng 10 lần (reactive programming) |