SlideShare a Scribd company logo
1 of 41
© 2017 SERIALGAMES inc.
普通のエンジニアが
【Cloud Spanner】 使ってみ
た
1
株式会社シリアルゲームズ 荒井
© 2017 SERIALGAMES inc. 2
株式会社シリアルゲームズ
http://www.serialgames.co.jp/
秋葉原で(主に)ゲーム開発しています
・ラーメン魂
・ガンスリンガーストラトス3 ゲーム連動モバイルサイト
・魔法科高校の劣等生 LOST ZERO
・ファイナルファンタジー レジェンズ2
・ディシディア ファイナルファンタジー オペラオムニア
・にこにこ侵略パズル ピタッチ!
サーバアプリケーション:Java+Tomcat+MySQL+Redis+etc.
© 2017 SERIALGAMES inc. 3
自己紹介
株式会社シリアルゲームズ
http://www.serialgames.co.jp/
サーバサイドエンジニア
荒井 翔地
1985年生まれ 大阪府出身
2014年株式会社シリアルゲームズ入社
・ソーシャルゲームのサーバアプリケーション開発、運用
・Javaは3年くらい
趣味:フットサル、マラソン、トレラン、登山、旅行
© 2017 SERIALGAMES inc.
Cloud Spanner とは?
4
© 2017 SERIALGAMES inc. 5
Cloud Spanner とは?
GCP(Google Cloud Platform)のサービスのひとつ
2012年?〜 Google社内で使用
2017年2月15日 GCPでのβリリース
2017年6月17日 北米リージョンGA
2017年7月16日 東京リージョンGA
© 2017 SERIALGAMES inc. 6
Cloud Spanner とは?
GCP(Google Cloud Platform)のサービスのひとつ
・完全に管理されたミッションクリティカルなRDBMSサービス
・グローバルスケールの整合性、ACIDトランザクション、SQL
・数百台、数千台に水平スケーリング可能
・99.999%の可用性
・Google社内での長期の運用実績(AdWords, Google Play)
© 2017 SERIALGAMES inc.
大規模アクセスが
継続的にあるコンテンツに最適
7
© 2017 SERIALGAMES inc. 8
大規模アクセスが継続的にあるコンテンツに最適
RDBMSとして使える上に自前でスケーリングを考える必要がない
・ノード数を増やせば自動でシャーディングされ、理論上無限にスケール可能
・性能は他のクラウドデータベースと同等に出る
(※ただしSpanner特有のクセを設計で補う必要がある)
・BigTableやDatastoreのデータ構造の上で動くRDBMS
© 2017 SERIALGAMES inc. 9
BigTableのアーキテクチャ
Frontend Server Pool
Cloud BigTable Cluster
Colossus
Node
Shared Log
SSTable SSTable SSTable
Client
Node
Shared Log
SSTable SSTable SSTable
Client
Node
Shared Log
SSTable SSTable SSTable
Client
© 2017 SERIALGAMES inc.
Spanner特有のクセ
10
© 2017 SERIALGAMES inc. 11
Spanner特有のクセ
1.auto_incrementはNG
2.アクセスするテーブルをインターリーブする
3.インデックスはカバリングインデックス
4.カラム追加はFastDDL
5.Spannerのトランザクション
6.急激なUU増加は・・・?
7.集計・分析は向いてない
8.フェイルオーバー気にする必要ない。つまり・・・
© 2017 SERIALGAMES inc. 12
Spanner特有のクセ
1.auto_incrementはNG【重要!】
2.アクセスするテーブルをインターリーブする
3.インデックスはカバリングインデックス
4.カラム追加はFastDDL
5.Spannerのトランザクション
6.急激なUU増加は・・・?
7.集計・分析は向いてない
8.フェイルオーバー気にする必要ない。つまり・・・
© 2017 SERIALGAMES inc. 13
1.auto_incrementはNG
auto_incrementはバッドノウハウ
・auto_increment オプションや serial型 に相当するものが無い
・Spannerでは単調増加 or 単調減少な主キーはNG
・HotSpotが発生する
・主キーを分散させることでデータが分散される
© 2017 SERIALGAMES inc. 14
1.auto_incrementはNG
非シーケンシャルなIDを試してみた
・IDをアプリケーション制御で auto_increment
・1ノード 〜 5ノード:ほとんど性能が上がらず
・IDをINT64の範囲で無作為(かつユニーク)
・1ノード 〜 5ノード:ほぼ線形に性能があがるように
© 2017 SERIALGAMES inc. 15
Spanner特有のクセ
1.auto_incrementはNG
2.アクセスするテーブルをインターリーブする【超重要!!】
3.インデックスはカバリングインデックス
4.カラム追加はFastDDL
5.Spannerのトランザクション
6.急激なUU増加は・・・?
7.集計・分析は向いてない
8.フェイルオーバー気にする必要ない。つまり・・・
© 2017 SERIALGAMES inc. 16
2.インターリーブ
外部キー制約のようなもの
・Spannerではすべてのクエリは自動的に分散処理される
・インターリーブしておくと、
物理的にまとめて格納・トランザクション処理される
・「使うとより速い」的なものではなく、
Spannerを使う以上は「ほぼ必須」なもの
© 2017 SERIALGAMES inc. 17
2.インターリーブ
player_id player_name
1 Arai
2 Obata
3 Maruyama
Relational Data Layout
player_id rule arm_front
1 Yagura C+
1 Area B
3 Hoko A-
※某ゲームとは無関係です
player player_arm_front
© 2017 SERIALGAMES inc. 18
2.インターリーブ
InterLeave Data Layout
player_id player_name
player_id rule arm_front
1 Arai
1 Yagura C+
1 Area B
2 Obata
3 Maruyama
3 Hoko A- ※某ゲームとは無関係です
player_id player_name
1 Arai
2 Obata
3 Maruyama
player_id rule arm_front
1 Yagura C+
1 Area B
3 Hoko A-
player
player_arm_front
© 2017 SERIALGAMES inc. 19
BigTableのアーキテクチャ
Frontend Server Pool
Cloud BigTable Cluster
Colossus
Node
Shared Log
SSTable SSTable SSTable
Client
Node
Shared Log
SSTable SSTable SSTable
Client
Node
Shared Log
SSTable SSTable SSTable
Client
© 2017 SERIALGAMES inc. 20
CREATE TABLE player (
player_id INT64 NOT NULL,
player_name STRING(64),
player_info BYTES(MAX),
player_comment STRING(1024)
) PRIMARY KEY (player_id);
CREATE TABLE player_udemae (
player_id INT64 NOT NULL,
rule INT64 NOT NULL,
arm_front STRING(64)
) PRIMARY KEY (player_id, rule),
INTERLEAVE IN PARENT player ON DELETE CASCADE;
2.インターリーブ
© 2017 SERIALGAMES inc.
サロゲートキーは使えないぞ!
21
© 2017 SERIALGAMES inc. 22
Spanner特有のクセ
1.auto_incrementはNG
2.アクセスするテーブルをインターリーブする
3.インデックスはカバリングインデックス
4.カラム追加はFastDDL
5.Spannerのトランザクション
6.急激なUU増加は・・・?
7.集計・分析は向いてない
8.フェイルオーバー気にする必要ない。つまり・・・
© 2017 SERIALGAMES inc. 23
3.インデックスはカバリングインデックス
・セカンダリインデックスによるアクセスではインデックスキー列と主キー列しか参照
できない
→ 強制的にカバリングインデックス
・STORINGインデックスというものもある
ビューのようなもの
© 2017 SERIALGAMES inc. 24
CREATE TABLE player (
player_id INT64 NOT NULL,
player_name STRING(64),
player_info BYTES(MAX),
player_comment STRING(1024)
) PRIMARY KEY (player_id);
CREATE INDEX storing ON player (
player_name
) STORING (
player_info,
player_comment
)
3.インデックスはカバリングインデックス - STORINGインデックス
© 2017 SERIALGAMES inc. 25
Spanner特有のクセ
1.auto_incrementはNG
2.アクセスするテーブルをインターリーブする
3.インデックスはカバリングインデックス
4.カラム追加はFastDDL
5.Spannerのトランザクション
6.急激なUU増加は・・・?
7.集計・分析は向いてない
8.フェイルオーバー気にする必要ない。つまり・・・
© 2017 SERIALGAMES inc. 26
4.カラム追加はFastDDL
・ALTERでのカラム追加はテーブルの大きさに関係なく一瞬
ただし NOT NULL 指定ができない
・スキーマ変更は即時、レコードへの反映はアクセス時
≒ Amazon Aurora の FastDDL
© 2017 SERIALGAMES inc.
重くなりそうな設計・処理は禁止?
27
© 2017 SERIALGAMES inc. 28
Spanner特有のクセ
1.auto_incrementはNG
2.アクセスするテーブルをインターリーブする
3.インデックスはカバリングインデックス
4.カラム追加はFastDDL
5.Spannerのトランザクション
6.急激なUU増加は・・・?
7.集計・分析は向いてない
8.フェイルオーバー気にする必要ない。つまり・・・
© 2017 SERIALGAMES inc. 29
5.Spannerのトランザクション
アルゴリズムの詳細は割愛。他を参照してください><
トランザクションの種類
・Read-Onlyトランザクション
参照のみであればこちらを使う
・Read/Writeトランザクション
書き込みする場合はこちらを使う
・(ただのReadもある)
© 2017 SERIALGAMES inc. 30
5.Spannerのトランザクション
自動リトライ(クライアントライブラリに組み込まれた挙動)
・Txは正常にcommitされるまで何度もリトライされる
・必ず最初のクエリからリトライされる
分離レベルはシリアライザブル?
・並行したTxの厳格なアボート+クライアントライブラリでの上記挙動も含めてよ
しなにやる仕組み
・ロストアップデートは発生しない
・明示的なロックはできない
・分離レベルの変更も、もちろんできない
© 2017 SERIALGAMES inc. 31
5.Spannerのトランザクション(自動リトライ)
Cloud Spanner API でトランザクションを使用するときは、関数オブジェクトの形式でトランザクションの本体
(つまり、データベースの 1 つ以上のテーブルに対して実行する読み取りと書き込み)を定義します。Cloud
Spanner では、トランザクションが commit するまで、または再試行不可能なエラーが発生するまで、関数の
実行を繰り返します。
static void writeWithTransaction(DatabaseClient dbClient) {
dbClient
.readWriteTransaction()
.run(
new TransactionCallable<Void>() {
@Override
public Void run(TransactionContext transaction) throws Exception {
// selectする
Struct row = transaction.readRow(“player_arm_front”, Key.of(1, “Yagura”));
long armFrontPoint = row.getLong(“point”);
// 条件にもとづいてupdateする
if (armFrontPoint <= 900 && armFrontPoint < 1000){
transaction.buffer(
Mutation.newUpdateBuilder(“player_arm_front”)
.set(“player_id”).to(1)
.set(“rule”).to(“Yagura”)
.set(“arm_front”).to(“B+”)
.build());
return null;
}
}
}); // 問題なければcommitされる
}
© 2017 SERIALGAMES inc. 32
Spanner特有のクセ
1.auto_incrementはNG
2.アクセスするテーブルをインターリーブする
3.インデックスはカバリングインデックス
4.カラム追加はFastDDL
5.Spannerのトランザクション
6.急激なUU増加は・・・?
7.集計・分析は向いてない
8.フェイルオーバー気にする必要ない。つまり・・・
© 2017 SERIALGAMES inc. 33
6.急激なUU増加
データのシャーディングに関して
・インターリーブされるキーに対して一定量のデータがたまることでスプリットされる
・たまり始めてから30分〜60分ラグがある
・データが十分たまることで性能が出てくる
・UUの急激な増加をさばきたいサービスには向いていないかも
© 2017 SERIALGAMES inc. 34
Spanner特有のクセ
1.auto_incrementはNG
2.アクセスするテーブルをインターリーブする
3.インデックスはカバリングインデックス
4.カラム追加はFastDDL
5.Spannerのトランザクション
6.急激なUU増加は・・・?
7.集計・分析は向いてない
8.フェイルオーバー気にする必要ない。つまり・・・
© 2017 SERIALGAMES inc. 35
7.集計・分析
向いていない
・インターリーブされたキーに対する自動シャーディングがSpannerの特徴
・ユーザ毎の処理の負荷分散、スケーリングが本業
・集計、分析はほかに向いているサービスに任せよう
・GCPならBigQuery、BigTable
© 2017 SERIALGAMES inc. 36
Spanner特有のクセ
1.auto_incrementはNG
2.アクセスするテーブルをインターリーブする
3.インデックスはカバリングインデックス
4.カラム追加はFastDDL
5.Spannerのトランザクション
6.急激なUU増加は・・・?
7.集計・分析は向いてない
8.フェイルオーバー気にする必要ない。つまり・・・
© 2017 SERIALGAMES inc. 37
8.フェイルオーバー
全部やってくれる
・・・逆にいうと
・アプリケーションレベルでの致命的ミスがリカバリできない?
・巻き戻し+ロールフォワードの代わりになるものは?
© 2017 SERIALGAMES inc.
まとめ
38
© 2017 SERIALGAMES inc. 39
Spanner使ってみた結果
・フルマネージドのスケーラブルなRDBMS
・実際の使い勝手はRDBMSと結構違う
・大量のアクティブユーザを抱えるサービスなら向いてる
© 2017 SERIALGAMES inc. 40
株式会社シリアルゲームズ
http://www.serialgames.co.jp/
・Unityエンジニア
・サーバエンジニア
秋葉原でゲーム開発したい人、お待ちしてます。
© 2017 SERIALGAMES inc.
ご清聴ありがとうございました。
41

More Related Content

Featured

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellSaba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming LanguageSimplilearn
 

Featured (20)

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

20170822 普通のエンジニアが【Cloud Spanner】使ってみた

  • 1. © 2017 SERIALGAMES inc. 普通のエンジニアが 【Cloud Spanner】 使ってみ た 1 株式会社シリアルゲームズ 荒井
  • 2. © 2017 SERIALGAMES inc. 2 株式会社シリアルゲームズ http://www.serialgames.co.jp/ 秋葉原で(主に)ゲーム開発しています ・ラーメン魂 ・ガンスリンガーストラトス3 ゲーム連動モバイルサイト ・魔法科高校の劣等生 LOST ZERO ・ファイナルファンタジー レジェンズ2 ・ディシディア ファイナルファンタジー オペラオムニア ・にこにこ侵略パズル ピタッチ! サーバアプリケーション:Java+Tomcat+MySQL+Redis+etc.
  • 3. © 2017 SERIALGAMES inc. 3 自己紹介 株式会社シリアルゲームズ http://www.serialgames.co.jp/ サーバサイドエンジニア 荒井 翔地 1985年生まれ 大阪府出身 2014年株式会社シリアルゲームズ入社 ・ソーシャルゲームのサーバアプリケーション開発、運用 ・Javaは3年くらい 趣味:フットサル、マラソン、トレラン、登山、旅行
  • 4. © 2017 SERIALGAMES inc. Cloud Spanner とは? 4
  • 5. © 2017 SERIALGAMES inc. 5 Cloud Spanner とは? GCP(Google Cloud Platform)のサービスのひとつ 2012年?〜 Google社内で使用 2017年2月15日 GCPでのβリリース 2017年6月17日 北米リージョンGA 2017年7月16日 東京リージョンGA
  • 6. © 2017 SERIALGAMES inc. 6 Cloud Spanner とは? GCP(Google Cloud Platform)のサービスのひとつ ・完全に管理されたミッションクリティカルなRDBMSサービス ・グローバルスケールの整合性、ACIDトランザクション、SQL ・数百台、数千台に水平スケーリング可能 ・99.999%の可用性 ・Google社内での長期の運用実績(AdWords, Google Play)
  • 7. © 2017 SERIALGAMES inc. 大規模アクセスが 継続的にあるコンテンツに最適 7
  • 8. © 2017 SERIALGAMES inc. 8 大規模アクセスが継続的にあるコンテンツに最適 RDBMSとして使える上に自前でスケーリングを考える必要がない ・ノード数を増やせば自動でシャーディングされ、理論上無限にスケール可能 ・性能は他のクラウドデータベースと同等に出る (※ただしSpanner特有のクセを設計で補う必要がある) ・BigTableやDatastoreのデータ構造の上で動くRDBMS
  • 9. © 2017 SERIALGAMES inc. 9 BigTableのアーキテクチャ Frontend Server Pool Cloud BigTable Cluster Colossus Node Shared Log SSTable SSTable SSTable Client Node Shared Log SSTable SSTable SSTable Client Node Shared Log SSTable SSTable SSTable Client
  • 10. © 2017 SERIALGAMES inc. Spanner特有のクセ 10
  • 11. © 2017 SERIALGAMES inc. 11 Spanner特有のクセ 1.auto_incrementはNG 2.アクセスするテーブルをインターリーブする 3.インデックスはカバリングインデックス 4.カラム追加はFastDDL 5.Spannerのトランザクション 6.急激なUU増加は・・・? 7.集計・分析は向いてない 8.フェイルオーバー気にする必要ない。つまり・・・
  • 12. © 2017 SERIALGAMES inc. 12 Spanner特有のクセ 1.auto_incrementはNG【重要!】 2.アクセスするテーブルをインターリーブする 3.インデックスはカバリングインデックス 4.カラム追加はFastDDL 5.Spannerのトランザクション 6.急激なUU増加は・・・? 7.集計・分析は向いてない 8.フェイルオーバー気にする必要ない。つまり・・・
  • 13. © 2017 SERIALGAMES inc. 13 1.auto_incrementはNG auto_incrementはバッドノウハウ ・auto_increment オプションや serial型 に相当するものが無い ・Spannerでは単調増加 or 単調減少な主キーはNG ・HotSpotが発生する ・主キーを分散させることでデータが分散される
  • 14. © 2017 SERIALGAMES inc. 14 1.auto_incrementはNG 非シーケンシャルなIDを試してみた ・IDをアプリケーション制御で auto_increment ・1ノード 〜 5ノード:ほとんど性能が上がらず ・IDをINT64の範囲で無作為(かつユニーク) ・1ノード 〜 5ノード:ほぼ線形に性能があがるように
  • 15. © 2017 SERIALGAMES inc. 15 Spanner特有のクセ 1.auto_incrementはNG 2.アクセスするテーブルをインターリーブする【超重要!!】 3.インデックスはカバリングインデックス 4.カラム追加はFastDDL 5.Spannerのトランザクション 6.急激なUU増加は・・・? 7.集計・分析は向いてない 8.フェイルオーバー気にする必要ない。つまり・・・
  • 16. © 2017 SERIALGAMES inc. 16 2.インターリーブ 外部キー制約のようなもの ・Spannerではすべてのクエリは自動的に分散処理される ・インターリーブしておくと、 物理的にまとめて格納・トランザクション処理される ・「使うとより速い」的なものではなく、 Spannerを使う以上は「ほぼ必須」なもの
  • 17. © 2017 SERIALGAMES inc. 17 2.インターリーブ player_id player_name 1 Arai 2 Obata 3 Maruyama Relational Data Layout player_id rule arm_front 1 Yagura C+ 1 Area B 3 Hoko A- ※某ゲームとは無関係です player player_arm_front
  • 18. © 2017 SERIALGAMES inc. 18 2.インターリーブ InterLeave Data Layout player_id player_name player_id rule arm_front 1 Arai 1 Yagura C+ 1 Area B 2 Obata 3 Maruyama 3 Hoko A- ※某ゲームとは無関係です player_id player_name 1 Arai 2 Obata 3 Maruyama player_id rule arm_front 1 Yagura C+ 1 Area B 3 Hoko A- player player_arm_front
  • 19. © 2017 SERIALGAMES inc. 19 BigTableのアーキテクチャ Frontend Server Pool Cloud BigTable Cluster Colossus Node Shared Log SSTable SSTable SSTable Client Node Shared Log SSTable SSTable SSTable Client Node Shared Log SSTable SSTable SSTable Client
  • 20. © 2017 SERIALGAMES inc. 20 CREATE TABLE player ( player_id INT64 NOT NULL, player_name STRING(64), player_info BYTES(MAX), player_comment STRING(1024) ) PRIMARY KEY (player_id); CREATE TABLE player_udemae ( player_id INT64 NOT NULL, rule INT64 NOT NULL, arm_front STRING(64) ) PRIMARY KEY (player_id, rule), INTERLEAVE IN PARENT player ON DELETE CASCADE; 2.インターリーブ
  • 21. © 2017 SERIALGAMES inc. サロゲートキーは使えないぞ! 21
  • 22. © 2017 SERIALGAMES inc. 22 Spanner特有のクセ 1.auto_incrementはNG 2.アクセスするテーブルをインターリーブする 3.インデックスはカバリングインデックス 4.カラム追加はFastDDL 5.Spannerのトランザクション 6.急激なUU増加は・・・? 7.集計・分析は向いてない 8.フェイルオーバー気にする必要ない。つまり・・・
  • 23. © 2017 SERIALGAMES inc. 23 3.インデックスはカバリングインデックス ・セカンダリインデックスによるアクセスではインデックスキー列と主キー列しか参照 できない → 強制的にカバリングインデックス ・STORINGインデックスというものもある ビューのようなもの
  • 24. © 2017 SERIALGAMES inc. 24 CREATE TABLE player ( player_id INT64 NOT NULL, player_name STRING(64), player_info BYTES(MAX), player_comment STRING(1024) ) PRIMARY KEY (player_id); CREATE INDEX storing ON player ( player_name ) STORING ( player_info, player_comment ) 3.インデックスはカバリングインデックス - STORINGインデックス
  • 25. © 2017 SERIALGAMES inc. 25 Spanner特有のクセ 1.auto_incrementはNG 2.アクセスするテーブルをインターリーブする 3.インデックスはカバリングインデックス 4.カラム追加はFastDDL 5.Spannerのトランザクション 6.急激なUU増加は・・・? 7.集計・分析は向いてない 8.フェイルオーバー気にする必要ない。つまり・・・
  • 26. © 2017 SERIALGAMES inc. 26 4.カラム追加はFastDDL ・ALTERでのカラム追加はテーブルの大きさに関係なく一瞬 ただし NOT NULL 指定ができない ・スキーマ変更は即時、レコードへの反映はアクセス時 ≒ Amazon Aurora の FastDDL
  • 27. © 2017 SERIALGAMES inc. 重くなりそうな設計・処理は禁止? 27
  • 28. © 2017 SERIALGAMES inc. 28 Spanner特有のクセ 1.auto_incrementはNG 2.アクセスするテーブルをインターリーブする 3.インデックスはカバリングインデックス 4.カラム追加はFastDDL 5.Spannerのトランザクション 6.急激なUU増加は・・・? 7.集計・分析は向いてない 8.フェイルオーバー気にする必要ない。つまり・・・
  • 29. © 2017 SERIALGAMES inc. 29 5.Spannerのトランザクション アルゴリズムの詳細は割愛。他を参照してください>< トランザクションの種類 ・Read-Onlyトランザクション 参照のみであればこちらを使う ・Read/Writeトランザクション 書き込みする場合はこちらを使う ・(ただのReadもある)
  • 30. © 2017 SERIALGAMES inc. 30 5.Spannerのトランザクション 自動リトライ(クライアントライブラリに組み込まれた挙動) ・Txは正常にcommitされるまで何度もリトライされる ・必ず最初のクエリからリトライされる 分離レベルはシリアライザブル? ・並行したTxの厳格なアボート+クライアントライブラリでの上記挙動も含めてよ しなにやる仕組み ・ロストアップデートは発生しない ・明示的なロックはできない ・分離レベルの変更も、もちろんできない
  • 31. © 2017 SERIALGAMES inc. 31 5.Spannerのトランザクション(自動リトライ) Cloud Spanner API でトランザクションを使用するときは、関数オブジェクトの形式でトランザクションの本体 (つまり、データベースの 1 つ以上のテーブルに対して実行する読み取りと書き込み)を定義します。Cloud Spanner では、トランザクションが commit するまで、または再試行不可能なエラーが発生するまで、関数の 実行を繰り返します。 static void writeWithTransaction(DatabaseClient dbClient) { dbClient .readWriteTransaction() .run( new TransactionCallable<Void>() { @Override public Void run(TransactionContext transaction) throws Exception { // selectする Struct row = transaction.readRow(“player_arm_front”, Key.of(1, “Yagura”)); long armFrontPoint = row.getLong(“point”); // 条件にもとづいてupdateする if (armFrontPoint <= 900 && armFrontPoint < 1000){ transaction.buffer( Mutation.newUpdateBuilder(“player_arm_front”) .set(“player_id”).to(1) .set(“rule”).to(“Yagura”) .set(“arm_front”).to(“B+”) .build()); return null; } } }); // 問題なければcommitされる }
  • 32. © 2017 SERIALGAMES inc. 32 Spanner特有のクセ 1.auto_incrementはNG 2.アクセスするテーブルをインターリーブする 3.インデックスはカバリングインデックス 4.カラム追加はFastDDL 5.Spannerのトランザクション 6.急激なUU増加は・・・? 7.集計・分析は向いてない 8.フェイルオーバー気にする必要ない。つまり・・・
  • 33. © 2017 SERIALGAMES inc. 33 6.急激なUU増加 データのシャーディングに関して ・インターリーブされるキーに対して一定量のデータがたまることでスプリットされる ・たまり始めてから30分〜60分ラグがある ・データが十分たまることで性能が出てくる ・UUの急激な増加をさばきたいサービスには向いていないかも
  • 34. © 2017 SERIALGAMES inc. 34 Spanner特有のクセ 1.auto_incrementはNG 2.アクセスするテーブルをインターリーブする 3.インデックスはカバリングインデックス 4.カラム追加はFastDDL 5.Spannerのトランザクション 6.急激なUU増加は・・・? 7.集計・分析は向いてない 8.フェイルオーバー気にする必要ない。つまり・・・
  • 35. © 2017 SERIALGAMES inc. 35 7.集計・分析 向いていない ・インターリーブされたキーに対する自動シャーディングがSpannerの特徴 ・ユーザ毎の処理の負荷分散、スケーリングが本業 ・集計、分析はほかに向いているサービスに任せよう ・GCPならBigQuery、BigTable
  • 36. © 2017 SERIALGAMES inc. 36 Spanner特有のクセ 1.auto_incrementはNG 2.アクセスするテーブルをインターリーブする 3.インデックスはカバリングインデックス 4.カラム追加はFastDDL 5.Spannerのトランザクション 6.急激なUU増加は・・・? 7.集計・分析は向いてない 8.フェイルオーバー気にする必要ない。つまり・・・
  • 37. © 2017 SERIALGAMES inc. 37 8.フェイルオーバー 全部やってくれる ・・・逆にいうと ・アプリケーションレベルでの致命的ミスがリカバリできない? ・巻き戻し+ロールフォワードの代わりになるものは?
  • 38. © 2017 SERIALGAMES inc. まとめ 38
  • 39. © 2017 SERIALGAMES inc. 39 Spanner使ってみた結果 ・フルマネージドのスケーラブルなRDBMS ・実際の使い勝手はRDBMSと結構違う ・大量のアクティブユーザを抱えるサービスなら向いてる
  • 40. © 2017 SERIALGAMES inc. 40 株式会社シリアルゲームズ http://www.serialgames.co.jp/ ・Unityエンジニア ・サーバエンジニア 秋葉原でゲーム開発したい人、お待ちしてます。
  • 41. © 2017 SERIALGAMES inc. ご清聴ありがとうございました。 41

Editor's Notes

  1. 今日話す内容です