← 返回刚才阅读的位置

知识卡片

普通读书笔记卡 · 2313.e.5.f.10.a.3.a.1.a.1.a

独立调用并发缩短关键路径

理解等级:高级

互不依赖的画像与历史查询可并发,存在依赖的相似商品计算仍须等待历史。总等待要由统一截止时间约束;原书逐次Future.get各给一段超时,并不保证整体只等同样时长,超时后工作也未必被取消。

这张卡片看不懂?

已有反馈 · 0 条

    还没有人提交反馈。

    在整书笔记中的位置

    原书出处 · 1 处

    • Building Resilient Systems · 查看原书依据

      Effective latency management requires the following: Concurrent processing of independent operations Prioritization of critical paths over optional features Caching strategies at multiple levels Dynamic timeout management based on system conditions Graceful degradation of non-essential features Following is how to handle latency properly: public List<Recommendation> getRecommendations(String userId) { // Use caching for relatively static data List<Product> trending = cache.get("trending", () -> analyticsService.getTrending(), Duration.ofMinutes(5)); // Parallel execution of necessary real-time calls CompletableFuture<UserProfile> profileFuture = CompletableFuture .supplyAsync(() -> userService.getProfile(userId)); CompletableFuture<List<Purchase>> historyFuture = CompletableFuture .supplyAsync(() -> orderService.getHistory(userId)); // Wait for all data with timeout try { UserProfile profile = profileFuture.get(200, TimeUnit.MILLISECONDS); List<Purchase> history = historyFuture.get(200, TimeUnit.MILLISECONDS); // Calculate recommendations asynchronously return CompletableFuture .supplyAsync(() -> calculateRecommendations(profile, history, trending)) .get(500, TimeUnit.MILLISECONDS); } catch (TimeoutException e) { return getFallbackRecommendations(userId); } } // Implement a circuit breaker for external service calls @CircuitBreaker(name = "similarityService", fallbackMethod = "getFallbackSimilar") private List<Product> getSimilarProducts(List<Purchase> history) { return similarityService.findSimilar(history); } private List<Product> getFallbackSimilar(List<Purchase> history, Exception e) { return cache.get("default_recommendations"); } Designing for limited and costly bandwidth While network bandwidth has increased dramatically since these fallacies were first articulated, so have the demands we place on our systems.

    参考来源

    - 位置:《Building Distributed Systems Designing scalable architectures with microservices, event-driven patterns, and robust deployment…》第08章《Building Resilient Systems》(来源项目book-sources;源文件:extracted/Building Distributed Systems Designing scalable architectures with microservices, event-driven patterns, and robust deployment…/_epub-src/OPS/ch08.xhtml) - 结论依据:原文给出本卡对应定义或示例;互不依赖的画像与历史查询可并发,存在依赖的相似商品计算仍须等待历史。 对原文省略和过度承诺的限定是本卡显式纠正,不能反过来当作原书已证明。 - 原始内容:Effective latency management requires the following: Concurrent processing of independent operations Prioritization of critical paths over optional features Caching strategies at multiple levels Dynamic timeout management based on system conditions Graceful degradation of non-essential features Following is how to handle latency properly: public List<Recommendation> getRecommendations(String userId) { // Use caching for relatively static data List<Product> trending = cache.get("trending", () -> analyticsService.getTrending(), Duration.ofMinutes(5)); // Parallel execution of necessary real-time calls CompletableFuture<UserProfile> profileFuture = CompletableFuture .supplyAsync(() -> userService.getProfile(userId)); CompletableFuture<List<Purchase>> historyFuture = CompletableFuture .supplyAsync(() -> orderService.getHistory(userId)); // Wait for all data with timeout try { UserProfile profile = profileFuture.get(200, TimeUnit.MILLISECONDS); List<Purchase> history = historyFuture.get(200, TimeUnit.MILLISECONDS); // Calculate recommendations asynchronously return CompletableFuture .supplyAsync(() -> calculateRecommendations(profile, history, trending)) .get(500, TimeUnit.MILLISECONDS); } catch (TimeoutException e) { return getFallbackRecommendations(userId); } } // Implement a circuit breaker for external service calls @CircuitBreaker(name = "similarityService", fallbackMethod = "getFallbackSimilar") private List<Product> getSimilarProducts(List<Purchase> history) { return similarityService.findSimilar(history); } private List<Product> getFallbackSimilar(List<Purchase> history, Exception e) { return cache.get("default_recommendations"); } Designing for limited and costly bandwidth While network bandwidth has increased dramatically since these fallacies were first articulated, so have the demands we place on our systems.