mongodb分區原理

來源:互聯網
上載者:User

標籤:mongodb分區原理 什麼是mongodb分區

分區介紹

分區是使用多個機器儲存資料的方法,MongoDB使用分區以支援巨大的資料存放區量與對資料操作.

分區的目的

高資料量和輸送量的資料庫應用會對單機的效能造成較大壓力,大的查詢量會將單機的CPU耗盡,大的資料量對單機的儲存壓力較大,最終會耗盡系統的記憶體而將壓力轉移到磁碟IO上.

為瞭解決這些問題,有兩個基本的方法: 縱向擴充分區 .

 

 650) this.width=650;" src="http://docs.mongoing.com/manual/_images/sharded-collection.png" alt="sharded-collection.png" />

分區為應對高輸送量與大資料量提供了方法.

  • 使用分區減少了每個分區需要處理的請求數,因此,通過 水平擴充 ,叢集可以提高自己的儲存容量和輸送量.

    舉例來說,當插入一條資料時,應用只需要訪問儲存這條資料的分區.

  • 使用分區減少了每個分區儲存的資料.

    舉例來說,如果一個資料庫有1TB資料,並有4個分區,則每個分區只需要儲存256GB資料,如果資料庫有40個分區,則每個分區只需要儲存25GB資料.

 

MongoDB的分區

MongoDB通過配置 叢集 支援分區.

650) this.width=650;" alt="Diagram of a sample sharded cluster for production purposes. Contains exactly 3 config servers, 2 or more ``mongos`` query routers, and at least 2 shards. The shards are replica sets." src="http://docs.mongoing.com/manual/_images/sharded-cluster-production-architecture.png" />

Diagram of a sample sharded cluster for production purposes. Contains exactly 3 config servers, 2 or more mongos query routers, and at least 2 shards. The shards are replica sets.

叢集擁有以下組件: 分區, 分發路由,:term:設定管理員 <config server>.

Shards store the data. To provide high availability and data consistency, in a production sharded cluster, each shard is a replica set [1]. For more information on replica sets, see Replica Sets.

Query Routers, or mongos instances, interface with client applications and direct operations to the appropriate shard or shards. The query router processes and targets operations to shards and then returns results to the clients. A sharded cluster can contain more than one query router to divide the client request load. A client sends requests to one query router. Most sharded cluster have many query routers.

Config servers store the cluster’s metadata. This data contains a mapping of the cluster’s data set to the shards. The query router uses this metadata to target operations to specific shards. Production sharded clusters have exactly 3 config servers.

[1]

在測試與開發環境下,每個 分區 可以是單獨的 mongod 而不用必須是複製集.在生產環境中 必須 部署3台設定管理員.

資料分區

MongoDB中資料的分區是以集合為基本單位的,集合中的資料通過 片鍵 被分成多部分.

片鍵

對集合進行分區時,你需要選擇一個 片鍵 , shard key 是每條記錄都必須包含的,且建立了索引的單個欄位或複合欄位,MongoDB按照片鍵將資料劃分到不同的 資料區塊 中,並將 資料區塊 均衡地分布到所有分區中.為了按照片鍵劃分資料區塊,MongoDB使用 基於範圍的分區方式 或者 基於雜湊的分區方式 ,參見 片鍵 獲得更多資訊.

以範圍為基礎的分區

對於 基於範圍的分區 ,MongoDB按照片鍵的範圍把資料分成不同部分.假設有一個數位片鍵:想象一個從負無窮到正無窮的直線,每一個片鍵的值都在直線上畫了一個點.MongoDB把這條直線劃分為更短的不重疊的片段,並稱之為 資料區塊 ,每個資料區塊包含了片鍵在一定範圍內的資料.

在使用片鍵做範圍劃分的系統中,擁有”相近”片鍵的文檔很可能儲存在同一個資料區塊中,因此也會儲存在同一個分區中.

650) this.width=650;" alt="Diagram of the shard key value space segmented into smaller ranges or chunks." src="http://docs.mongoing.com/manual/_images/sharding-range-based.png" />

Diagram of the shard key value space segmented into smaller ranges or chunks.

基於雜湊的分區

對於 基於雜湊的分區 ,MongoDB計算一個欄位的雜湊值,並用這個雜湊值來建立資料區塊.

在使用基於雜湊分區的系統中,擁有”相近”片鍵的文檔 很可能不會 儲存在同一個資料區塊中,因此資料的分離性更好一些.

650) this.width=650;" alt="Diagram of the hashed based segmentation." src="http://docs.mongoing.com/manual/_images/sharding-hash-based.png" />

Diagram of the hashed based segmentation.

基於範圍的分區方式與基於雜湊的分區方式效能對比

基於範圍的分區方式提供了更高效的範圍查詢,給定一個片鍵的範圍,分發路由可以很簡單地確定哪個資料區塊儲存了請求需要的資料,並將請求轉寄到相應的分區中.

不過,基於範圍的分區會導致資料在不同分區上的不均衡,有時候,帶來的消極作用會大於查詢效能的積極作用.比如,如果片鍵所在的欄位是線性增長的,一定時間內的所有請求都會落到某個固定的資料區塊中,最終導致分布在同一個分區中.在這種情況下,一小部分分區承載了叢集大部分的資料,系統並不能很好地進行擴充.

與此相比,基於雜湊的分區方式以範圍查詢效能的損失為代價,保證了叢集中資料的均衡.雜湊值的隨機性使資料隨機分布在每個資料區塊中,因此也隨機分布在不同分區中.但是也正由於隨機性,一個範圍查詢很難確定應該請求哪些分區,通常為了返回需要的結果,需要請求所有分區.

使用標記自訂叢集中資料的分布

MongoDB允許管理員使用 標記 直接決定叢集的均衡策略.管理員使用標記與片鍵的範圍做綁定,並將標記與分區直接綁定,之後,均衡器會將滿足標記的資料直接分發到與之綁定的分區上,並且確保之後滿足標記的資料一直儲存在相應的分區上.

標記是控制均衡器行為和資料區塊分布的首要條件,一般來講,在擁有多個資料中心時,才會使用標記自訂叢集中資料區塊的分布,以提高不同地區之間資料訪問的效率.

參見 受標記作用的分區 獲得更多資訊.

資料均衡的維護

新資料的加入或者新分區的加入可能會導致叢集中資料的不均衡,即表現為有些分區儲存的資料區塊數目顯著地大於其他分區儲存的資料區塊數.

MongoBD使用兩個過程維護叢集中資料的均衡:分裂和均衡器.

分裂

分裂是防止某個資料區塊過大而進行的一個背景工作.當一個資料區塊的大小超過 :ref:`設定的資料區塊大小 <sharding-chunk-size>`時,MongoDB會將其一分為二,插入與更新觸發分裂過程.分裂改變了元資訊,但是效率很高.進行分裂時,MongoDB 不會 遷移任何資料,對叢集效能也沒有影響.

650) this.width=650;" alt="Diagram of a shard with a chunk that exceeds the default chunk size of 64 MB and triggers a split of the chunk into two chunks." src="http://docs.mongoing.com/manual/_images/sharding-splitting.png" />

Diagram of a shard with a chunk that exceeds the default chunk size of 64 MB and triggers a split of the chunk into two chunks.

均衡

The balancer is a background process that manages chunk migrations. The balancer runs in all of the query routers in a cluster.

當叢集中資料的不均衡發生時,均衡器會將資料區塊從資料區塊數目最多的分區遷移到資料區塊最少的分區上,舉例來講:如果集合 users 在 分區1 上有100個資料區塊,在 分區2 上有50個資料區塊,均衡器會將資料區塊從 分區1 一直向 分區2 遷移,一直到資料均衡為止.

分區管理在後台管理從 源分區目標分區資料區塊遷移 ,在遷移過程中, 目標分區 首先會接收源分區在遷移資料區塊上的所有資料,之後,目標分區應用在上一遷移步驟之間發生在源分區上的遷移資料區塊的更改,最後,儲存在 設定管理員 上的元資訊被更新.

如果遷移中發生錯誤,源分區上的資料不會被修改,遷移會停止.在遷移成功 結束 之後MongoDB才會在源分區上將資料刪除.

650) this.width=650;" alt="Diagram of a collection distributed across three shards. For this collection, the difference in the number of chunks between the shards reaches the *migration thresholds* (in this case, 2) and triggers migration." src="http://docs.mongoing.com/manual/_images/sharding-migrating.png" />

Diagram of a collection distributed across three shards. For this collection, the difference in the number of chunks between the shards reaches the migration thresholds (in this case, 2) and triggers migration.

在叢集中增加或者刪除分區

在叢集中增加分區時,由於新的分區上並沒有資料區塊,會造成資料的不均衡.此時MongoDB會立即開始向新分區遷移資料,叢集達到資料均衡的狀態需要花費一些時間.

當刪除一個分區時,均衡器需要將被刪除的分區上的資料全部遷移到其他分區上,在全部遷移結束且元資訊更新完畢之後,你可以安全地將這個分區移除.

 

 

叢集組件

Sharded clusters implement sharding. A sharded cluster consists of the following components:

  • 分區

  • 分區是儲存了一個集合部分資料的MongoDB執行個體,每個分區是單獨的 mongod 或者是 replica set .在生產環境中,所有的分區都應該是複製集.參見 分區 獲得更多資訊.

  • 設定管理員

  • 每個:ref:設定管理員 <sharding-config-server>`都是儲存了叢集元資訊的 :program:`mongod.元資訊儲存了 資料區塊 對分區的映射,參見 設定管理員.獲得更多資訊.

  • 分發路由

  • 每個路由都是 mongos ,它將讀寫請求分發到分區中.應用並不直接存取分區.650) this.width=650;" alt="Diagram of a sharded cluster." src="http://docs.mongoing.com/manual/_images/sharded-cluster.png" />

Diagram of a sharded cluster.

MongoDB中分區的基本單位是集合,對每個開啟了分區的集合,都可以設定一個 shard key .

Further Reading
  • 分區

  • A shard is a mongod instance that holds a part of the sharded collection’s data.

  • 設定管理員

  • Config servers hold the metadata about the cluster, such as the shard location of the data.

 

 

mongodb分區原理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.