[Japan Tech summit 2017] DAL 005

64
Microsoft Tech Summit 2017 本情報の内容(添付文書、リンク先などを含む)は、Microsoft Tech Summit 2017 開催日(2017 11 8-9 日)時点のものであり、予告なく変更される場合があります。

Transcript of [Japan Tech summit 2017] DAL 005

Microsoft Tech Summit 2017本情報の内容(添付文書、リンク先などを含む)は、Microsoft Tech Summit 2017 開催日(2017 年 11 月 8日 - 9 日)時点のものであり、予告なく変更される場合があります。

1.

2.

3.

4.

#mstsjp17 #DAL005

﹣ Blog: k-miyake.github.io/blog/Twitter: @kazuyukimiyake

A globally distributed, massively scalable, multi-model database service

Column-familyDocument

Graph

Turnkey global distribution

Elastic scale out

of storage & throughput

Guaranteed low latency at the 99th percentile

Comprehensive SLAs

Five well-defined consistency models

Table API

Key-value

Azure Cosmos DB

MongoDB API

A globally distributed, massively scalable, multi-model database service

Column-familyDocument

Graph

Turnkey global distribution

Elastic scale out

of storage & throughput

Guaranteed low latency at the 99th percentile

Comprehensive SLAs

Five well-defined consistency models

Table API

Key-value

Azure Cosmos DB

MongoDB API

API Apps

SearchCosmos

DB

Blob

Storage

利用者

認証

App Service

Microsoft Azure

Storage

Queue

Blob

Storage

Functions

Storage

Queue

SQL DatabaseCosmos

DB

Cognitive

ServicesMachine

Learning

Function

s

PC Clients(Windows/Mac)Mobile Clients(iOS/Android)

API Gateway外部システム

Application

Insights

Azure

Monitor

Web Apps

Token

.NET

Identity

Framework

Functions

REST/OAuth2

SPA (Browser App)SPA (Browser App)

負荷モニター/オートスケール

Microsoft導入事例サイト:

https://www.microsoft.com/ja-jp/casestudies/ffs.fujifilm.aspx

1.

2.

3.

■ https://docs.microsoft.com/ja-jp/azure/cosmos-db/

■■Azure Cosmos DB:

The globally distributed, multi-model database

https://myignite.microsoft.com/sessions/54947

■Azure Cosmos DB, design patterns and case studiesfor globally distributed applications

https://myignite.microsoft.com/sessions/54954

参考: A technical overview of Azure Cosmos DB

https://azure.microsoft.com/en-us/blog/a-technical-overview-of-azure-cosmos-db/

Dr. Leslie Lamport

Azure Cosmos DB

Document型

Graph型

Key-Value型

• DocumentDB(SQL) API

• MongoDB API

• Gremlin API (プレビュー)

• Table API(プレビュー)

参考: Automatic regional failover for business continuity in Azure Cosmos DB

https://docs.microsoft.com/ja-jp/azure/cosmos-db/regional-failover

﹣https://www.documentdb.com/capacityplanner

参考: Azure Cosmos DB の要求ユニット

https://docs.microsoft.com/ja-jp/azure/cosmos-db/request-units

「Person」を表現するために4つのエンティティが必要

画面の多くがマスタを参照していることが多い

参考: NoSQL データベースのドキュメントデータのモデル化

https://docs.microsoft.com/ja-jp/azure/cosmos-db/modeling-data

• 1つのJSONは、1つのcsファイルにまとめても良い

• 実際には、各プロパティにJsonPropertyを付与してキャメルケースに対応させる

public class Rootobject{

public string Id { get; set; }public string LastName { get; set; }public string FirstName { get; set; }public Address Address { get; set; }public string Title { get; set; }public Contact[] Contacts { get; set; }

}

public class Address{

public string Line1 { get; set; }public string Line2 { get; set; }public string City { get; set; }public string State { get; set; }public int Zip { get; set; }

}

public class Contact{

public string Email { get; set; }}

Use the best data store for the jobhttps://docs.microsoft.com/en-us/azure/architecture/guide/design-principles/use-the-best-data-store

• 自動レプリケーション• フェールオーバー(自動/手動)

アクティブ

アクティブ

ルールによるトラフィック振分け

参考: Automatic regional failover for business continuity in Azure Cosmos DB

https://docs.microsoft.com/ja-jp/azure/cosmos-db/regional-failover

// 接続ポリシーの作成

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/

動的設定の実装例

Make all things redundanthttps://docs.microsoft.com/en-us/azure/architecture/guide/design-principles/redundancy

Azure Cosmos DB Triggerを使ったAzure Functionの実行フロー

﹣https://docs.microsoft.com/ja-jp/azure/cosmos-db/import-data

Use managed serviceshttps://docs.microsoft.com/en-us/azure/architecture/guide/design-principles/managed-services

デモで利用したソースコードはGithubで公開しています

https://github.com/k-miyake/mstsjp2017-dal005

Try Azure Cosmos DB for free!

https://azure.microsoft.com/ja-jp/try/cosmosdb/

DAL005: Azure Cosmos DB を使った高速分散アプリケーションの設計パターン

Cosmos DBのリソースモデル •

Account

Database

Container

Item

=Collection Graph Table

• ドキュメント• ストアドプロシージャ• トリガー• ユーザ定義関数

参考: Azure Cosmos DB 階層型リソースモデルと中心的概念

https://docs.microsoft.com/ja-jp/azure/cosmos-db/documentdb-resources

Container(DocumentDB APIでは「コレクション」)が設計上最も重要な単位

Pro

visi

oned

req

uest

/ s

ec

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

参考: Azure Cosmos DB の SLA

https://azure.microsoft.com/ja-jp/support/legal/sla/cosmos-db/v1_0/

参考: Azure Cosmos DB での予約されたスループット上限の超過

https://docs.microsoft.com/ja-jp/azure/cosmos-db/request-units#RequestRateTooLarge

一貫性レベル

説明

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

Physical index

https://docs.microsoft.com/ja-jp/azure/cosmos-db/indexing-policies

参考: Azure Cosmos DB のサーバー側 JavaScript プログラミング

https://docs.microsoft.com/ja-jp/azure/cosmos-db/programming#database-program-transactions

﹣https://www.documentdb.com/sql/demo

参考: Azure Cosmos DB DocumentDB API: SQL 構文

https://docs.microsoft.com/ja-jp/azure/cosmos-db/documentdb-sql-query-reference#select-query

PM> Install-Package Microsoft.Azure.DocumentDB

• TCP直接接続モードの例

• ポート範囲 10000 ~ 20000

を開けておく必要あり

参考: Azure Cosmos DB .NET SDK

https://docs.microsoft.com/ja-jp/azure/cosmos-db/documentdb-sdk-dotnet

サンプル実装:

https://github.com/Azure-Samples/documentdb-dotnet-todo-app/blob/master/src/DocumentDBRepository.cs

• SDK利用に多くの解釈がないため、ほぼ同じようなコードに収束する

• Upsertもサポートされている

• 実はSQLを直接使う機会はあまりない

DocumentDBRepository抜粋

参考: 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