MongoDB:
Scaling write performance
                            Junegunn Choi
MongoDB

• Document    data store

 • JSON-like   document

• Secondary   indexes

• Automatic   failover

• Automatic   sharding
First impression:
                     Easy

• Easy   installation

• Easy   data model

• No   prior schema design

• Native   support for secondary indexes
Second thought:
                Not so easy

• No   SQL

• Coping    with massive data growth

• Setting   up and operating sharded cluster

• Scaling   write performance
Today we’ll talk about
 insert performance
Insert throughput
  on a replica set
Steady 5k inserts/sec




              * 1kB record. ObjectId as PK
              * WriteConcern: Journal sync on Majority
Insert throughput
    on a replica set
with a secondary index
Mongodb - Scaling write performance
Culprit:
               B+Tree index

• Good    at sequential insert

 • e.g. ObjectId, Sequence       #, Timestamp

• Poor   at random insert

 • Indexes   on randomly-distributed data
Sequential vs. Random insert
  1                                           55
  2                                           75
  3                                           78
  4                                            1
  5                                           99
  6                                           36
  7                                           80
  8                                           91
  9                                           52
 10          B+Tree                           63      B+Tree
 11                                           56
 12                                           33
                            working set               working set




 Sequential insert ➔ Small working set    Random insert ➔ Large working set
   ➔ Fits in RAM ➔ Sequential I/O         ➔ Cannot fit in RAM ➔ Random I/O
         (bandwidth-bound)                          (IOPS-bound)
So, what do we do now?
1. Partitioning

                        Aug 2012   Sep 2012     Oct 2012




      B+Tree

                                              fits in memory

does not fit in memory
1. Partitioning

• MongoDB       doesn’t support partitioning

• Partitioning   at application-level

• e.g. Daily   log collection

  • logs_20121012
Switch collection every hour
2. Better H/W

• More   RAM

• More   IOPS

 • RAID   striping

 • SSD

 • AWS    Provisioned IOPS (1k ~ 10k)
Mongodb - Scaling write performance
3. More H/W: Sharding
• Automatic   partitioning across nodes

   SHARD1             SHARD2              SHARD3




                      mongos router
3 shards (3x3)
3 shards (3x3)
on RAID 1+0
There’s no free lunch
•   Manual partitioning

    •   Incidental complexity

•   Better H/W

    •   $

•   Sharding

    •   $$

    •   Operational complexity
“Do you really need that index?”
Scaling insert performance
       with sharding
=
Choosing the right shard key
Shard key example:
          year_of_birth
                                 64MB chunk

    ~ 1950    1971 ~ 1990     1951 ~ 1970

1991 ~ 2005   2006 ~ 2010

                2010 ~ ∞

  USERS         USERS           USERS
 SHARD1        SHARD2          SHARD3



              mongos router
5k inserts/sec w/o sharding
Sequential key


• ObjectId   as shard key

• Sequence   #

• Timestamp
Worse throughput with 3x H/W.
Sequential key

                                               1000 ~ 2000
• All   inserts into one chunk
                                               5000 ~ 7500
• Cannot    scale insert performance
                                                  9000 ~ ∞
• Chunk    migration overhead                     USERS
                                                SHARD-x

                                 9001, 9002, 9003, 9004, ...
Sequential key
Hash key


• e.g. SHA1(_id)   = 9f2feb0f1ef425b292f2f94 ...

• Distributes   inserts evenly across all chunks
Mongodb - Scaling write performance
Hash key


• Performance   drops as collection grows

 • Why?   Mandatory index on shard key

     • B+Tree   problem again!
Sequential key
  Hash key
Sequential + hash key
• Coarse-grained    sequential prefix

• e.g. Year-month   + hash value

  • 201210_24c3a5b9

                                          B+Tree



                               201208_*   201209_*   201210_*
But what if...


                         B+Tree




                             large working set

   201208_*   201209_*            201210_*
Sequential + hash key

• Can   you predict data growth rate?

• Balancer   not clever enough

  • Only   considers # of chunks

  • Migration   slow during heavy-writes
Sequential key
     Hash key
Sequential + hash key
Low-cardinality hash key
• Small   portion of hash value                   Shard key range: A ~ D


  • e.g. A~Z, 00~FF

• Alleviates   B+Tree problem
                                               Local
  • Sequential   access on fixed #             B+Tree
   of parts

    • Cardinality   / # of shards
                                    A A   A   B     B   B   C C C
Mongodb - Scaling write performance
Low-cardinality hash key

• Limits   the # of possible chunks

  • e.g. 00   ~ FF ➔ 256 chunks

  • Chunk     grows past 64MB

    • Balancing   becomes difficult
Sequential key
       Hash key
 Sequential + hash key
Low-cardinality hash key
Low-cardinality hash prefix
      + sequential part
                                         Shard key range: A000 ~ C999


• e.g. Short   hash prefix + timestamp

• Nice   index access pattern              Local
                                          B+Tree
• Unlimited    number of chunks


                                A000 A123 B000 B123 C000 C123
Finally, 2x throughput
Lessons learned
• Know     the performance impact of secondary index

• Choose     the right shard key

• Test   with large data sets

• Linear   scalability is hard

  • If   you really need it, consider HBase or Cassandra

  • SSD
Thank you. Questions?

         유응섭 rspeed@daumcorp.com
         최준건 gunn@daumcorp.com

More Related Content

PDF
Oracle Real Application Clusters 19c- Best Practices and Internals- EMEA Tour...
DOCX
Rac questions
PDF
MySQL GTID 시작하기
PPTX
ProxySQL & PXC(Query routing and Failover Test)
PDF
MySQL 8.0 EXPLAIN ANALYZE
PDF
Oracle advanced queuing
PDF
Zabbix Performance Tuning
PDF
[오픈소스컨설팅] 스카우터 사용자 가이드 2020
Oracle Real Application Clusters 19c- Best Practices and Internals- EMEA Tour...
Rac questions
MySQL GTID 시작하기
ProxySQL & PXC(Query routing and Failover Test)
MySQL 8.0 EXPLAIN ANALYZE
Oracle advanced queuing
Zabbix Performance Tuning
[오픈소스컨설팅] 스카우터 사용자 가이드 2020

What's hot (20)

PDF
SQLd360
PDF
Cost-based Query Optimization in Apache Phoenix using Apache Calcite
PPTX
Apache NiFi- MiNiFi meetup Slides
PDF
Intro to Airflow: Goodbye Cron, Welcome scheduled workflow management
PPTX
Kafka 101
PDF
DocValues aka. Column Stride Fields in Lucene 4.0 - By Willnauer Simon
PDF
Linux tuning to improve PostgreSQL performance
PDF
Oracle db performance tuning
PDF
Apache kafka 모니터링을 위한 Metrics 이해 및 최적화 방안
PPTX
Understanding SQL Trace, TKPROF and Execution Plan for beginners
PDF
Federated Engine 실무적용사례
PDF
新機能によるデータベースシステムの改善ポイント
PDF
[오픈소스컨설팅]Scouter 설치 및 사용가이드(JBoss)
PPTX
Elastic stack Presentation
PDF
Tanel Poder - Scripts and Tools short
PDF
Oracle AHF Insights 23c: Deeper Diagnostic Insights for your Oracle Database
PDF
Row Pattern Matching in SQL:2016
PPTX
SQL Tuning 101
PPT
01 oracle architecture
PPTX
[135] 오픈소스 데이터베이스, 은행 서비스에 첫발을 내밀다.
SQLd360
Cost-based Query Optimization in Apache Phoenix using Apache Calcite
Apache NiFi- MiNiFi meetup Slides
Intro to Airflow: Goodbye Cron, Welcome scheduled workflow management
Kafka 101
DocValues aka. Column Stride Fields in Lucene 4.0 - By Willnauer Simon
Linux tuning to improve PostgreSQL performance
Oracle db performance tuning
Apache kafka 모니터링을 위한 Metrics 이해 및 최적화 방안
Understanding SQL Trace, TKPROF and Execution Plan for beginners
Federated Engine 실무적용사례
新機能によるデータベースシステムの改善ポイント
[오픈소스컨설팅]Scouter 설치 및 사용가이드(JBoss)
Elastic stack Presentation
Tanel Poder - Scripts and Tools short
Oracle AHF Insights 23c: Deeper Diagnostic Insights for your Oracle Database
Row Pattern Matching in SQL:2016
SQL Tuning 101
01 oracle architecture
[135] 오픈소스 데이터베이스, 은행 서비스에 첫발을 내밀다.
Ad

Similar to Mongodb - Scaling write performance (20)

PDF
MongoDB: Scaling write performance | Devon 2012
PPTX
Scaling with MongoDB
PDF
MongoDB and Fractal Tree Indexes
PDF
20121024 mongodb-boston (1)
KEY
Scaling with MongoDB
PPTX
Hellenic MongoDB user group - Introduction to sharding
KEY
2012 phoenix mug
KEY
Scaling MongoDB (Mongo Austin)
PDF
A Guide to the Post Relational Revolution
PDF
SQL? NoSQL? NewSQL?!? What's a Java developer to do? - PhillyETE 2012
PDF
Hbase schema design and sizing apache-con europe - nov 2012
PDF
Optimizing MongoDB: Lessons Learned at Localytics
PDF
HBase Advanced - Lars George
PDF
Cassandra on Castle
KEY
2011 mongo sf-schemadesign
KEY
2011 mongo sf-scaling
PPTX
Introduce Apache Cassandra - JavaTwo Taiwan, 2012
PPT
2011 mongo FR - scaling with mongodb
PPTX
Hadoop World 2011: Advanced HBase Schema Design
PDF
Шардинг в MongoDB, Henrik Ingo (MongoDB)
MongoDB: Scaling write performance | Devon 2012
Scaling with MongoDB
MongoDB and Fractal Tree Indexes
20121024 mongodb-boston (1)
Scaling with MongoDB
Hellenic MongoDB user group - Introduction to sharding
2012 phoenix mug
Scaling MongoDB (Mongo Austin)
A Guide to the Post Relational Revolution
SQL? NoSQL? NewSQL?!? What's a Java developer to do? - PhillyETE 2012
Hbase schema design and sizing apache-con europe - nov 2012
Optimizing MongoDB: Lessons Learned at Localytics
HBase Advanced - Lars George
Cassandra on Castle
2011 mongo sf-schemadesign
2011 mongo sf-scaling
Introduce Apache Cassandra - JavaTwo Taiwan, 2012
2011 mongo FR - scaling with mongodb
Hadoop World 2011: Advanced HBase Schema Design
Шардинг в MongoDB, Henrik Ingo (MongoDB)
Ad

More from Daum DNA (20)

PDF
Daum의 개방형 기술 전략 및 자바 기술 로드맵(2007)
PDF
Daum OAuth 2.0
PDF
Daum 음성인식 API (김한샘)
PDF
Daum 검색/지도 API (이정주)
PDF
오픈 API 활용방법(Daum 사례 중심, 윤석찬)
PDF
Daum 티스토리 API (천정환)
PDF
Daum 로그인 API (함태윤)
PDF
FT직군의 현재와 미래 - 홍윤표
PDF
웹접근성과 장애인 차별 금지법 - 장성민
PDF
반응형 웹 디자인은 만능인가? - 신현석
PDF
Daum devday 13 [bap]
PPTX
Daum DevDay 13-힐링이 필요해
PPT
Daum DevDay 13 - 마음의 소리
PPTX
Daum DevDay 13 - OpenBrace
PPTX
Daum DevDay 13 - Ogangjang
PDF
Daum DevDay 13 - Mook
PDF
Daum DevDay 13 - Moonlight
PPTX
Daum DevDay 13 - In-N-Out
PPTX
Daum DevDay 13 - i-DF
PDF
Daum 키노트 | Devon 2012
Daum의 개방형 기술 전략 및 자바 기술 로드맵(2007)
Daum OAuth 2.0
Daum 음성인식 API (김한샘)
Daum 검색/지도 API (이정주)
오픈 API 활용방법(Daum 사례 중심, 윤석찬)
Daum 티스토리 API (천정환)
Daum 로그인 API (함태윤)
FT직군의 현재와 미래 - 홍윤표
웹접근성과 장애인 차별 금지법 - 장성민
반응형 웹 디자인은 만능인가? - 신현석
Daum devday 13 [bap]
Daum DevDay 13-힐링이 필요해
Daum DevDay 13 - 마음의 소리
Daum DevDay 13 - OpenBrace
Daum DevDay 13 - Ogangjang
Daum DevDay 13 - Mook
Daum DevDay 13 - Moonlight
Daum DevDay 13 - In-N-Out
Daum DevDay 13 - i-DF
Daum 키노트 | Devon 2012

Mongodb - Scaling write performance