0Pricing
MongoDB Academy · Ders

Bölge Parçalama: Verileri Bölgelere Sabitleme

Öğrenenler, parça anahtarı aralıklarında bölgeler tanımlayıp bunları parçalara atayarak veri yerelliğini ve coğrafi bölümlemeyi etkinleştireceklerdir.

Bölge Parçalama: Verileri Bölgelere Sabitleme, CoddyKit'te ücretsiz bir MongoDB Academy dersidir. Bu, 4 dersinin 4. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, MongoDB Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. MongoDB Academy kursu toplamda 4 dersten oluşur.

Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.

What Is Zone Sharding?

Zone sharding (formerly tag-aware sharding) lets you assign specific shard key ranges to designated groups of shards called zones. Documents whose shard key falls in a zone's range are stored only on the shards belonging to that zone. This enables geographic data residency, hardware-tier affinity, and workload isolation within a single sharded cluster.

Key Use Cases for Zone Sharding

Zone sharding solves several production problems: Data residency — EU GDPR requirements mandate that European user data stays in EU data centers. Tiered storage — keep hot recent data on NVMe shards and cold archive data on slower, cheaper shards. Tenant isolation — in a multi-tenant SaaS application, pin each enterprise customer's data to dedicated shards for noisy-neighbor prevention.

Step 1: Tag Shards With Zone Names

The first step is to assign a zone tag to one or more shards. A shard can belong to multiple zones. Tags are just arbitrary strings you choose. Once tagged, the balancer will only move chunks assigned to a zone tag onto shards that carry that same tag.

// Tag shards with geographic zones
sh.addShardTag('shard-eu-west-1', 'EU')
sh.addShardTag('shard-eu-central-1', 'EU')
sh.addShardTag('shard-us-east-1', 'US')
sh.addShardTag('shard-us-west-2', 'US')

Step 2: Define Shard Key Ranges for Zones

Next, define which shard key ranges map to each zone using sh.addTagRange(). This tells the balancer: 'all documents with a shard key in this range must live on shards tagged with this zone.' Use MinKey and MaxKey as open-ended range boundaries.

// Assign EU shard key range to EU zone
sh.addTagRange(
  'mydb.users',
  { region: 'EU', userId: MinKey },
  { region: 'EU', userId: MaxKey },
  'EU'
)

// Assign US shard key range to US zone
sh.addTagRange(
  'mydb.users',
  { region: 'US', userId: MinKey },
  { region: 'US', userId: MaxKey },
  'US'
)

Shard Key Design for Zone Sharding

Zone sharding requires a compound shard key where the first component is the zone discriminator (e.g., region, tenantId, tier). The second component provides granularity within the zone for further splitting. Example: { region: 1, userId: 1 } — region routes to the right zone; userId allows fine-grained chunk splitting within each zone.

// Shard the collection with a zone-aware compound key
sh.enableSharding('mydb')
sh.shardCollection('mydb.users', { region: 1, userId: 1 })

The Balancer Enforces Zone Membership

Once zones are configured, the balancer continuously enforces zone membership. If a chunk with a key in the EU zone range ends up on a US shard (e.g., after adding a shard or changing zone definitions), the balancer migrates it to an EU shard. This migration happens automatically during the next balancer round.

// Monitor that chunks are on the correct zone shards
use config
db.chunks.find(
  { ns: 'mydb.users', 'min.region': 'EU' },
  { shard: 1, min: 1 }
)
// All returned chunks should be on EU-tagged shards

Tiered Storage: Hot and Cold Zones

Zone sharding enables tiered storage: assign recent data (e.g., current month) to a 'hot' zone on fast NVMe-backed shards, and older data to a 'cold' zone on slower HDD-backed shards. As data ages, you can update the zone range assignments so the balancer gradually migrates aging chunks from the hot zone to the cold zone.

// Tiered storage by age
sh.addShardTag('shard01', 'HOT')
sh.addShardTag('shard02', 'COLD')

sh.addTagRange('mydb.events',
  { year: 2025, _id: MinKey },
  { year: 2025, _id: MaxKey },
  'HOT'
)
sh.addTagRange('mydb.events',
  { year: 2024, _id: MinKey },
  { year: 2024, _id: MaxKey },
  'COLD'
)

Removing and Updating Zone Ranges

Zone range assignments can be updated to move data as business needs change. Remove an existing range with sh.removeTagRange() and add a new one. The balancer will then migrate the affected chunks to match the new assignment. This is how you roll data from a 'hot' zone to a 'cold' zone monthly.

// Remove an old zone range
sh.removeTagRange(
  'mydb.events',
  { year: 2024, _id: MinKey },
  { year: 2024, _id: MaxKey },
  'HOT'
)

// Add it to COLD zone instead
sh.addTagRange('mydb.events',
  { year: 2024, _id: MinKey },
  { year: 2024, _id: MaxKey },
  'COLD'
)

Querying With Zone-Aware Shard Keys

When using a compound zone-aware shard key like { region: 1, userId: 1 }, queries that include the region field are automatically routed to the correct zone's shards. This gives both data residency compliance and query performance — EU queries never touch US shards.

// Query from a EU service — targeted to EU shards only
const user = await db.users.findOne({ region: 'EU', userId: 'u789' })
// mongos routes this to EU-tagged shards only — data never
// leaves EU-region shards even for the query itself

Limitations and Caveats

Zone sharding has some important limitations: 1) It only works with ranged sharding (not hashed sharding with a single field). 2) Documents not covered by any zone range can be balanced to any shard — ensure your ranges cover all possible values using MinKey to MaxKey. 3) Zone migration can take time for large collections; plan migrations during low-traffic windows.

// Ensure all documents are covered by a zone
// Uncovered ranges can land on any shard
sh.addTagRange('mydb.users',
  { region: MinKey, userId: MinKey },  // catch-all
  { region: 'EU', userId: MinKey },
  'US'  // everything before EU goes to US zone
)

Inspecting Zone Configuration

You can inspect the current zone configuration at any time. The zone tags are stored in the config server's config.shards and config.tags collections. sh.status() prints zone information along with chunk distribution, making it easy to verify that the balancer has enforced zone membership correctly.

// View all zone ranges configured for a collection
use config
db.tags.find({ ns: 'mydb.users' })

// Full cluster and zone overview
sh.status()
// Shows: zones, shards in each zone, chunks per shard

Quick Check

Test your understanding of MongoDB & NoSQL Databases concepts from this lesson.

Lesson Recap

In this lesson you learned: zone sharding pins shard key ranges to tagged shards for data residency and workload isolation, a compound shard key with a zone discriminator as the first field is required, and the balancer automatically enforces zone membership and migrates chunks when zone assignments change. Next up we explore performance tuning and the database profiler.

Sıkça Sorulan Sorular

“Bölge Parçalama: Verileri Bölgelere Sabitleme” dersi ücretsiz mi?

Evet — “Bölge Parçalama: Verileri Bölgelere Sabitleme” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve MongoDB Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. MongoDB Academy kursu toplamda 4 dersten oluşur.

“Bölge Parçalama: Verileri Bölgelere Sabitleme” dersinde ne öğreneceğim?

Öğrenenler, parça anahtarı aralıklarında bölgeler tanımlayıp bunları parçalara atayarak veri yerelliğini ve coğrafi bölümlemeyi etkinleştireceklerdir. MongoDB Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.

MongoDB Academy öğrenmeye başlamak için deneyim gerekli mi?

Önceden deneyim gerekmez. CoddyKit'te MongoDB Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 4. dersidir.

“Bölge Parçalama: Verileri Bölgelere Sabitleme” dersi ne kadar sürer?

Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.

Bu MongoDB Academy dersinde kod yazıp çalıştırabilir miyim?

Evet. Her MongoDB Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.

Bu kursun tüm dersleri

  1. Parçalama Kavramları: Öbekler, Dengeleyici ve Parça Anahtarları
  2. Parça Anahtarı Seçme: Kardinalite, Sıklık, Monotonluk
  3. Aralıklı ve Özetlenmiş Parçalama Stratejileri
  4. Bölge Parçalama: Verileri Bölgelere Sabitleme
← MongoDB Academy Sayfasına Dön