46
Pathtraq Building a computation-centric web s ervice Cybozu Labs Kazuho Oku

YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

Embed Size (px)

DESCRIPTION

The talk describes the architecture of Pathtraq, one of Japan's largest web access statistics service, covering from database compression techniques to embedded SQL in perl.

Citation preview

Page 1: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

PathtraqBuilding a computation-centric web service

PathtraqBuilding a computation-centric web service

Cybozu LabsKazuho Oku

Page 2: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 2

Self Introduction ( 自己紹介 )Self Introduction ( 自己紹介 )

Name: Kazuho Oku名前 : 奥 一穂

Joined Cybozu Labs in 20052005 年からサイボウズ・ラボ勤務

R&D of web-related technologiesWeb 関連技術の研究開発が仕事

Developer of Xiino (Palmscape), JapanizeXiino (Palmscape), Japanize の開発者

Page 3: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 3

The Long Tail of the Internetインターネットのロングテール

Page 4: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 4

The Long Tail of the Internet (cont.) The Long Tail of the Internet (cont.)

1

10

100

1000

10000

100000

1000000

1 10 100 1000 10000 100000 1000000 10000000

URLêî

ÉqÉbÉgêî

Page 5: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 5

The Long Tail of the Internet (cont.)The Long Tail of the Internet (cont.)

y=C ・ x-0.44

Number of pages with 1/10 hits: ×2.75ヒット数が 1/10 のページは 2.75 倍存在

Page 6: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 6

Introduction of Pathtraq (Pathtraq の紹介 )Introduction of Pathtraq (Pathtraq の紹介 )

Alexa-like web access stats serviceAlexa のようなウェブアクセス統計サービス

Per-page, realtime statsページ単位のリアルタイム統計

Extracts and displays what is hotホットな情報を抽出して表示

Page 7: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 7

Introduction of Pathtraq (cont.)Introduction of Pathtraq (cont.)

Demo

Page 8: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 8

Introduction of Pathtraq (cont.)Introduction of Pathtraq (cont.)

Active sample users: 6,300サンプルユーザー数 : 6,300

# of accesses collected: 100M (1M/day)アクセスの収集数 : 1 億件 (100 万件 / 日 )

# of URLs stored: >40M保存されている URL の個数 : 4000 万以上

Data Size: 100GBデータサイズ : 100GB

Page 9: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 9

To Scale Out or Not ( スケールアウトの是非 )To Scale Out or Not ( スケールアウトの是非 )

Depends on CPU / storage costCPU やストレージのコストに依存する

Overhead of scaling out depends on app.アプリケーションによってはスケールアウトに

よるオーバーヘッドが発生

32GB-64GB per server is today’s sweet spot for memory intensive servicesメモリインテンシヴなサービスではサーバあた

り 32GB-64GB のメモリ容量が最安

Page 10: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 10

Data Size vs. Moore’s LawData Size vs. Moore’s Law

1 2 3 4 5 6 7 8 9 10

Year

Data SizeMoore's Law

Page 11: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 11

Data Size vs. Moore’s Law (cont.)Data Size vs. Moore’s Law (cont.)

1 2 3 4 5 6 7 8 9 10

Year

Data SizeMoore's LawSystem Cost

Page 12: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 12

Data Size vs. Moore’s Law (cont.)Data Size vs. Moore’s Law (cont.)

Assume data size is proportional to years of operationデータサイズが運用年数に比例する場合

Software-based optimization is only necessary for first three years最適化は最初の3年間だけ考えればいい

After then, Moore’s Law becomes dominantその後はムーアの法則が支配的になる

Page 13: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 13

Scaling: Our ChoiceScaling: Our Choice

Requirement of first year: 24GB mem.初年度の必要メモリ量 : 24GB

Overhead expected if scaled out↓

No distributed databaseデータベースの分割は行わない

Export some tasks to other serversタスク単位で別サーバへ移動

Page 14: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 14

Hardware of PathtraqHardware of Pathtraq

Main Server (1U)Opteron 2218 x2, 32GB Mem, 500GB HDDMySQL 5.1, Apache, etc.

Helper ServersWeb Content Analysis (Web コンテンツの分析 )Full-text Search ( 全文検索 )

Page 15: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 15

Technical Problems of PathtraqTechnical Problems of Pathtraq

Compression for size & speed圧縮による省メモリ化と高速化

Log analysis requires fast random access高速なランダムアクセスが必要

HDD is toooooo slow ( 〜 3MB/sec.)HDD はランダムアクセスが遅すぎる

Main tables need to be on memory主要テーブルはオンメモリが MUST

Page 16: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 16

Technical Problems of Pathtraq (cont.)Technical Problems of Pathtraq (cont.)

Query cachingLog analysis is heavy task

ログ分析は重たいNeed for sophisticated cache control

高度なキャッシュコントロールが必要

Fast and trustable message queues高速で信頼できるメッセージキュー

Page 17: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 17

Compression for Size and Speedサイズと速度のための圧縮

Page 18: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 18

Facts re Compression ( 圧縮関連の現状)Facts re Compression ( 圧縮関連の現状)

# of access logs stored: 100M保存されているアクセスログ : 約1億件

A single access log: (url,referer,when)アクセスログ : (url,referer, 日時 )

Average length of URL: 70byesURL の平均バイト数 : 70 バイト

Page 19: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 19

URL Compression (URL の圧縮 )URL Compression (URL の圧縮 )

URLs can be compressedURL は圧縮可能http://www.example.com/foobar.html

But too short for gzip (deflate)gzip (deflate) には短すぎる

Need for a compression algorithm using prior knowledge事前知識を使用した圧縮アルゴリズムが必要

Page 20: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 20

URL Compression (cont.)URL Compression (cont.)

Should be searchable in compressed-form, or we cannot use indexes圧縮状態のまま、インデックスを利用して検索で

きる必要がある

Page 21: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 21

URL Compression: Static PPMURL Compression: Static PPM

PPM: Prediction by Partial MatchingUsed by 7-zip, etc. (7-zip 等で使用 )

Use range coder as backendOriginal collation order can be preservedレンジコーダと組み合わせることで文字集合の

順序を保ったまま圧縮できる

Page 22: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 22

URL Compression in PathtraqURL Compression in Pathtraq

Impl. as MySQL UDFsMySQL UDF として実装

Original Query:url like “http://example.com/%”

Query for compressed URL:compress(“http://…/”)<=curl and curl<compress(“http://…0”)

Search in compressed form is faster圧縮版の方が高速に検索可能

Page 23: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 23

URL Compression in Pathtraq (cont.)URL Compression in Pathtraq (cont.)

Size of prior knowledge: 4MB事前知識のサイズ : 4MB

Compression ratio: 37%圧縮率 : 37%

Compression speed: 40MB/sec.Decompression speed: 19.3MB/sec.

Optimized using SSE (SSE で最適化 )

Page 24: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 24

Using Together with InnoDB PluginUsing Together with InnoDB Plugin

InnoDB Pluginreleased May 2008 (2008 年 5 月リリース )supports per-page compression using deflate

Deflate を使用した DB のページ単位圧縮を実現mainly for optimizing disk-based databases

ディスクベースのデータベース最適化に好適

Page 25: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 25

Using Together with InnoDB Plugin (cont.)Using Together with InnoDB Plugin (cont.)

Table size becomes smallest when both compressions are applied両者の併用で高率の圧縮が可能

Compression RatioN/A 100%URL compression 57%InnoDB compression 50%using both 33%

Page 26: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 26

Counter Compression ( カウンタの圧縮 )Counter Compression ( カウンタの圧縮 )

Counter is a list of (hit_count,time)カウンタは ( ヒット数 , 日時 ) のリスト

Time granularity is 1 hour日時の分解能は1時間

Compressed as a sparse array, stored as a blob in DBスパースアレイとして圧縮し、 blob として DB

に保存

Page 27: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 27

Counter Compression (cont.)Counter Compression (cont.)

Accessors impl. as MySQL UDFsMySQL UDF としてアクセサを実装

Accessors decompress sparse arrays and perform vector operations within MySQLアクセサは粗行列を展開して MySQL 内でベクタ

演算を実行

Fewer rows lead to smaller footprint and faster speed行数の減少による省メモリ化と高速化

Page 28: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 28

Query Cachingクエリキャッシュ

Page 29: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 29

Facts re Query CachingFacts re Query Caching

Log analysis is aggregationログ分析は集約演算

Aggregation is heavy task集約演算は重たい

Some queries are repeated very often一部のクエリが頻発

Page 30: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 30

KeyedMutexKeyedMutex

An interprocess mutex impl’ed as a tiny server/client systemプロセス間 Mutex を実現するサーバ

Used to prevent equiv. queries from being sent to MySQL at the same timeMySQL へ同時に同一クエリを行わないために利

Query results shared using Cache::Swiftyクエリ結果は Cache::Swifty で共有

Page 31: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 31

Cache::SwiftyCache::Swifty

A very fast cache using mmap’ed files, optimized for read performance mmap されたファイル群を利用する高速キャッシュ、特

に read が速い

Lock-free reads, flock’ed writes lock-free の読み込みと flock を利用した書き込み

Robust futexes might be usable to optimize write performance on recent linux kernels Robust futexes を使えば書込パフォーマンスも上がるか

Page 32: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 32

Cache::Swifty (cont.)Cache::Swifty (cont.)

70 times faster for 16 bytes reads than Cache::Memcached16 バイトの読込だと Cache::Memcached の 70

倍速

Suitable for caching data on a single server without re-designing the app.単一サーバ上でデータをキャッシュする際に簡

単に利用できる(透過的に追加できる )

Page 33: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 33

Cache::Swifty (cont.)Cache::Swifty (cont.)

Has a feature that notifies a single client to update requested cached data sometime before the cached entry actually expiresキャッシュエントリがエクスパイヤする前に、

クライアントのうちひとつだけに対して更新を推奨できる

Prevents thundering herd upon cache expirationエクスパイヤに伴うブロックを抑制

Page 34: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 34

Filter::SQLFilter::SQL

Pathtraq is heavily dependent on SQLPathtraq の開発においては SQL が中心

Perl code is basically a gluePerl コードは glue としての役割が大きい

DBI (and others) is too complicated for writing glue codeDBI その他はグルーコードを書くには複雑すぎ

Page 35: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 35

Filter::SQL (cont.)Filter::SQL (cont.)

Embedded SQL in perl stylePerl で書きやすい形の埋め込み SQL

EXEC INSERT INTO speakers (surname,firstname) VALUES (’oku’, ’kazuho’);

$sn = ‘oku’; for my $r (SELECT firstname FROM speakers WHERE surname=$sn;) { print “$r->[0]\n”; # kazuho }

Page 36: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 36

Fast and Trustable Message Queues高速で信頼できるメッセージキュー

Page 37: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 37

Facts re Message QueuesFacts re Message Queues

Used for transferring content analysis requests and responsesコンテンツ解析処理を委譲するために使用

Polling a 40M row table is heavy4000 万行のテーブルを poll するのは重たい

Need for a proper message queueちゃんとしたメッセージキューが必要

Page 38: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 38

Q4MQ4M

A Pluggable Storage Engine for MySQL 5.1MySQL 5.1 のプラガブルストレージエンジン

Looks like an ordinal SQL table, has a special mode to operate as MQ一見通常の SQL テーブルだが、 MQ として動作

するモードがある

Page 39: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 39

Features of Q4MFeatures of Q4M

Trustable ( 高信頼性 )Every operation is fsync’ed to disk

すべての操作がディスクへ fsync される

Fast ( 高速性 )>10,000 mess./sec. on Opteron 2218 x 2

Trustable forwarding between queuesキュー間の信頼できる転送機能

no losses or duplicates on network failureネットワーク障害時にもデータロスや複製が発生しな

Page 40: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 40

Q4M: Sample CodeQ4M: Sample Code

Queue Submission:INSERT INTO mq (mess) VALUES (‘hello’);

Queue Subscription:while (1) { next unless SELECT ROW queue_wait(‘mq’);; my $mess = SELECT ROW mess FROM mq;; …. SELECT ROW queue_end(‘mq’);;}

Page 41: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 41

Q4M: Sample Code (cont.)Q4M: Sample Code (cont.)

A wrapper module (Queue::Q4M) is available as wellラッパーモジュール (Queue::Q4M) もあります

Page 42: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 42

Q4M in PathtraqQ4M in Pathtraq

Used for transferring content analysis requests and responsesコンテンツ解析処理を委譲するために使用

Exchanging data between servers located in different iDCs, over SSL-encrypted MySQL connection, between ruby and perl異なる iDC 間でのデータ交換に、 SSL 暗号化さ

れた MySQL 接続を使い、 ruby と perl 間でデータ交換

Page 43: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 43

Conclusionまとめ

Page 44: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 44

Keywords were… ( でてきたキーワード )Keywords were… ( でてきたキーワード )

Data compression is importantMySQL UDFCaching and MutexesEmbedded SQL in PerlMessage Queues

Page 45: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 45

For More InformationFor More Information

See http://labs.cybozu.co.jp/blog/kazuho/

Page 46: YAPC::Asia 2008 Tokyo - Pathtraq - building a computation-centric web service

2008 5 16年 月 日 Pathtraq - building a computation-centric web service 46

Thanks toThanks to

For all the code we useCPAN modules authors and OSS developersThe Developers of Senna and TritonnDaisuke Maki-san (CPAN ID: dmaki) for Gungho an

d Queue::Q4MMembers of Cybozu Labs

Others who gave us advises

and lastly but mostly, to the users of Pathtraq

(Pathtraq のユーザーのみなさん、ありがとうございます )