SlideShare une entreprise Scribd logo
1  sur  48
www.dotnetconf.net
淺談高並發與多執行緒
排程處理
同時處理
多執行緒爭搶什麼資源?
快 慢
什麼是Disruptor ?
註:QCon是一个InfoQ主辦的全球性的技術盛會,每年在倫敦、東京、北京、紐約、聖保羅、上海、舊金山等地舉辦
https://github.com/LMAX-Exchange/disruptor
https://groups.google.com/forum/#!forum/lmax-disruptor
ABC Arbitrage
https://www.nuget.org/packages/Disruptor
https://github.com/disruptor-net/Disruptor-net
Disruptor的架構設計
Thread 2Thread 1 Save state Restore state
Context Switch
Time
https://mechanical-sympathy.blogspot.com/2011/07/false-sharing.html
0
2,000
4,000
6,000
8,000
10,000
12,000
14,000
-
10,000,000
20,000,000
30,000,000
40,000,000
50,000,000
60,000,000
70,000,000
1 2 3 4 5 6 7
Performance
ConcurrentQueue Operations per second Disruptor.Net Operations per second
ConcurrentQueue Duration (ms) Disruptor.Net Duration (ms)
適合的應用場景與實務技巧分享
我的FB 我的Blog 我的GitHub
Disruptor.Net

Contenu connexe

Tendances

The Evolution of SpeedyCloud Resources Scheduling System
The Evolution of  SpeedyCloud Resources Scheduling SystemThe Evolution of  SpeedyCloud Resources Scheduling System
The Evolution of SpeedyCloud Resources Scheduling SystemSpeedyCloud
 
Redis -- Memory as the New Disk
Redis -- Memory as the New DiskRedis -- Memory as the New Disk
Redis -- Memory as the New DiskTim Lossen
 
Capodieci - Proof of... what?
Capodieci - Proof of... what?Capodieci - Proof of... what?
Capodieci - Proof of... what?Roberto Capodieci
 
SlideShare API ''Get Slideshow Information'' method example with Groovy
SlideShare API ''Get Slideshow Information'' method example with GroovySlideShare API ''Get Slideshow Information'' method example with Groovy
SlideShare API ''Get Slideshow Information'' method example with GroovyKeiji Yamashita
 
2017대선 빅데이터 분석
2017대선 빅데이터 분석2017대선 빅데이터 분석
2017대선 빅데이터 분석지승 한
 
Смарт-контракты: базовые инструменты для разработки и тестирования. Спикер: Д...
Смарт-контракты: базовые инструменты для разработки и тестирования. Спикер: Д...Смарт-контракты: базовые инструменты для разработки и тестирования. Спикер: Д...
Смарт-контракты: базовые инструменты для разработки и тестирования. Спикер: Д...Cyber Fund
 

Tendances (7)

The Evolution of SpeedyCloud Resources Scheduling System
The Evolution of  SpeedyCloud Resources Scheduling SystemThe Evolution of  SpeedyCloud Resources Scheduling System
The Evolution of SpeedyCloud Resources Scheduling System
 
Redis -- Memory as the New Disk
Redis -- Memory as the New DiskRedis -- Memory as the New Disk
Redis -- Memory as the New Disk
 
Capodieci - Proof of... what?
Capodieci - Proof of... what?Capodieci - Proof of... what?
Capodieci - Proof of... what?
 
SlideShare API ''Get Slideshow Information'' method example with Groovy
SlideShare API ''Get Slideshow Information'' method example with GroovySlideShare API ''Get Slideshow Information'' method example with Groovy
SlideShare API ''Get Slideshow Information'' method example with Groovy
 
2017대선 빅데이터 분석
2017대선 빅데이터 분석2017대선 빅데이터 분석
2017대선 빅데이터 분석
 
2017 03-29-elastic-meetup-kibana
2017 03-29-elastic-meetup-kibana2017 03-29-elastic-meetup-kibana
2017 03-29-elastic-meetup-kibana
 
Смарт-контракты: базовые инструменты для разработки и тестирования. Спикер: Д...
Смарт-контракты: базовые инструменты для разработки и тестирования. Спикер: Д...Смарт-контракты: базовые инструменты для разработки и тестирования. Спикер: Д...
Смарт-контракты: базовые инструменты для разработки и тестирования. Спикер: Д...
 

Similaire à Disruptor.Net

O'Reilly Software Architecture Conference London 2017: Building Resilient Mic...
O'Reilly Software Architecture Conference London 2017: Building Resilient Mic...O'Reilly Software Architecture Conference London 2017: Building Resilient Mic...
O'Reilly Software Architecture Conference London 2017: Building Resilient Mic...Ambassador Labs
 
Blockchain IoT Night / 25th Oct 2017
Blockchain IoT Night / 25th Oct 2017Blockchain IoT Night / 25th Oct 2017
Blockchain IoT Night / 25th Oct 2017Lothar Wieske
 
Learning to Scale OpenStack: Juno Update from the Rackspace Public Cloud
Learning to Scale OpenStack: Juno Update from the Rackspace Public CloudLearning to Scale OpenStack: Juno Update from the Rackspace Public Cloud
Learning to Scale OpenStack: Juno Update from the Rackspace Public CloudRainya Mosher
 
KubeCon USA 2017 brief Overview - from Kubernetes meetup Bangalore
KubeCon USA 2017 brief Overview - from Kubernetes meetup BangaloreKubeCon USA 2017 brief Overview - from Kubernetes meetup Bangalore
KubeCon USA 2017 brief Overview - from Kubernetes meetup BangaloreKrishna-Kumar
 
デベロッパーのためのAzureクラウドネイティブスタック 〜 提供したい価値からはじめる高速+高可用+高付加価値ソリューション
デベロッパーのためのAzureクラウドネイティブスタック 〜 提供したい価値からはじめる高速+高可用+高付加価値ソリューションデベロッパーのためのAzureクラウドネイティブスタック 〜 提供したい価値からはじめる高速+高可用+高付加価値ソリューション
デベロッパーのためのAzureクラウドネイティブスタック 〜 提供したい価値からはじめる高速+高可用+高付加価値ソリューションYoichi Kawasaki
 
Docker for Java Developers
Docker for Java DevelopersDocker for Java Developers
Docker for Java DevelopersImesh Gunaratne
 
Blockchain Talent 101 | Women in Blockchain (Oct 2, 2018)
Blockchain Talent 101 | Women in Blockchain (Oct 2, 2018)Blockchain Talent 101 | Women in Blockchain (Oct 2, 2018)
Blockchain Talent 101 | Women in Blockchain (Oct 2, 2018)AnchorTaiwan
 
Debugging Microservices - QCON 2017
Debugging Microservices - QCON 2017Debugging Microservices - QCON 2017
Debugging Microservices - QCON 2017Idit Levine
 
Chicago Docker Meetup Presentation - Mediafly
Chicago Docker Meetup Presentation - MediaflyChicago Docker Meetup Presentation - Mediafly
Chicago Docker Meetup Presentation - MediaflyMediafly
 
Introduction to CRI and OCI
Introduction to CRI and OCIIntroduction to CRI and OCI
Introduction to CRI and OCIHungWei Chiu
 
Akka and Kubernetes: Reactive From Code To Cloud
Akka and Kubernetes: Reactive From Code To CloudAkka and Kubernetes: Reactive From Code To Cloud
Akka and Kubernetes: Reactive From Code To CloudLightbend
 
IAU workshop 2018 day one
IAU workshop 2018 day oneIAU workshop 2018 day one
IAU workshop 2018 day oneWalid Shaari
 
Understanding blockchain
Understanding blockchainUnderstanding blockchain
Understanding blockchainKingsley Davies
 
Docker Devoxx UK - Never mind the bollocks here's the Linux Containers
Docker Devoxx UK - Never mind the bollocks here's the Linux ContainersDocker Devoxx UK - Never mind the bollocks here's the Linux Containers
Docker Devoxx UK - Never mind the bollocks here's the Linux ContainersPatrick Chanezon
 
Blockcerts: The Open Standard for Blockchain Credentials
Blockcerts: The Open Standard for Blockchain CredentialsBlockcerts: The Open Standard for Blockchain Credentials
Blockcerts: The Open Standard for Blockchain CredentialsSSIMeetup
 
Hong Kong Hyperledger Meetup January 2018
Hong Kong Hyperledger Meetup January 2018Hong Kong Hyperledger Meetup January 2018
Hong Kong Hyperledger Meetup January 2018Tracy Kuhrt
 

Similaire à Disruptor.Net (20)

O'Reilly Software Architecture Conference London 2017: Building Resilient Mic...
O'Reilly Software Architecture Conference London 2017: Building Resilient Mic...O'Reilly Software Architecture Conference London 2017: Building Resilient Mic...
O'Reilly Software Architecture Conference London 2017: Building Resilient Mic...
 
Blockchain IoT Night / 25th Oct 2017
Blockchain IoT Night / 25th Oct 2017Blockchain IoT Night / 25th Oct 2017
Blockchain IoT Night / 25th Oct 2017
 
Learning to Scale OpenStack: Juno Update from the Rackspace Public Cloud
Learning to Scale OpenStack: Juno Update from the Rackspace Public CloudLearning to Scale OpenStack: Juno Update from the Rackspace Public Cloud
Learning to Scale OpenStack: Juno Update from the Rackspace Public Cloud
 
KubeCon USA 2017 brief Overview - from Kubernetes meetup Bangalore
KubeCon USA 2017 brief Overview - from Kubernetes meetup BangaloreKubeCon USA 2017 brief Overview - from Kubernetes meetup Bangalore
KubeCon USA 2017 brief Overview - from Kubernetes meetup Bangalore
 
デベロッパーのためのAzureクラウドネイティブスタック 〜 提供したい価値からはじめる高速+高可用+高付加価値ソリューション
デベロッパーのためのAzureクラウドネイティブスタック 〜 提供したい価値からはじめる高速+高可用+高付加価値ソリューションデベロッパーのためのAzureクラウドネイティブスタック 〜 提供したい価値からはじめる高速+高可用+高付加価値ソリューション
デベロッパーのためのAzureクラウドネイティブスタック 〜 提供したい価値からはじめる高速+高可用+高付加価値ソリューション
 
Blockchain Introduction
Blockchain IntroductionBlockchain Introduction
Blockchain Introduction
 
Docker for Java Developers
Docker for Java DevelopersDocker for Java Developers
Docker for Java Developers
 
Blockchain Talent 101 | Women in Blockchain (Oct 2, 2018)
Blockchain Talent 101 | Women in Blockchain (Oct 2, 2018)Blockchain Talent 101 | Women in Blockchain (Oct 2, 2018)
Blockchain Talent 101 | Women in Blockchain (Oct 2, 2018)
 
Debugging Microservices - QCON 2017
Debugging Microservices - QCON 2017Debugging Microservices - QCON 2017
Debugging Microservices - QCON 2017
 
Chicago Docker Meetup Presentation - Mediafly
Chicago Docker Meetup Presentation - MediaflyChicago Docker Meetup Presentation - Mediafly
Chicago Docker Meetup Presentation - Mediafly
 
Introduction to CRI and OCI
Introduction to CRI and OCIIntroduction to CRI and OCI
Introduction to CRI and OCI
 
Akka and Kubernetes: Reactive From Code To Cloud
Akka and Kubernetes: Reactive From Code To CloudAkka and Kubernetes: Reactive From Code To Cloud
Akka and Kubernetes: Reactive From Code To Cloud
 
IAU workshop 2018 day one
IAU workshop 2018 day oneIAU workshop 2018 day one
IAU workshop 2018 day one
 
Docker & Daily DevOps
Docker & Daily DevOpsDocker & Daily DevOps
Docker & Daily DevOps
 
Docker and-daily-devops
Docker and-daily-devopsDocker and-daily-devops
Docker and-daily-devops
 
Understanding blockchain
Understanding blockchainUnderstanding blockchain
Understanding blockchain
 
Docker Devoxx UK - Never mind the bollocks here's the Linux Containers
Docker Devoxx UK - Never mind the bollocks here's the Linux ContainersDocker Devoxx UK - Never mind the bollocks here's the Linux Containers
Docker Devoxx UK - Never mind the bollocks here's the Linux Containers
 
Blockcerts: The Open Standard for Blockchain Credentials
Blockcerts: The Open Standard for Blockchain CredentialsBlockcerts: The Open Standard for Blockchain Credentials
Blockcerts: The Open Standard for Blockchain Credentials
 
Hong Kong Hyperledger Meetup January 2018
Hong Kong Hyperledger Meetup January 2018Hong Kong Hyperledger Meetup January 2018
Hong Kong Hyperledger Meetup January 2018
 
Rolling upgrade OpenStack
Rolling upgrade OpenStackRolling upgrade OpenStack
Rolling upgrade OpenStack
 

Dernier

Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commercemanigoyal112
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Software Coding for software engineering
Software Coding for software engineeringSoftware Coding for software engineering
Software Coding for software engineeringssuserb3a23b
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Mater
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
cpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.pptcpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.pptrcbcrtm
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 

Dernier (20)

Cyber security and its impact on E commerce
Cyber security and its impact on E commerceCyber security and its impact on E commerce
Cyber security and its impact on E commerce
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Software Coding for software engineering
Software Coding for software engineeringSoftware Coding for software engineering
Software Coding for software engineering
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
Odoo Development Company in India | Devintelle Consulting Service
Odoo Development Company in India | Devintelle Consulting ServiceOdoo Development Company in India | Devintelle Consulting Service
Odoo Development Company in India | Devintelle Consulting Service
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)Ahmed Motair CV April 2024 (Senior SW Developer)
Ahmed Motair CV April 2024 (Senior SW Developer)
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
cpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.pptcpct NetworkING BASICS AND NETWORK TOOL.ppt
cpct NetworkING BASICS AND NETWORK TOOL.ppt
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 

Notes de l'éditeur

  1. 大家早安,我是Jed 講第一場壓力好大,特別是人還這麼多,希望接下來的40分鐘能讓各位有所收獲 我目前是在台北的一間公司擔任資深架構師 雖然說我目前在台北工作,但其實我老家在台中 能有這個機會跟台中的朋友分享技術感覺蠻開心的
  2. Disruptor是我在2014年開始研究的一個Framework 後來也曾經實際用來開發在工作上的一個即時交易系統 希望今天這個Session能讓更多人瞭解與學習這個Framework
  3. 其實蠻多人容易搞混這2個名詞所代表的意義,跟多執行緒都有關係,這也導致許多人在設計與開發系統時錯誤的使用多執行緒試圖解決效率問題,所以我們先來看一下這2個的差異
  4. 舉個例子
  5. 2個女朋友,不能同時約會 怎麼辦? 時間排開不就好了? 一個約星期六,一個約星期日 感覺這樣安排很美好的舉手? 有舉手的的應該是沒交過二個以上的女朋友 沒舉手的如果有帶女朋友來的,自己想辦法跟她解釋
  6. 當然我也沒同時交過2個 但重點不是交幾個女朋友 重點是並發的多執行緒其實還是只有一個主體 只是開了一堆影分身,讓你感覺像是很多個主體一樣
  7. 執行緒死結(Dead lock) 你同時跟三個女生交往 平常約會的時候都安排的很好 但情人節的時候不小心約在同一個時段的同地方
  8. 那平行呢? 我們一樣舉個例子 爸爸喝啤酒 兒子喝牛奶
  9. 並發的主要目的是提昇反應時間 平行的主要目的是加速解決問題的時間
  10. 這張圖是台北很有名的機車瀑布,發生在早上的上班時間 當一綠燈時,所有的機車都會搶著通過 你們把這些機車想像成很多的執行緒,而這條道路就是被爭搶的共用資源 高並發是很多的執行緒爭搶共享資源的現象
  11. 資源大致可分為2大類,運算與I/O,運算的代表就是CPU,I/O則粗略可分為3類 速度最快的自然是CPU 最慢的是網路
  12. 所謂執行緒安全,是指程式碼在並發的情況下,可以確保多執行緒的排程處理結果是正確的 而同步(Synchronized)是指藉由一些設計讓程式碼具備執行緒安全的特性,進而保證多執行緒存取共享資源的結果正確
  13. 這是一個很常見的Web系統架構,愈往下層走,資源的競爭程度愈高 有效交易會發生在最內層,想要增加有效交易的吞吐量是最不容易的
  14. Disruptor是由英國一家叫LMAX的金融商品交易公司以Java開發的開源非同步事件處理框架,發表於2010年的QCon,當時以單執行緒每秒處理600萬金融撮合交易的超高效能引起關注。
  15. 以股票為例 台灣大(3045)的目前成交價是105元 我以104元買進50張,這就會在市場上掛單104元買入50張 當有人願意以104元賣出25張,我掛的單就會成交25張 撮合交易是一種多對多的交易,每一次撮合時要分別對買方與賣方的交易數量做鎖定,因為每次撮合雙方都有可能只是部份成交 我們可以想像一下,這樣的交易行為如果以DB的方式來實現的話,瞬間交易壓力是非常高的,因為每個人下的交易單,都必須依據買與賣進行比對,在雙方交易時都必須進行鎖定 所以單執行緒能每秒處理600萬是非常恐怖的處理能力
  16. ABC Arbitrage [ˈärbiˌträZH]
  17. 接下來我們來看一下LMAX的主要架構設計
  18. Disruptor這個Framework的架構設計很簡潔 RingBuffer負責存放Event object,提供一個lock-free的類佇列結構,使用Array來實現,同時也是一個Object pool,因此也解決了頻繁的GC回收造成的性能損耗 Sequence 使用遞增序號,用來追蹤目前Producer與Consumer分別處理到哪一個Event,並解決False sharing的問題 關於什麼是False sharing? 待會兒我們會特別說明這個部份 Sequencer 核心邏輯,負責處理並發數據存取的演算法,基於各種使用情景,可以使用不同的策略演算法 SequenceBarrier 依使用情境,可能會有多個SequenceBarrier,分別負責參考Producer與Consumer的Sequence,避免存取了還不能存取的Event IEventHandler 用來實現複雜的業務邏輯 Event 事件訊息
  19. LMAX在設計Disruptor之前,做了很多實驗去驗證可行性及效能,因此發現了Queue本質上的缺陷 Queue的Enqueue及Dequeue行為都是寫入,所以都會需要把Queue lock住才能做Enqueue/Dequeue 以C#的Queue來說,在Enqueue時要先計算現有的Array長度,加上新增的元素後Copy成新的Array,Dequeuq則是直接取第一個元素,然後把head + 1 為了確保Enqueue/Dequeue的不同執行緒不會拿錯,一次只能有一條執行緒存取 後來ConcurrentQueue有針對這部份做了一些設計上的優化,大幅度減少同時寫入的效能損耗,但本質上還是有一個lock存在 -- 底下不講 C#的Queue與ConcurrentQueue都是以Array來實作,但都會需要lock來解決多執行緒存取時的問題,實務上加上Lock通常都會影響效能,以.Net內建的ConcurrentQueue的實作來說,雖然已經使用了很輕量的Interlocked,但還是有個鎖定等待
  20. 因為鎖定的關係,多執行緒同時爭搶Queue的寫入時,只有一條Thread能搶到,其他的Thread就必須先暫停等待通知 執行緒愈多,Context switch造成的延遲浪費也就愈多
  21. *要解釋RingBuffer的運行方式 資料結構其實是Array Array裡放的object address會是連續的記憶體空間,有助於CPU的快取機制,相鄰的item會被pre-load到同一個cache line,所以就不需要頻繁地回到主記憶體加載下一個item 而且因為可以預先建立Array裡的object,更新時只更新內容而不是一直建立新的物件,可以避免大量的GC所造成的性能損耗
  22. 為了提昇CPU運算的效率,CPU都有L1, L2及L3 cache L1跟L2 cache是每個核心獨立,而L3則是多核心共用 每次從Memory讀取資料到Cache是以一個Cache line為單位,常見的Cache line大小是64bytes 當有2個變數X、Y在相鄰的記憶體,就會被一併讀入同一個Cache line 問題,在多處理器的架構下,不同的執行緒會跑在不同的核心 假設在Core 1執行的指令會去修改變數X的內容,而Core 2執行的指令會去修改變數Y 由於同時對同一份Cache line寫入,就會造成2個核心的Cache miss,必須重新從Memory讀入更新過的變數
  23. 如果希望避免剛提到的False sharing,就必須對Cache line做填充的動作,簡單說就是用記憶體空間來換效能 舉列來說,RingBuffer的Tail與Head都是一個長整數,分別由Producer與最後一個Consumer存取,而Producer是會一直改變Head的值,如果Tail與Head放在同一個Cache line 那在多核心的機器就會頻繁的造成False sharing,為了避免這個問題,就可以對這個id做填充的動作
  24. Disruptor能夠達到這麼高的OPS,是因為整個架構設計很明確的解決掉影響效能的幾個瓶頸
  25. Disruptor在實務的設計與開發,要注意幾個地方 需要處理的資料應該放在Event裡,或是在建構EventHandler的時候就傳進去了