Casual Compression
     on   
-present at MongoDB Casual Talks-

          @just_do_neet
Today’s Agenda
         今日のお題目


            •MongoDBの課題

            •MongoDBでのデータ圧縮

            •まとめ




MongoDB Casual Talks #1       2
MongoDB
         MongoDB




         http://www.mongodb.org/



                                   http://www.mongodb.jp/


         •10gen社が主体として開発しているオープンソース
          所謂「NoSQL」の一つ
MongoDB Casual Talks #1                                     3
MongoDB is over?
         MongoDBはオワコン?




                            http://suzuzuzuru.blogspot.jp/2012/04/mongodb.html

                    http://blog.engineering.kiip.me/post/20988881092/a-year-with-mongodb

MongoDB Casual Talks #1                                                                    4
MongoDB is over?
         MongoDBはオワコン?




                          http://www.zopyx.com/blog/goodbye-mongodb


MongoDB Casual Talks #1                                               5
Cons.
         MongoDBの欠点(主観含む)


            •トランザクション未サポート

                •トランザクションは甘え (ドヤァ

            •Global Lock(2.2からCollection Lockに?)

            •システムリソースが肥大化(メモリ、ディスク)

            •データ圧縮未対応(通信、データストア共)

            •セキュリティ周りが弱い etc.

MongoDB Casual Talks #1                            6
Cons.
         MongoDBの欠点(主観含む)


            •トランザクション未サポート

                •トランザクションは甘え (ドヤァ

            •Global Lock(2.2からCollection Lockに?)

            •システムリソースが肥大化(メモリ、ディスク)

            •データ圧縮未対応(通信、データストア共)

            •セキュリティ周りが弱い etc.

MongoDB Casual Talks #1                            7
Compress
         圧縮関連のJIRA


            •BSONデータの圧縮→not supported!




                          https://jira.mongodb.org/browse/SERVER-164




MongoDB Casual Talks #1                                                8
Compress
         圧縮関連のJIRA


            •通信路の圧縮→not supported!




                          https://jira.mongodb.org/browse/SERVER-3018




MongoDB Casual Talks #1                                                 9
Compress
         Quoraに掲載されている「最も興味があるMongoDBのJIRA」




http://www.quora.com/MongoDB/What-are-the-most-interesting-MongoDB-JIRA-issues


MongoDB Casual Talks #1                                                     10
Compress vs Not Compress
         圧縮:非圧縮のデータサイズの差


            •下記例は同一フォーマットの文字列データを格納
             した際の比較(MongoDB / HBase)

            •MongoDBはHBase(snappy圧縮時)の三倍強。
             700000000                                 MongoDB
             600000000                                 MongoDB(fragment)
             500000000                                 HBase
                                                       HBase(fragment)
             400000000                                 HBase(snappy)
             300000000
             200000000
             100000000
                          0
                              size(1,000,000 record)

MongoDB Casual Talks #1                                                    11
Cons.
         MongoDBの欠点(主観含む)


            •Big Dataを扱う環境にはあまり向かない。

                •スケールするが故に、下手にそれなりの規模の
                  システムに導入するとサーバー無限増殖の刑
                  に...




MongoDB Casual Talks #1                  12
圧縮




MongoDB Casual Talks #1        13
Casual Compression
         MongoDBでのカジュアルなデータ圧縮


            •以下について試してみました。
                1.フィールド名をできるだけ短くする

                2.特定のデータをbinary形式で保存

                3.小さい正整数の整数符号化




MongoDB Casual Talks #1                14
#1 To shorten filed name
         フィールド名の短縮


            •MongoDBはBSON形式でデータを保存

            •BSONは1つのドキュメントの中にフィールド名
             情報を持つ。

                •複数のレコードが同一のフィールド名を持って
                  いても、1レコードごとに情報を持つ。




MongoDB Casual Talks #1                  15
#1 To shorten filed name
         フィールド名の短縮




                          http://bsonspec.org/#/specification




MongoDB Casual Talks #1                                        16
#1 To shorten filed name
         フィールド名の短縮




MongoDB Casual Talks #1            17
#1 To shorten filed name
         フィールド名の短縮




                          100万件で

                          約8MBの差




MongoDB Casual Talks #1            18
#1 To shorten filed name
         参考ブログ




           http://christophermaier.name/blog/2011/05/22/MongoDB-key-names

MongoDB Casual Talks #1                                                     19
#1 To shorten filed name
         参考ブログ




           http://christophermaier.name/blog/2011/05/22/MongoDB-key-names

MongoDB Casual Talks #1                                                     20
#1 To shorten filed name
         OR Mapperを用いたfield nameのマッピング


            •OR Mapperでfield nameのマッピングを行うと名
             前が短すぎる弊害は多少抑制できる。

            •JavaではMorphiaがオススメ。
             http://code.google.com/p/morphia/

                •Spring Dataは重厚すぎる気がする。




MongoDB Casual Talks #1                          21
#1 To shorten filed name
         OR Mapperを用いたfield nameのマッピング



                @Data
                @Entity(value = "slim")
                class TestDTOSlim {
                  @Id
                  ObjectId id;
                  @Property(value = "u")
                  long uuid;
                  @Property(value = "n")
                  String name;
                  @Property(value = "d")
                  Date date;
                }




MongoDB Casual Talks #1                    22
#2 Convert to binary
         特定のデータをbinary形式に変換


            •MongoDBが圧縮をサポートしていないのでアプ
             リケーション側で圧縮をしてbinaryで保存。

                •特定のフィールドを圧縮

                •BSON以外の構造化フォーマットを用いて複数
                  フィールドをまとめてシリアライズ→圧縮




MongoDB Casual Talks #1                   23
#2 Convert to binary
         検証に使用したデータモデル




                public class NormalModel {
                  @Id
                  ObjectId oid;
                  long uuid;
                  int id;
                  char flag;
                  String name;
                  String description;
                }




MongoDB Casual Talks #1                      24
#2 Convert to binary
         検証で使用した圧縮アルゴリズム


            •Deflate(Best Compression)

            •LZO

            •Google Snappy

            •LZ4




MongoDB Casual Talks #1                 25
#2 Convert to binary
         Google Snappy


            •2011/4ごろにGoogleがオープンソースとして公開
             した圧縮アルゴリズム。
             高速な圧縮・伸張が特徴。

            •http://code.google.com/p/snappy/




MongoDB Casual Talks #1                         26
#2 Convert to binary
         LZ4


            •Google Snappyよりも圧縮・伸張速度が速いと言
               われている圧縮アルゴリズム。

            •http://code.google.com/p/lz4/




MongoDB Casual Talks #1                      27
#2 Convert to binary
         BSON以外のシリアライズ手法


            •Message Pack
             http://msgpack.org/




MongoDB Casual Talks #1            28
#2 Convert to binary
         検証条件


            •以下の条件で比較

                1.何もしない

                2.フィールド名の短縮

                3. 2 + 特定のフィールドの圧縮

                4.複数のフィールド情報をMessagePackでシリ
                  アライズ

                5. 4.+圧縮
MongoDB Casual Talks #1                       29
#2 Convert to binary
         検証結果

                                           normal
                                           short key
                                           short key + msgpack

  1500000


  1200000


    900000


    600000


    300000


          0
                    none   deflate   lzo   snappy           lz4

MongoDB Casual Talks #1                                           30
#2 Convert to binary
         検証結果

                                           normal
                                           short key
       1,2, 4(非圧縮)                         short key + msgpack

  1500000


  1200000


    900000


    600000


    300000


          0
                    none   deflate   lzo   snappy           lz4

MongoDB Casual Talks #1                                           31
#2 Convert to binary
         検証結果

                                           normal
                                           short key
                                           short key + msgpack

  1500000                            3,5(圧縮)

  1200000


    900000


    600000


    300000


          0
                    none   deflate   lzo   snappy           lz4

MongoDB Casual Talks #1                                           32
#2 Convert to binary
         検証結果

                                           normal
                                           short key
                                           short key + msgpack

  1500000


  1200000


    900000


    600000


    300000


          0
                    none   deflate   lzo   snappy           lz4

MongoDB Casual Talks #1                                           33
#2 Convert to binary
         検証結果


            •「複数のフィールドをMessagePackでシリアライ
             ズ+圧縮アルゴリズムで圧縮」
             の組み合わせで最大2/3の省サイズ化に成功。

                •データパターン/データモデルによって傾向は
                  様々だと思う。

                •圧縮・シリアライズのオーバーヘッドに注意。

                •独自binary化すると後戻りできないので注意。

MongoDB Casual Talks #1                     34
#3 Integer Encoding
         整数値符号化


            •たとえば数字の「1」を数バイト使用して表現す
             るのはもったいない。→整数値符号化

                •Variable Byte Code

                •Simple9

                •Simple16

                •etc...



MongoDB Casual Talks #1               35
#3 Integer Encoding
         Variable Byte Code


            •整数値の値を最小1バイトで表現するための符号
             化方式。数値部7bit(0~127)と、数値終端を表すフ
             ラグ1bitの組み合わせで数値を符号化します。

            •https://gist.github.com/3003981
                • 0x00-0x7f     : 1xxxxxxx


                • 0x80-0x3fff   : 0xxxxxxx 1xxxxxxx


                • 0x4000-0x1fffff : 0xxxxxxx 0xxxxxxx 1xxxxxxx
                  ※「x」は0、もしくは1



MongoDB Casual Talks #1                                          36
#3 Integer Encoding
         検証に使用したデータモデル




               public class NormalModel{
              ! @Id
              ! ObjectId oid;
              ! @Property(value = "id")
              ! int id; //もしくはlong
                }




MongoDB Casual Talks #1                    37
#3 Integer Encoding
         検証条件


            •以下の条件で比較

                1. 整数値をinteger(4byte)で保存

                2. 整数値をlong(8byte)で保存

                3. 整数値をVariable Byte Codeで変換して保存




MongoDB Casual Talks #1                            38
#3 Integer Encoding
         検証結果

                                                    integer                 long
                                                    variable byte code


 37000000

 36000000

 35000000

 34000000

 33000000

 32000000

 31000000

 30000000
                          max : 128   max : 16384               max : 2097152

MongoDB Casual Talks #1                                                            39
#3 Integer Encoding
         検証結果


            •整数値符号化で保存をしたら、逆にIntegerよりも
             サイズが大きくなった・・・

                •BSONの仕様が関係
                   int32 : 4bytes
                   int64 : 8bytes
                   binary : int32 subtype(byte*)




MongoDB Casual Talks #1                            40
Casual Compression
         MongoDBでのカジュアルなデータ圧縮


            •以下について試してみました。
                1.フィールド名をできるだけ短くする
                  →◎

                2.特定のデータをbinary形式で保存
                  →⃝

                3.小さい正整数の整数符号化
                  →☓

MongoDB Casual Talks #1                41
参考情報




MongoDB Casual Talks #1          42
HBase
         HBaseなら圧縮をサポートしてます


            •HBaseなら....

                •データの圧縮に標準で対応
                  (圧縮したいTableのFamilyごとに指定可能。
                  アルゴリズムも複数選択可能)

                •可変長整数値に標準で対応
                  (VIntWritable / VLongWritable)

                •大きいデータを扱う場合はHBaseを(ry

MongoDB Casual Talks #1                            43
まとめ




MongoDB Casual Talks #1         44
Conclusion
         まとめにかえて


            •MongoDBはデータサイズが肥大化しがちです
             が、アプリケーション側のカジュアルな工夫で多
             少はデータサイズの削減ができます。

            •用途に応じて、適切な現場でMongoDBを使いま
             しょう。

            •個人的にはRedisが好きです。




MongoDB Casual Talks #1                 45
ご清聴
                ありがとうございました



MongoDB Casual Talks #1         46

Casual Compression on MongoDB

  • 1.
    Casual Compression on    -present at MongoDB Casual Talks- @just_do_neet
  • 2.
    Today’s Agenda 今日のお題目 •MongoDBの課題 •MongoDBでのデータ圧縮 •まとめ MongoDB Casual Talks #1 2
  • 3.
    MongoDB MongoDB http://www.mongodb.org/ http://www.mongodb.jp/ •10gen社が主体として開発しているオープンソース 所謂「NoSQL」の一つ MongoDB Casual Talks #1 3
  • 4.
    MongoDB is over? MongoDBはオワコン? http://suzuzuzuru.blogspot.jp/2012/04/mongodb.html http://blog.engineering.kiip.me/post/20988881092/a-year-with-mongodb MongoDB Casual Talks #1 4
  • 5.
    MongoDB is over? MongoDBはオワコン? http://www.zopyx.com/blog/goodbye-mongodb MongoDB Casual Talks #1 5
  • 6.
    Cons. MongoDBの欠点(主観含む) •トランザクション未サポート •トランザクションは甘え (ドヤァ •Global Lock(2.2からCollection Lockに?) •システムリソースが肥大化(メモリ、ディスク) •データ圧縮未対応(通信、データストア共) •セキュリティ周りが弱い etc. MongoDB Casual Talks #1 6
  • 7.
    Cons. MongoDBの欠点(主観含む) •トランザクション未サポート •トランザクションは甘え (ドヤァ •Global Lock(2.2からCollection Lockに?) •システムリソースが肥大化(メモリ、ディスク) •データ圧縮未対応(通信、データストア共) •セキュリティ周りが弱い etc. MongoDB Casual Talks #1 7
  • 8.
    Compress 圧縮関連のJIRA •BSONデータの圧縮→not supported! https://jira.mongodb.org/browse/SERVER-164 MongoDB Casual Talks #1 8
  • 9.
    Compress 圧縮関連のJIRA •通信路の圧縮→not supported! https://jira.mongodb.org/browse/SERVER-3018 MongoDB Casual Talks #1 9
  • 10.
    Compress Quoraに掲載されている「最も興味があるMongoDBのJIRA」 http://www.quora.com/MongoDB/What-are-the-most-interesting-MongoDB-JIRA-issues MongoDB Casual Talks #1 10
  • 11.
    Compress vs NotCompress 圧縮:非圧縮のデータサイズの差 •下記例は同一フォーマットの文字列データを格納 した際の比較(MongoDB / HBase) •MongoDBはHBase(snappy圧縮時)の三倍強。 700000000 MongoDB 600000000 MongoDB(fragment) 500000000 HBase HBase(fragment) 400000000 HBase(snappy) 300000000 200000000 100000000 0 size(1,000,000 record) MongoDB Casual Talks #1 11
  • 12.
    Cons. MongoDBの欠点(主観含む) •Big Dataを扱う環境にはあまり向かない。 •スケールするが故に、下手にそれなりの規模の システムに導入するとサーバー無限増殖の刑 に... MongoDB Casual Talks #1 12
  • 13.
  • 14.
    Casual Compression MongoDBでのカジュアルなデータ圧縮 •以下について試してみました。 1.フィールド名をできるだけ短くする 2.特定のデータをbinary形式で保存 3.小さい正整数の整数符号化 MongoDB Casual Talks #1 14
  • 15.
    #1 To shortenfiled name フィールド名の短縮 •MongoDBはBSON形式でデータを保存 •BSONは1つのドキュメントの中にフィールド名 情報を持つ。 •複数のレコードが同一のフィールド名を持って いても、1レコードごとに情報を持つ。 MongoDB Casual Talks #1 15
  • 16.
    #1 To shortenfiled name フィールド名の短縮 http://bsonspec.org/#/specification MongoDB Casual Talks #1 16
  • 17.
    #1 To shortenfiled name フィールド名の短縮 MongoDB Casual Talks #1 17
  • 18.
    #1 To shortenfiled name フィールド名の短縮 100万件で 約8MBの差 MongoDB Casual Talks #1 18
  • 19.
    #1 To shortenfiled name 参考ブログ http://christophermaier.name/blog/2011/05/22/MongoDB-key-names MongoDB Casual Talks #1 19
  • 20.
    #1 To shortenfiled name 参考ブログ http://christophermaier.name/blog/2011/05/22/MongoDB-key-names MongoDB Casual Talks #1 20
  • 21.
    #1 To shortenfiled name OR Mapperを用いたfield nameのマッピング •OR Mapperでfield nameのマッピングを行うと名 前が短すぎる弊害は多少抑制できる。 •JavaではMorphiaがオススメ。 http://code.google.com/p/morphia/ •Spring Dataは重厚すぎる気がする。 MongoDB Casual Talks #1 21
  • 22.
    #1 To shortenfiled name OR Mapperを用いたfield nameのマッピング @Data @Entity(value = "slim") class TestDTOSlim { @Id ObjectId id; @Property(value = "u") long uuid; @Property(value = "n") String name; @Property(value = "d") Date date; } MongoDB Casual Talks #1 22
  • 23.
    #2 Convert tobinary 特定のデータをbinary形式に変換 •MongoDBが圧縮をサポートしていないのでアプ リケーション側で圧縮をしてbinaryで保存。 •特定のフィールドを圧縮 •BSON以外の構造化フォーマットを用いて複数 フィールドをまとめてシリアライズ→圧縮 MongoDB Casual Talks #1 23
  • 24.
    #2 Convert tobinary 検証に使用したデータモデル public class NormalModel { @Id ObjectId oid; long uuid; int id; char flag; String name; String description; } MongoDB Casual Talks #1 24
  • 25.
    #2 Convert tobinary 検証で使用した圧縮アルゴリズム •Deflate(Best Compression) •LZO •Google Snappy •LZ4 MongoDB Casual Talks #1 25
  • 26.
    #2 Convert tobinary Google Snappy •2011/4ごろにGoogleがオープンソースとして公開 した圧縮アルゴリズム。 高速な圧縮・伸張が特徴。 •http://code.google.com/p/snappy/ MongoDB Casual Talks #1 26
  • 27.
    #2 Convert tobinary LZ4 •Google Snappyよりも圧縮・伸張速度が速いと言 われている圧縮アルゴリズム。 •http://code.google.com/p/lz4/ MongoDB Casual Talks #1 27
  • 28.
    #2 Convert tobinary BSON以外のシリアライズ手法 •Message Pack http://msgpack.org/ MongoDB Casual Talks #1 28
  • 29.
    #2 Convert tobinary 検証条件 •以下の条件で比較 1.何もしない 2.フィールド名の短縮 3. 2 + 特定のフィールドの圧縮 4.複数のフィールド情報をMessagePackでシリ アライズ 5. 4.+圧縮 MongoDB Casual Talks #1 29
  • 30.
    #2 Convert tobinary 検証結果 normal short key short key + msgpack 1500000 1200000 900000 600000 300000 0 none deflate lzo snappy lz4 MongoDB Casual Talks #1 30
  • 31.
    #2 Convert tobinary 検証結果 normal short key 1,2, 4(非圧縮) short key + msgpack 1500000 1200000 900000 600000 300000 0 none deflate lzo snappy lz4 MongoDB Casual Talks #1 31
  • 32.
    #2 Convert tobinary 検証結果 normal short key short key + msgpack 1500000 3,5(圧縮) 1200000 900000 600000 300000 0 none deflate lzo snappy lz4 MongoDB Casual Talks #1 32
  • 33.
    #2 Convert tobinary 検証結果 normal short key short key + msgpack 1500000 1200000 900000 600000 300000 0 none deflate lzo snappy lz4 MongoDB Casual Talks #1 33
  • 34.
    #2 Convert tobinary 検証結果 •「複数のフィールドをMessagePackでシリアライ ズ+圧縮アルゴリズムで圧縮」 の組み合わせで最大2/3の省サイズ化に成功。 •データパターン/データモデルによって傾向は 様々だと思う。 •圧縮・シリアライズのオーバーヘッドに注意。 •独自binary化すると後戻りできないので注意。 MongoDB Casual Talks #1 34
  • 35.
    #3 Integer Encoding 整数値符号化 •たとえば数字の「1」を数バイト使用して表現す るのはもったいない。→整数値符号化 •Variable Byte Code •Simple9 •Simple16 •etc... MongoDB Casual Talks #1 35
  • 36.
    #3 Integer Encoding Variable Byte Code •整数値の値を最小1バイトで表現するための符号 化方式。数値部7bit(0~127)と、数値終端を表すフ ラグ1bitの組み合わせで数値を符号化します。 •https://gist.github.com/3003981 • 0x00-0x7f : 1xxxxxxx • 0x80-0x3fff : 0xxxxxxx 1xxxxxxx • 0x4000-0x1fffff : 0xxxxxxx 0xxxxxxx 1xxxxxxx ※「x」は0、もしくは1 MongoDB Casual Talks #1 36
  • 37.
    #3 Integer Encoding 検証に使用したデータモデル public class NormalModel{ ! @Id ! ObjectId oid; ! @Property(value = "id") ! int id; //もしくはlong } MongoDB Casual Talks #1 37
  • 38.
    #3 Integer Encoding 検証条件 •以下の条件で比較 1. 整数値をinteger(4byte)で保存 2. 整数値をlong(8byte)で保存 3. 整数値をVariable Byte Codeで変換して保存 MongoDB Casual Talks #1 38
  • 39.
    #3 Integer Encoding 検証結果 integer long variable byte code 37000000 36000000 35000000 34000000 33000000 32000000 31000000 30000000 max : 128 max : 16384 max : 2097152 MongoDB Casual Talks #1 39
  • 40.
    #3 Integer Encoding 検証結果 •整数値符号化で保存をしたら、逆にIntegerよりも サイズが大きくなった・・・ •BSONの仕様が関係  int32 : 4bytes  int64 : 8bytes  binary : int32 subtype(byte*) MongoDB Casual Talks #1 40
  • 41.
    Casual Compression MongoDBでのカジュアルなデータ圧縮 •以下について試してみました。 1.フィールド名をできるだけ短くする →◎ 2.特定のデータをbinary形式で保存 →⃝ 3.小さい正整数の整数符号化 →☓ MongoDB Casual Talks #1 41
  • 42.
  • 43.
    HBase HBaseなら圧縮をサポートしてます •HBaseなら.... •データの圧縮に標準で対応 (圧縮したいTableのFamilyごとに指定可能。 アルゴリズムも複数選択可能) •可変長整数値に標準で対応 (VIntWritable / VLongWritable) •大きいデータを扱う場合はHBaseを(ry MongoDB Casual Talks #1 43
  • 44.
  • 45.
    Conclusion まとめにかえて •MongoDBはデータサイズが肥大化しがちです が、アプリケーション側のカジュアルな工夫で多 少はデータサイズの削減ができます。 •用途に応じて、適切な現場でMongoDBを使いま しょう。 •個人的にはRedisが好きです。 MongoDB Casual Talks #1 45
  • 46.
    ご清聴 ありがとうございました MongoDB Casual Talks #1 46