Architecture patterns and practices

1,626
-1

Published on

ADC2013-阿里技术嘉年华上的分享

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,626
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
64
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Architecture patterns and practices

  1. 1. trait Speaker { val name:String val aliases:Set[String] } trait Organization{ val bu = "tmall" val group = "alibaba" } object WangFuqiang extends Speaker with Organization{ val name = "王福强" val aliases = Set("千任", "@囚千任") val blog = “http://afoo.me” } 架构模式与实践漫谈 Sunday, July 14, 13
  2. 2. Architecture Sunday, July 14, 13
  3. 3. Architecture Principles • Abstraction • Modularity • Scalability • Robustness • Security • Availability • Reusability • name it more... Sunday, July 14, 13
  4. 4. Exciting? Sunday, July 14, 13
  5. 5. Don’t Be Silly! Sunday, July 14, 13
  6. 6. Overload Sunday, July 14, 13
  7. 7. How About Practices + Patterns Sunday, July 14, 13
  8. 8. Roadmap First Sunday, July 14, 13
  9. 9. Consistency Layering Isolation Immutability Clustering Buffering Async Throttling 单结点 多结点 Sunday, July 14, 13
  10. 10. Sunday, July 14, 13
  11. 11. So Far, So Good Sunday, July 14, 13
  12. 12. Consistency Layering Caching Isolation Immutability Clustering Async Throttling Sunday, July 14, 13
  13. 13. What we do? Sunday, July 14, 13
  14. 14. Sure, 1. Scale Up!纵向扩展 Sunday, July 14, 13
  15. 15. “I try so hard, I got so far...” ‘cause hardware can’t work well without proper software Sunday, July 14, 13
  16. 16. Sunday, July 14, 13
  17. 17. Sunday, July 14, 13
  18. 18. Buffering缓冲 Sunday, July 14, 13
  19. 19. Queues fucking simple idea, right? Sunday, July 14, 13
  20. 20. Queue Everywhere • cpu task run queue • thread pool task queue • actors’ mailbox • TCP stack send buffer /receive buffer • MOM • etc. Sunday, July 14, 13
  21. 21. Batching分批处理 when they slowly move Sunday, July 14, 13
  22. 22. Sunday, July 14, 13
  23. 23. When to batch When to flush Sunday, July 14, 13
  24. 24. Problem With Nagle Algorithm Sunday, July 14, 13
  25. 25. Latency VS. Throughput It’s your choice! Sunday, July 14, 13
  26. 26. Smart Batching focus on the whole pipeline Sunday, July 14, 13
  27. 27. http://mechanical-sympathy.blogspot.com/2011/10/smart- batching.html • Lock-free • 100K+ TPS Sunday, July 14, 13
  28. 28. Disruptor Start 2 use it today Sunday, July 14, 13
  29. 29. --------割-------- Sunday, July 14, 13
  30. 30. Sunday, July 14, 13
  31. 31. Throttling 限流|flow control Sunday, July 14, 13
  32. 32. Rate Control Backpressure Sunday, July 14, 13
  33. 33. Rate Control Sunday, July 14, 13
  34. 34. Semaphore Sunday, July 14, 13
  35. 35. 僵硬 活 Sunday, July 14, 13
  36. 36. Let’s apply BackPressureButterfly Effect Sunday, July 14, 13
  37. 37. Sunday, July 14, 13
  38. 38. Sunday, July 14, 13
  39. 39. Sunday, July 14, 13
  40. 40. 表征 位置 (反 馈 )方 式 what where how Back Pressure load, rt, etc. ack, nack, etc.hardware, os, app, etc. Sunday, July 14, 13
  41. 41. --------割-------- Sunday, July 14, 13
  42. 42. 2 + 2 = ? Sunday, July 14, 13
  43. 43. Sunday, July 14, 13
  44. 44. Sunday, July 14, 13
  45. 45. Caching缓存 Sunday, July 14, 13
  46. 46. Why Caching? Sunday, July 14, 13
  47. 47. Sunday, July 14, 13
  48. 48. Cache Everywhere • CPU Level1-3 cache • Browser-side cache • Server-side cache • Business-layer cache • DAL-layer cache • Database-level cache • Reverse Proxy • DNS • Name it more... Sunday, July 14, 13
  49. 49. Cache Types • Local Cache • Map • Ehcache * (BigHeap) • Redis • Remote Cache • Tair * • Memcached • In-Memory Data Grid - IMDG • Coherence • GemFire * means Hybrid Sunday, July 14, 13
  50. 50. Cache Strategy Dimension Synchronous Asynchronous READ Read-Through Refresh-Ahead WRITE Write-Through Write-Behind Sunday, July 14, 13
  51. 51. Caching Tricks • Dummy Value • non-exist entities in storage • Versioning • long-period-cached files K1 K2 K3 K1 K2 K3 K4 K4 K5 K5 Cache Storage file1.v1 file1.v2 index.html <=> file1 Sunday, July 14, 13
  52. 52. Case Study - 支付宝双11预充值 我爬... 我也爬... 我跟着爬... 我等?! Sunday, July 14, 13
  53. 53. --------割-------- Sunday, July 14, 13
  54. 54. Async 步 Sunday, July 14, 13
  55. 55. give me the quote of EUR/USD here u are buy 2 lots transaction result Story Background... Sunday, July 14, 13
  56. 56. Future<Double> rateQuoteFuture = executor.submit(new Callable<Double>() { @Override public Double call() throws Exception { connection.getCurrentValue(USD); } }); try { Double rateQuote = rateQuoteFuture.get(5, TimeUnit.SECONDS); // Blocking wait Future<Integer> future = executor.submit(new Callable<Object>() { @Override public Integer call() throws Exception { if(isProfitable(rateQuote)) connection.buy(amount, rateQuote) else throw n Exception("not profitable"); } }); Integer amount = future.get(5, TimeUnit.SECONDS); // Blocking wait System.out.println("Purchased "+ amount + " USD"); } catch (InterruptedException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | Fi } catch (ExecutionException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | Fi } catch (TimeoutException e) { e.printStackTrace(); //To change body of catch statement use File | Settings | Fi } Go Async Sunday, July 14, 13
  57. 57. The Pain In The Neck blocking wait Sunday, July 14, 13
  58. 58. 1. val rateQuote = future { 2. connection.getCurrentValue(USD) 3. } 5. rateQuote onSuccess { case quote => 6. val purchase = future { 7. if (isProfitable(quote)) connection.buy(amount, quote) 8. else throw new Exception("not profitable") 9. } 10. 11. purchase onSuccess { 12. case _ => println("Purchased " + amount + " USD") 13. } 14.} Go Reactive Sunday, July 14, 13
  59. 59. Behind The Pretty Face blocking still exists Sunday, July 14, 13
  60. 60. Task Scheduling Matters Sunday, July 14, 13
  61. 61. Async & Task Schedule Granularity •Process •Thread •Actor •Coroutine •Continuation Sunday, July 14, 13
  62. 62. Go Non-Blocking Sunday, July 14, 13
  63. 63. And Think In Big Picture Sunday, July 14, 13
  64. 64. Sir, Yes, Sir. Sunday, July 14, 13
  65. 65. R All Async Good? Sunday, July 14, 13
  66. 66. “Trade A For A” Async Anti-Pattern Sunday, July 14, 13
  67. 67. A yelled ‘get it done’ B working... A looking/waiting/ smoking... Sunday, July 14, 13
  68. 68. Future<Double> rateQuoteFuture = executor.submit(new Callable<Double>() { @Override public Double call() throws Exception { connection.getCurrentValue(USD); } }); Double rateQuote = rateQuoteFuture.get(5, TimeUnit.SECONDS); // Nothing more to do, just wait and return Sunday, July 14, 13
  69. 69. Future<Double> rateQuoteFuture = executor.submit(new Callable<Double>() { @Override public Double call() throws Exception { connection.getCurrentValue(USD); } }); Double rateQuote = rateQuoteFuture.get(5, TimeUnit.SECONDS); // Nothing more to do, just wait and return Why don’t U do it in current thread? Sunday, July 14, 13
  70. 70. It’s All About RESOURCE UTILITY资源利用率是 键 Sunday, July 14, 13
  71. 71. SEDA A grand master of buffering + async Sunday, July 14, 13
  72. 72. Sunday, July 14, 13
  73. 73. --------割-------- Sunday, July 14, 13
  74. 74. Sunday, July 14, 13
  75. 75. Sunday, July 14, 13
  76. 76. Layering Isolation Immutability Clustering Buffering Async ThrottlingManage Dependencies Wisely明智地管理依赖 Sunday, July 14, 13
  77. 77. WhereWe Stand Service2 Service1 Service3 Sunday, July 14, 13
  78. 78. Exception Start Small, But Basic! Sunday, July 14, 13
  79. 79. What we do after catch? retry? ignore? throw up? Sunday, July 14, 13
  80. 80. Sunday, July 14, 13
  81. 81. Sunday, July 14, 13
  82. 82. Timeout Everywhere • Thread.join(long) • Future.get(long, TimeUnit) • ExecutorService.awaitTermination(long, TimeUnit) • CountDownLatch.await(long, TimeUnit) • RPC Framework, say, HSF or Dubbo • IO timeout • you name it... Sunday, July 14, 13
  83. 83. Circuit Breaker Pattern I want it to be automatic! Sunday, July 14, 13
  84. 84. Sunday, July 14, 13
  85. 85. Sunday, July 14, 13
  86. 86. SwitchesI don’t need you here! Sunday, July 14, 13
  87. 87. Sunday, July 14, 13
  88. 88. trait PartialFunction[-A, +B] extends (A => B) { self =>   import PartialFunction._   /** Checks if a value is contained in the function's domain. * * @param x the value to test * @return `'''true'''`, iff `x` is in the domain of this function, `'''false'''` otherwise. */   def isDefinedAt(x: A): Boolean ... } trait Function1[@specialized(..) -T1, @specialized(..) +R] extends AnyRef { self =>   /** Apply the body of this function to the argument. * @return the result of function application. */   def apply(v1: T1): R http://www.infoq.com/cn/articles/function-switch-realize-better-continuous-implementations Switch.reduce{ } Sunday, July 14, 13
  89. 89. Hard Enough Sunday, July 14, 13
  90. 90. 2. Scale Out横向扩展 Sunday, July 14, 13
  91. 91. We r gonna Cluster it. Sunday, July 14, 13
  92. 92. Symmetric Stateful State Asymmetric Stateless ComputationSunday, July 14, 13
  93. 93. Replicas 制品, 副本 Sunday, July 14, 13
  94. 94. Replica means Symmetric Sunday, July 14, 13
  95. 95. When It Goes Stateless Sunday, July 14, 13
  96. 96. Computational Replicas Sunday, July 14, 13
  97. 97. Master-Workers Pattern Sunday, July 14, 13
  98. 98. Sunday, July 14, 13
  99. 99. Sunday, July 14, 13
  100. 100. Most of the time, Computation and state are so close... Sunday, July 14, 13
  101. 101. State Replicas Sunday, July 14, 13
  102. 102. Read Scales Redundancy Sunday, July 14, 13
  103. 103. Replication Sunday, July 14, 13
  104. 104. Replication Types • Consistency Concerning Replication • Synchronous Replication • Asynchronous Replication • Bandwidth Concerning Replication • Intra-IDC Replication • Inter-IDC Replication • Other Views... Sunday, July 14, 13
  105. 105. Load-Balancing Sunday, July 14, 13
  106. 106. Sunday, July 14, 13
  107. 107. So short? Just Stay with me Sunday, July 14, 13
  108. 108. --------割-------- Sunday, July 14, 13
  109. 109. Shards分片,分区 aka. Partitioning Sunday, July 14, 13
  110. 110. Write Scales Storage Scales Sunday, July 14, 13
  111. 111. Routing Sunday, July 14, 13
  112. 112. Lookup Table Sunday, July 14, 13
  113. 113. Hashing Sunday, July 14, 13
  114. 114. Consistent Hashing Sunday, July 14, 13
  115. 115. Sharding Strategy Matters • Capacity Planning • Capacity Expanding • State Transferring • State Access Pattern • Fault-Tolerance • etc. Sunday, July 14, 13
  116. 116. L.B. Vs. Routing Sunday, July 14, 13
  117. 117. --------割-------- Sunday, July 14, 13
  118. 118. Shit Happens Sunday, July 14, 13
  119. 119. Sunday, July 14, 13
  120. 120. Isolation隔离性 Sunday, July 14, 13
  121. 121. Bulkhead Pattern Sunday, July 14, 13
  122. 122. Sunday, July 14, 13
  123. 123. One Tab, One Process Sunday, July 14, 13
  124. 124. Linux Container Sunday, July 14, 13
  125. 125. Single Box In A Cluster Sunday, July 14, 13
  126. 126. A Whole Cluster Goes Bad... Sunday, July 14, 13
  127. 127. So When you deploy clusters of • Search Service • HSF/Dubbo Service • Data Storage Service • Caching Service(Tair, Redis, memcached...) • Whatever that’s important or have higher priority, NO Share! Sunday, July 14, 13
  128. 128. What Do U Think? Sunday, July 14, 13
  129. 129. --------割-------- Sunday, July 14, 13
  130. 130. Sunday, July 14, 13
  131. 131. Redundancy冗余 Sunday, July 14, 13
  132. 132. Symmetric Cluster? Sunday, July 14, 13
  133. 133. Symmetric Cluster? What A Lucky Boy! Sunday, July 14, 13
  134. 134. What About Asymmetric Cluster? Sunday, July 14, 13
  135. 135. BinaryStar Pattern Sunday, July 14, 13
  136. 136. Sunday, July 14, 13
  137. 137. --------割-------- Sunday, July 14, 13
  138. 138. Way&Style Sunday, July 14, 13
  139. 139. Consistency一致性 Sunday, July 14, 13
  140. 140. Why Consistency? Sunday, July 14, 13
  141. 141. Sunday, July 14, 13
  142. 142. Sunday, July 14, 13
  143. 143. Consistency Everywhere • Religion • Bible • Brotherhood initiation • Psychology • Gambling • Debating • Industry 大众就一款⻋车高尔夫,拍窄点就是菠萝,加个屁股就是新桑塔纳和新捷达,继续拍一拍就是速腾和朗逸、再捏一把就是新宝来, 扩大一圈就是新帕萨特,再拍一拍就是迈腾,再加大一圈就是辉腾,拍成方的就是途安,加多三个后座就是夏朗,加高底盘就是 途观,再 大点就是途锐,拍 就是尚酷,搓圆了就是甲 虫。 Sunday, July 14, 13
  144. 144. Show Time... Sunday, July 14, 13
  145. 145. 统一构建原则 Sunday, July 14, 13
  146. 146. In Framework Design like Spring Sunday, July 14, 13
  147. 147. -- Haskell is a pure functional language and even the I/O system can't break this purity. Sunday, July 14, 13
  148. 148. Go Consistency When Necessary Sunday, July 14, 13
  149. 149. Layering分层 Sunday, July 14, 13
  150. 150. Why Layering? Sunday, July 14, 13
  151. 151. impact Sunday, July 14, 13
  152. 152. Layer Patterns Everywhere Can U Name More? Sunday, July 14, 13
  153. 153. Cake Pattern Sunday, July 14, 13
  154. 154. Stackable Traits Pattern Decorator Pattern In Scala Sunday, July 14, 13
  155. 155. --------割-------- Sunday, July 14, 13
  156. 156. Immutability不变性 Sunday, July 14, 13
  157. 157. Sunday, July 14, 13
  158. 158. Sunday, July 14, 13
  159. 159. Sunday, July 14, 13
  160. 160. Persistent Data Structure Sunday, July 14, 13
  161. 161. Familiar? Sunday, July 14, 13
  162. 162. Event Sourcing Sunday, July 14, 13
  163. 163. Capture all changes to an application state as a sequence of events. Sunday, July 14, 13
  164. 164. Real World ES • Binlog, HLog . . . • Kafka’s Storage Strategy • MetaQ - Java Clone of Kafka, • LMAX Platform • Many home-brew tools • Ultra Messaging Stream Edition(UMS) - Informatica • Available Projects/Solutions • https://github.com/eligosource/eventsourced • https://github.com/sbtourist/Journal.IO • https://github.com/fusesource/hawtjournal Sunday, July 14, 13
  165. 165. 案例分析 •TMS编辑和发布的并发控制与潜在冲突 •模板中变量名变更的追踪无案底可查 Sunday, July 14, 13
  166. 166. Lambda Pattern Sunday, July 14, 13
  167. 167. Does Lambda’s face inspire U? Sunday, July 14, 13
  168. 168. Sunday, July 14, 13
  169. 169. Sunday, July 14, 13
  170. 170. http://www.infoq.com/presentations/High-Performance-Network-Applications-in-the-Capital-Markets Todd-Montgomery Sunday, July 14, 13
  171. 171. Sunday, July 14, 13
  172. 172. Log VS. JMX Your Opinion? Sunday, July 14, 13
  173. 173. --------割-------- Sunday, July 14, 13
  174. 174. Composability Yes, This is a principle :0) 组合 Sunday, July 14, 13
  175. 175. 活运用工具的能力 比工具质量要重要的多 Sunday, July 14, 13
  176. 176. It’sNot The Ending, It’s The Beginning... Sunday, July 14, 13
  177. 177. 推荐书目 Sunday, July 14, 13
  178. 178. Have Fun & Happy Sunday, July 14, 13

×