クラウドネイティブなアプリケーションを実現する
「Azure Cosmos DB」の
キホンと使いドコロ
三宅 和之(Kazuyuki Miyake)
Microsoft MVP

1.
2.
﹣
﹣
Azure Cosmos DB

﹣
﹣

﹣
﹣
﹣
Blog: k-miyake.github.io/blog/
Twitter: @kazuyukimiyake

https://docs.microsoft.com/ja-jp/azure/cosmos-db/

﹣
https://channel9.msdn.com/Events/Build/2018/BRK3319

﹣
https://www.slideshare.net/satonaoki/20180522decode18da19cosmosdb
1.
2.
﹣
﹣
﹣
3.
4.
SQL
MongoDB
Table API
Turnkey global
distribution
Elastic scale out
of storage & throughput
Guaranteed low latency
at the 99th percentile
Comprehensive
SLAs
Five well-defined
consistency models
Azure Cosmos DB
DocumentColumn-family
Key-value Graph
A globally distributed, massively scalable, multi-model database service
出典 https://channel9.msdn.com/Events/Build/2018/BRK3319

﹣
﹣
﹣

参考: A technical overview of Azure Cosmos DB https://azure.microsoft.com/en-us/blog/a-technical-overview-of-azure-cosmos-db/
System Topology
Resource
Manager
Language
Runtime(s)
Hosts
Query
Processor
RSM
Index Manager
Bw-tree++/ LLAMA++
Log Manager
IO Manager
Resource Governor
Transport
Database engine
Admission control
…
…
Planet Earth Azure regions Datacenters Stamps Fault domains
Cluster Machine Replica Database engine
Container
Various agents
出典 https://channel9.msdn.com/Events/Build/2018/BRK3319
Azure Cosmos DB

﹣
Document型
Graph型
Column Family型
• SQL API (旧DocumentDB API)
• MongoDB API
• Gremlin API• Table API
Key-Value型
• Apache Cassandra API(プレビュー)

RDBスキーマ NoSQLドキュメント
• 関連データの埋め込み
• 非正規化

﹣
﹣

﹣
﹣

﹣
﹣
﹣
﹣
﹣
﹣
﹣
﹣

﹣

﹣

﹣

﹣
西日本
東日本
書き込みリージョン
(プライマリ)
読み取りリージョン
(セカンダリ)
Azure Cosmos DB
App Service 1
App Service 2
Azure Traffic
Manager
Readは近くの読み取りリージョンから
Readは近くの読み取りリージョンから
Writeは
書き込みリージョンへ
グローバル分散
レプリケーション
Account
DatabaseDatabaseDatabase
DatabaseDatabaseContainer
DatabaseDatabaseItem
Container(SQL APIでは「コレクション」)
が設計上最も重要な単位

﹣
﹣
﹣

﹣
﹣
﹣
https://www.documentdb.com/capacityplanner
参考: Azure Cosmos DB の要求ユニット https://docs.microsoft.com/ja-jp/azure/cosmos-db/request-units
% IOPS% CPU% Memory


参考: Azure Cosmos DB での予約されたスループット上限の超過
https://docs.microsoft.com/ja-jp/azure/cosmos-db/request-units#RequestRateTooLarge

﹣
﹣
https://docs.microsoft.com/ja-jp/azure/architecture/

﹣

﹣
﹣


﹣
CRUDパターン


CQRSパターン
Azure Cosmos DB
Azure Database for MySQL
Web App
Azure Functions
Change
Feed
ク
ラ
イ
ア
ン
ト
コマンド
クエリ
Write
Read
Publish
書き込みデータストア
他システムなど
読み取りデータストア
Azure Cosmos DB Triggerを使ったAzure Functionの実行フロー

﹣


﹣

﹣

﹣

﹣


﹣
センサーデータ・
クリックストリームetc
スピードレイヤー
データインジェスト
Azure Cosmos DB
Azure
Stream Analytics
Azure
Event Hubs
Hot Path
Cold Path
確定データ
Azure
Data Lake Store
Power BI
Azure Functions
Change
Feed
バッチレイヤー + (サービスレイヤー)
Azure
Databricks
速報データ
低レイテンシを生かす
スピードレイヤーの一次データストアとしてCosmos DBを配置

﹣
﹣
﹣
センサーデータ・
クリックストリームetc
データインジェスト
Azure
Stream Analytics
Azure
Event Hubs
Azure Cosmos DB

﹣

﹣

﹣

﹣
https://docs.microsoft.com/ja-jp/azure/cosmos-db/import-data


﹣

﹣
メッセージ
Function App
キュー
Azure Storage - Queue
monitor
処理実行基盤
App Service
データストア
Cosmos DB
Application Insights
テレメトリ
Azure Monitor
monitor
.NET
SQL Database
App
Service
Web App / API App /
Mobile App
(Windows)
ASP.NET アプリ
Cosmos
DB
SQL API
(Documet型)
マスタ系データ
(読み取り)
トランザクション系データ
(更新、読み取り)
Node.js
App
Service
Web App / API App /
(Linux)
Node.js アプリ
(Express)
Cosmos
DB
MongoDB API
(Documet型)
マスタ系データ
(読み取り)
トランザクション系データ
(更新、読み取り)
Azure Database for
MySQL
Easy out-of-the-box bulk operation functionality
Supports bulk import and update
Auto handles congestion control + transient errors
10x client-side performance improvement
Easily scale-out clients across more VMs
Available starting with .NET and Java
複数ドキュメントの入出力に有効。
移行シナリオ以外にも十分適用可能。
.NET SDKは.NET Coreには未対応。
Remove friction for OSS NoSQL APIs
Provision RU/sec shared across containers
Mix containers with dedicated throughput and
containers with shared throughput
Elastically scale provisioned throughput for a
set of containers at any time
「50,000RU」以上から利用可能のため大規
模データベース向き
Perfect for Intelligent Cloud
and Intelligent Edge Applications
Write scalability around the world
Low latency writes around the world
99.999% High Availability around the world
Well-defined consistency models
Comprehensive conflict management
新規にCosmos DBアカウントを作成すると
プレビュー申込ボタンが表示される
(2018年5月末時点)
Try Azure Cosmos DB for free!
https://azure.microsoft.com/ja-jp/try/cosmosdb/

﹣
﹣

﹣
﹣
﹣

﹣
﹣
Provisionedrequest/sec
Time
12000000
10000000
8000000
6000000
4000000
2000000
Nov 2016
Hourly throughput (request/sec)
参考: Azure Cosmos DB コンテナーのスループットの設定
https://docs.microsoft.com/ja-jp/azure/cosmos-db/set-throughput
•
•
Document document = await _client.ReadDocumentAsync(
UriFactory.CreateDocumentUri(_option.DatabaseId, _option.CollectionId, id),
new RequestOptions{ PartitionKey = new PartitionKey(pkey)}
);
参考 :Azure Cosmos DB でのパーティション分割とスケーリング
https://docs.microsoft.com/ja-jp/azure/cosmos-db/partition-data
// 接続ポリシーの作成
var connectionPolicy = new ConnectionPolicy
{
ConnectionMode = ConnectionMode.Direct, // ダイレクトモード
ConnectionProtocol = Protocol.Tcp // TCPを利用
};
// 読み取りリージョンを優先度順に追加
connectionPolicy.PreferredLocations.Add("Japan West");
connectionPolicy.PreferredLocations.Add("Japan East");
connectionPolicy.PreferredLocations.Add("Southeast Asia");
// DocumentDBクライアントの生成
client = new DocumentClient(
new Uri(ConfigurationManager.AppSettings["endpoint"]),
ConfigurationManager.AppSettings["authKey"],
connectionPolicy
);
client.OpenAsync(); // パフォーマンス改善のため一度接続しておく
接続クライアント生成時に、読み取
りリージョンを設定しておく
参考: PaaSがかりの部屋 - Cosmos DBで読み取りリージョンへ明示的にルーティングする
https://k-miyake.github.io/blog/cosmos-db-preferred-locations/
動的設定の
実装例

﹣
﹣
﹣
﹣


﹣
参考: Azure Cosmos DB の SLA
https://azure.microsoft.com/ja-jp/support/legal/sla/cosmos-db/
一貫性
レベル
説明
Strong
• 最新バージョンのデータを返すことが保証される
• 複数リージョンへの分散ができない
Bounded
Staleness
• 最大でアイテムの K 個のバージョンまたはプレフィックス
あるいは期間 t の分だけ、読み取りが書き込みに対し遅れる
ことが保証
• kとtは設定変更が可能
Session
• クライアント セッション内での一貫性が保証される
• デフォルト
Consistent
Prefix
• 返される更新が、それ以外の全更新の一部のプレフィックス
となる (ギャップなし)
• 書き込みが A, B, C の順で実行された場合、クライアントで
は A、A,B、または A,B,C で返る可能性があるが、A,C また
は B,A,C などで返されることはない
Eventual
• 結果整合性
• 読み取りと書き込みの待機時間は最短
• クライアントからの読み取りは順不同となる可能性がある
参考: Azure Cosmos DB の一貫性レベル
https://docs.microsoft.com/ja-jp/azure/cosmos-db/consistency-levels
トレードオフが存在
• レイテンシ
• 可用性
• スループット

﹣
﹣
https://docs.microsoft.com/ja-jp/azure/cosmos-db/indexing-policies
{
"automatic": true,
"indexingMode": "Consistent",
"includedPaths": [{
"path": "/*",
"indexes": [{
"kind": "Hash",
"dataType": "String",
"precision": -1
}, {
"kind": "Range",
"dataType": "Number",
"precision": -1
}, {
"kind": "Spatial",
"dataType": "Point"
}]
}],
"excludedPaths": [{
"path": "/nonIndexedContent/*"
}]
}

﹣
﹣

﹣
参考:プログラム データベース トランザクション
https://docs.microsoft.com/ja-jp/azure/cosmos-db/programming#database-program-transactions

﹣
﹣

﹣
﹣

﹣
参考: Azure Cosmos DB データベースのセキュリティ
https://docs.microsoft.com/ja-jp/azure/cosmos-db/database-security

﹣
﹣

﹣
﹣
参考: Azure Cosmos DB での自動オンライン バックアップと復元
https://docs.microsoft.com/ja-jp/azure/cosmos-db/online-backup-and-restore




﹣
参考: Azure Cosmos DB のメトリックを使用した監視とデバッグ
https://docs.microsoft.com/ja-jp/azure/cosmos-db/use-metrics
Azure Cosmos DB のキホンと使いドコロ

Azure Cosmos DB のキホンと使いドコロ