usage of thread pool for non blocking frameworks
TRANSCRIPT
![Page 1: Usage of Thread Pool for Non blocking frameworks](https://reader038.vdocuments.pub/reader038/viewer/2022110218/587b3c2c1a28ab9c0e8b4cd3/html5/thumbnails/1.jpg)
ノンブロッキング・フレームワークでのスレッドプールの使い方
![Page 2: Usage of Thread Pool for Non blocking frameworks](https://reader038.vdocuments.pub/reader038/viewer/2022110218/587b3c2c1a28ab9c0e8b4cd3/html5/thumbnails/2.jpg)
Table of Contents
同期処理と非同期処理 スレッドの枯渇 bulkhead-pattern circuit-breaker little’s law
![Page 3: Usage of Thread Pool for Non blocking frameworks](https://reader038.vdocuments.pub/reader038/viewer/2022110218/587b3c2c1a28ab9c0e8b4cd3/html5/thumbnails/3.jpg)
同期処理と非同期処理 同期処理
処理の呼び出し元が処理結果の取得を待つ必要がある 非同期処理
処理の呼び出し元が処理結果の取得を待つことなく次の処理を行うことができる
![Page 4: Usage of Thread Pool for Non blocking frameworks](https://reader038.vdocuments.pub/reader038/viewer/2022110218/587b3c2c1a28ab9c0e8b4cd3/html5/thumbnails/4.jpg)
同期処理と非同期処理 同期処理
![Page 5: Usage of Thread Pool for Non blocking frameworks](https://reader038.vdocuments.pub/reader038/viewer/2022110218/587b3c2c1a28ab9c0e8b4cd3/html5/thumbnails/5.jpg)
同期処理と非同期処理 非同期処理
![Page 6: Usage of Thread Pool for Non blocking frameworks](https://reader038.vdocuments.pub/reader038/viewer/2022110218/587b3c2c1a28ab9c0e8b4cd3/html5/thumbnails/6.jpg)
同期処理と非同期処理 非同期処理を行うメリット
スレッドを効率的に使うことができる 計算が独立している場合は応答時間を早められる
非同期処理のデメリット 複雑性
スレッドプール エラーハンドリング 合成 / メッセージ駆動
![Page 7: Usage of Thread Pool for Non blocking frameworks](https://reader038.vdocuments.pub/reader038/viewer/2022110218/587b3c2c1a28ab9c0e8b4cd3/html5/thumbnails/7.jpg)
スレッドの枯渇
![Page 8: Usage of Thread Pool for Non blocking frameworks](https://reader038.vdocuments.pub/reader038/viewer/2022110218/587b3c2c1a28ab9c0e8b4cd3/html5/thumbnails/8.jpg)
スレッドの枯渇 同期 API にしてしまうとその部分は Request-Response になってしまう
![Page 9: Usage of Thread Pool for Non blocking frameworks](https://reader038.vdocuments.pub/reader038/viewer/2022110218/587b3c2c1a28ab9c0e8b4cd3/html5/thumbnails/9.jpg)
スレッドの枯渇 同期 API にしてしまうとその部分は Request-Response になってしまう
![Page 10: Usage of Thread Pool for Non blocking frameworks](https://reader038.vdocuments.pub/reader038/viewer/2022110218/587b3c2c1a28ab9c0e8b4cd3/html5/thumbnails/10.jpg)
スレッドの枯渇
![Page 11: Usage of Thread Pool for Non blocking frameworks](https://reader038.vdocuments.pub/reader038/viewer/2022110218/587b3c2c1a28ab9c0e8b4cd3/html5/thumbnails/11.jpg)
スレッドの枯渇
![Page 12: Usage of Thread Pool for Non blocking frameworks](https://reader038.vdocuments.pub/reader038/viewer/2022110218/587b3c2c1a28ab9c0e8b4cd3/html5/thumbnails/12.jpg)
遅延の伝播 共通のスレッドプールだけだと、特定のサブシステムや処理の遅延がシステム全体に波及してしまう
![Page 13: Usage of Thread Pool for Non blocking frameworks](https://reader038.vdocuments.pub/reader038/viewer/2022110218/587b3c2c1a28ab9c0e8b4cd3/html5/thumbnails/13.jpg)
遅延の伝播 共通のスレッドプールだけだと、特定のサブシステムや処理の遅延がシステム全体に波及してしまう
![Page 14: Usage of Thread Pool for Non blocking frameworks](https://reader038.vdocuments.pub/reader038/viewer/2022110218/587b3c2c1a28ab9c0e8b4cd3/html5/thumbnails/14.jpg)
ここでやりたいこと サブシステムの障害をシステム全体に波及させないようにする
例えば、配送システムが応答を返せなくなった場合に商品を参照しているだけの機能にまで影響を与えないようにする 障害が発生してもなるべく早く応答を返す
![Page 15: Usage of Thread Pool for Non blocking frameworks](https://reader038.vdocuments.pub/reader038/viewer/2022110218/587b3c2c1a28ab9c0e8b4cd3/html5/thumbnails/15.jpg)
bulkhead-pattern
道路が一つしかないと事故によって全ての車両が待たされる
![Page 16: Usage of Thread Pool for Non blocking frameworks](https://reader038.vdocuments.pub/reader038/viewer/2022110218/587b3c2c1a28ab9c0e8b4cd3/html5/thumbnails/16.jpg)
bulkhead-pattern
道路を複数用意しておくと事故の起きている経路を通らない車両は待たされない
![Page 17: Usage of Thread Pool for Non blocking frameworks](https://reader038.vdocuments.pub/reader038/viewer/2022110218/587b3c2c1a28ab9c0e8b4cd3/html5/thumbnails/17.jpg)
bulkhead-pattern
スレッドプールは障害ドメインによって分ける データベースや外部サービスとの接続
スレッドの枯渇による影響を最小限に留められるようにする
![Page 18: Usage of Thread Pool for Non blocking frameworks](https://reader038.vdocuments.pub/reader038/viewer/2022110218/587b3c2c1a28ab9c0e8b4cd3/html5/thumbnails/18.jpg)
bulkhead-pattern
どうしても暗黙的にしたい場合 (Scala) ファントム参照を使うとよいかも?
![Page 19: Usage of Thread Pool for Non blocking frameworks](https://reader038.vdocuments.pub/reader038/viewer/2022110218/587b3c2c1a28ab9c0e8b4cd3/html5/thumbnails/19.jpg)
Little’s Law
L = λW L: スレッド数 λ: 到着率 (= 単位時間当たりのリクエスト数 ) W: 1 リクエスト当たりの処理時間
処理時間が安定している処理では、リトルの法則を適用することで効率的にスレッドを利用できる 処理時間が安定していない場合は平均時間だけでは考えないようにする
CPU バウンドな処理でも core 数以上のスレッドを割り当てないとスループットが出ないこともある
![Page 20: Usage of Thread Pool for Non blocking frameworks](https://reader038.vdocuments.pub/reader038/viewer/2022110218/587b3c2c1a28ab9c0e8b4cd3/html5/thumbnails/20.jpg)
circuit-breaker
スレッドプールを分割してそれぞれのプールサイズを適切に設定しても、障害の起きている経路を通る処理は遅延してしまう こういった経路はフェイルオーバーさせることで応答性を保つことができる。
![Page 21: Usage of Thread Pool for Non blocking frameworks](https://reader038.vdocuments.pub/reader038/viewer/2022110218/587b3c2c1a28ab9c0e8b4cd3/html5/thumbnails/21.jpg)
circuit-breaker
スレッドプールを分割してそれぞれのプールサイズを適切に設定しても、障害の起きている経路を通る処理は遅延してしまう こういった経路はフェイルオーバーさせることで応答性を保つことができる。
http://doc.akka.io/docs/akka/snapshot/common/circuitbreaker.html
![Page 22: Usage of Thread Pool for Non blocking frameworks](https://reader038.vdocuments.pub/reader038/viewer/2022110218/587b3c2c1a28ab9c0e8b4cd3/html5/thumbnails/22.jpg)
まとめ bullhead-pattern
障害・遅延がシステム全体に波及しないように複数のスレッドプールを使い分ける 非同期処理のライブラリを適切に扱えているかどうかをチェックしよう
little’s raw スレッドを効率的に使えるように適切なプールサイズを見積もる
circuit-breaker 障害が起きた経路はフェイルオーバーさせる