Share
Embed
[ウェビナー] 実録! Cosmos DBでのチャットサービス稼働 ~MongoDB API編~
isana.net,inc.
August 14, 2018
Transcript
28 slides
-
実録! Cosmos DB での チャットサービス稼働 〜MongoDB API編〜 イサナドットネット株式会社 技術部 技術ディレクター
⼆⼝ 俊介 Copyright © 2018 isana.net,inc. All Rights Reserved. -
概要 • Cosmos DB 導⼊のメリット • 稼働しているシステム構成について • コスト⾒積り実例 • ノウハウ / Tips
Copyright © 2018 isana.net,inc. All Rights Reserved. -
Everything as a Service on Azure NOSQLは? 特に MongoDB は?
アプリサーバ Azure Web Apps キャッシュサーバ Azure Redis Cache サーバ監視 Application Insights データベース Azure SQL Database ? Copyright © 2018 isana.net,inc. All Rights Reserved. -
MongoDB as a Service がやってきた 他、SQL / Graph / テーブル/
Cassandra API に対応したマルチモデルデータベース Copyright © 2018 isana.net,inc. All Rights Reserved. -
グローバルに配信 柔軟なスケールアウト 短い待機時間を保証 5つの整合性のモデル (整合性・可⽤性・待機時間の トレードオフ選択) 包括的なSLA (スループット・可⽤性保証など) ※公式資料より https://docs.microsoft.com/ja-jp/azure/cosmos-db/introduction
Cosmos DB 導⼊のメリット Copyright © 2018 isana.net,inc. All Rights Reserved. -
Cosmos DB のここがスゴイ Cosmos DB 対応は 1⾏ 書き換えで動作 (MongoDB API)
⾃動シャーディング・⾃動インデックス 詳しくは Web で 可⽤性 99.99% (すべてのマルチリージョンで⾮同期レプ リケーション使⽤時なら、99.999%) 待ち時間10ミリ秒以内 (99%の確率、⼀般的な1KBでの読 み込みにおけるエンドツーエンド) Copyright © 2018 isana.net,inc. All Rights Reserved. -
サービス紹介 Spika for Business • ⾃社サービス・製品に • オンプレミスで導⼊ • フレキシブルな
カスタマイズ性 • リアルタイムチャット • ユーザー数の関係ない ライセンス費⽤ Spika for business Copyright © 2018 isana.net,inc. All Rights Reserved. -
サービス紹介 Spika on Azure Copyright © 2018 isana.net,inc. All Rights
Reserved. テキストchat と・・・ 翻訳 分析 提案 Q&A -
システム構成 (ほとんど PaaS) iOS/Android Azure Web App for Containers Redis
Cache Cosmos DB Docker Hub Copyright © 2018 isana.net,inc. All Rights Reserved. -
気になる課⾦体系 処理能⼒ 要求ユニット(RU) がキーワード RU/秒 に対して課⾦が発⽣ 使⽤前に予約が必要 (随時変更可能) 予約はコレクション単位 (最低400RU)
1RU : 1KB データを取得するときの処理能⼒ Copyright © 2018 isana.net,inc. All Rights Reserved. -
コレクション数を減らす いくつかのコレクションのデータを⼀緒に 格納しましょう https://docs.microsoft.com/ja-jp/azure/cosmos-db/mongodb-mongoose より パフォーマンスが⼼配? ⇒ ⾃動でパーティション分割・インデックス調整 Copyright ©
2018 isana.net,inc. All Rights Reserved. -
データの読み書き量を減らす キャッシュを利⽤しましょう 期限があるデータ ex) セッション情報 頻繁に使うデータ ex) ユーザー情報 消えてもいいデータ ex)
未読 / 既読管理 最悪 Copyright © 2018 isana.net,inc. All Rights Reserved. -
⼀度に集計しない (すると⼤変) 秒あたりの処理量を減らしましょう ⇒ aggregation / count は要注意 すべてのデータを読む必要がある 読んだドキュメントサイズが膨⼤になる
予約RU/秒が⼤量に必要 運⽤レベルのデータ件数で負荷試験が重要 Copyright © 2018 isana.net,inc. All Rights Reserved. -
RU/秒をオートスケール 定期バッチ処理などで RU/秒 を 制御しましょう (Azure CLI) 失敗例) Webサーバ の
CPU / メモリ使⽤率 でスケール調整を計画 ⇒ 意外とアクセス数と⽐例せず、 ⇒ Cosmos DB の RU/秒 調整には使えず 時間帯 / 曜⽇ での管理がオススメ ⼀時的なアクセス増⼤に備え、バッファは必要 Copyright © 2018 isana.net,inc. All Rights Reserved. -
実際の⾒積り⽅(1) Endpoint Collection Method Option RU /signin apikeys findOne key:
? 3.84 organizations findOne _id: ? 2.99 users findOne _id: ? 3.50 users update _id: ? 4.81 APIごと / クエリーごとに RU を計測 削除では消えたデータサイズ分の RU が消費 Copyright © 2018 isana.net,inc. All Rights Reserved. 実例 -
実際の⾒積り⽅(2) 負荷試験と合わせ、RU をメトリックスで確認 Copyright © 2018 isana.net,inc. All Rights Reserved.
データ件数に合わせ RU/秒が増⼤するのは 要注意 -
RU消費量計測 以下のコードで取得できます https://docs.microsoft.com/ja-jp/azure/cosmos-db/set-throughput より抜粋 db.executeDbAdminCommand({getLastRequestStatistics: 1}, (err, doc) => {
if(!err) { console.log(`RequestCharge is ${doc.RequestCharge}`); } }); • メトリクス / 診断ログから取得 • Azure Portal 上でコマンド実⾏ Copyright © 2018 isana.net,inc. All Rights Reserved. 他の⽅法 -
メトリクスは過信しない メトリクスでエラー件数がグラフで表⽰されますが・・・ メトリクスは参考までに、ログからエラー確認 429エラー発⽣ (Request rate is too large) レスポンスが⼤きすぎで中断?
メトリクスに出ない エラーなし? Copyright © 2018 isana.net,inc. All Rights Reserved. -
データ移⾏ Mongo DB リストア ⇒ 簡単にRU/秒の上限を超えます https://docs.microsoft.com/ja-jp/azure/cosmos-db/mongodb-migrate より リストア時には以下のようなオプションが必要です。 mongorestore
--host 〜 -u〜 -p 〜 --ssl --sslAllowInvalidCertificates ./dumps/dump --numInsertionWorkersPerCollection 4 --batchSize 24 RUを計算する⽅法が公式ページであるが、⼩さい値から初めて、 徐々に⼤きくするほうが簡単 または Windows 向けのデータ移⾏ツール (azure-documentdb- datamigrationtool) を使⽤してください Copyright © 2018 isana.net,inc. All Rights Reserved. -
忘れやすいポイント SSL接続 再接続設定 var options = { autoReconnect: true, //
再接続あり connectTimeoutMS: 60000, // コネクションタイムアウト [ms] socketTimeoutMS: 60000, // ソケットタイムアウト [ms] reconnectInterval: 3000, // 再接続試行間隔 [ms] reconnectTries: Infinity // できるかぎり再接続を試みる }; mongoose.connect(‘mongodb://〜&ssl=true’, options, function(err){ ... }); MongoDB では以下の設定を忘れずに Copyright © 2018 isana.net,inc. All Rights Reserved. -
まとめ ⾼性能で管理が簡単 = PaaSにフィット コスト・パフォーマンス最適化が必要 ⼤きなコード変更は不要 まだまだ進化中 Copyright © 2018
isana.net,inc. All Rights Reserved.