Optimizing MongoDB for IoT data streams.
Instruction: Outline a strategy for optimizing a MongoDB database to handle high-volume, high-velocity data streams from IoT devices.
Updated
Official answer
Define event identity, device identity, event time, ingest time, units and schema version. Device clocks can be wrong and networks can resend or reorder data, so distinguish late events, duplicates and missing intervals before optimizing storage.
Use an authenticated, bounded ingestion path with buffering/backpressure and a documented retry policy. Validate payload size/types and preserve rejected records for controlled investigation. Avoid acknowledging durable acceptance before the chosen durability condition is met.
Evaluate a time-series collection with BSON Date timeField and stable device metadata. Check version-specific restrictions, including unsupported unique indexes; do not promise exactly-once ingestion by adding a unique index that the collection type cannot provide. If strict deduplication is required, design a supported durable ingestion ledger or ordinary-collection model and test its failure boundary.
Use device/time query patterns, retention and summaries that preserve count/sum for weighted aggregates. Handle late corrections explicitly. Choose a supported shard key from stable metadata and measured skew; a time-only growing key can create hotspots.
Load-test bursts, retries, out-of-order input and slow consumers. Track ingestion lag, rejected/duplicate rates, completeness, storage growth and query latency. Keep raw-data retention and summary semantics clear so performance improvements do not change the measurement being reported.