MongoDB Academy · บทเรียน

MongoDB เทียบกับ Cassandra: การเขียนข้อมูลในระดับทั่วโลก

ผู้เรียนจะเปรียบเทียบรูปแบบความสอดคล้องของชุดแบบจำลองของ MongoDB กับความสอดคล้องในที่สุดที่ปรับระดับได้และการจำลองแบบไร้ผู้นำของ Cassandra สำหรับงาน IoT ที่มีการเขียนข้อมูลจำนวนมาก

บทเรียน 2 จาก 413 ขั้นตอน

MongoDB เทียบกับ Cassandra: การเขียนข้อมูลในระดับทั่วโลก เป็นบทเรียน MongoDB Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน MongoDB Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส MongoDB Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Two Approaches to Distributed Data

MongoDB and Apache Cassandra both handle distributed data at scale, but with fundamentally different architectures. MongoDB uses a leader-follower (primary-secondary) model where writes go to a single primary per replica set. Cassandra uses a leaderless (peer-to-peer) model where any node can accept any write. This architectural difference drives every performance, consistency, and operational tradeoff between the two.

Cassandra's Leaderless Architecture

In Cassandra, all nodes are equal peers in a ring topology. A write can be sent to any node (the coordinator), which forwards it to the N replica nodes responsible for that row's partition key. The number of nodes that must acknowledge the write is configured by the consistency level (e.g., ONE, QUORUM, ALL). This architecture eliminates the single-primary bottleneck and enables truly multi-master, multi-region writes — every data center can accept writes simultaneously.

Write Throughput: Cassandra's Advantage

Cassandra is optimised for extremely high write throughput. Writes are appended to a commit log and a fast in-memory structure (Memtable) before being flushed to disk (SSTables) in bulk. This append-only approach means writes never conflict on disk and throughput scales linearly with node count. IoT platforms ingesting millions of measurements per second, event logging systems, and time-series workloads with write rates that overwhelm a single MongoDB primary are prime Cassandra use cases.

Tunable Consistency in Cassandra

Cassandra's consistency level is tunable per query. ONE means one replica acknowledges (fastest, weakest consistency). QUORUM means a majority of replicas acknowledge (balances latency and consistency). ALL means all replicas acknowledge (slowest, strongest consistency). The key formula: if read consistency + write consistency > replication factor, you get strong consistency. This flexibility allows Cassandra to serve different workloads differently within the same cluster.

-- Cassandra CQL: tunable consistency per query
CONSISTENCY QUORUM;

INSERT INTO iot_events (device_id, event_time, temperature)
VALUES ('sensor-42', toTimestamp(now()), 23.5);

-- For lower latency (weaker consistency)
CONSISTENCY ONE;

SELECT * FROM iot_events WHERE device_id = 'sensor-42'
  AND event_time >= '2024-06-01 00:00:00'
  LIMIT 100;

Query Model: Schema First in Cassandra

Cassandra's data model is fundamentally query-driven. You design tables to answer specific queries efficiently — there is no ad-hoc query engine like MongoDB's. Tables must be partitioned by a partition key (which determines which node stores the row), and rows within a partition are sorted by a clustering key. Secondary indexes exist but are far less capable than MongoDB's. Complex queries (joins, aggregations, multi-field filters) that MongoDB handles with the aggregation pipeline are not possible in standard CQL.

-- Cassandra CQL: table designed around a specific query
CREATE TABLE sensor_readings_by_device (
  device_id TEXT,
  event_time TIMESTAMP,
  temperature DOUBLE,
  humidity DOUBLE,
  PRIMARY KEY (device_id, event_time)  -- partition by device, cluster by time
) WITH CLUSTERING ORDER BY (event_time DESC);

-- This query is fast (uses partition and clustering key)
SELECT * FROM sensor_readings_by_device
  WHERE device_id = 'sensor-42'
  AND event_time >= '2024-06-01'
  LIMIT 100;

MongoDB's Query Advantage

MongoDB's aggregation pipeline and rich query operators allow ad-hoc queries across any field. Need to find all users in Istanbul who purchased a specific product in the last 30 days? A MongoDB query with the right compound index answers this directly. In Cassandra, you would need a pre-designed table for this specific query, or denormalise data into multiple tables, or use Spark for analytical queries. MongoDB is far more flexible for evolving query requirements.

// MongoDB: ad-hoc multi-field query — easy
db.orders.find({
  'customer.city': 'Istanbul',
  'items.sku': 'WGT-001',
  createdAt: { $gte: new Date(Date.now() - 30 * 86400000) }
}).sort({ createdAt: -1 })

// Cassandra: would need a pre-designed table for this exact query
// or resort to ALLOW FILTERING (very slow full-table scan)

Multi-Region Active-Active: Cassandra's Killer Feature

Cassandra's leaderless, multi-datacenter replication allows active-active deployments: all regions accept writes simultaneously. A user in New York writes to the US datacenter; the same user's data replicates asynchronously to Europe and Asia. MongoDB supports multi-region through replica set read preferences and global clusters (Atlas), but writes must still route to a single primary region. For applications requiring zero-latency writes from every region, Cassandra has a structural advantage.

Consistency Model Differences

MongoDB with w: majority provides strong consistency — once a write is acknowledged, all subsequent reads return the new value. Cassandra's default configuration is eventual consistency — a write acknowledged with ONE may not immediately be visible on reads from other replicas. Applications must tolerate this or configure QUORUM reads/writes to achieve strong consistency at the cost of higher latency. This affects application complexity significantly.

Operational Complexity

Both systems require operational expertise, but in different areas. MongoDB's replica set architecture is well-understood, and Atlas automates nearly all ops. Cassandra's ring topology requires careful capacity planning, token management, compaction monitoring, and tombstone management. Deletes in Cassandra produce tombstones that can accumulate and degrade read performance over time. MongoDB's delete model is simpler operationally. For small to mid-size teams, MongoDB's operational overhead is generally lower.

IoT and Time-Series: Cassandra vs MongoDB

Both databases are used for IoT and time-series workloads, but with different approaches. Cassandra's time-series partitioning (partition by device, cluster by time) delivers extremely high write throughput and efficient time-range scans per device. MongoDB's native time series collections (added in 5.0) close much of the gap with automatic bucketing and columnar storage. For write rates in the millions per second across thousands of devices, Cassandra still has the edge. For workloads under this scale with richer query needs, MongoDB time series is often more practical.

Decision Framework: MongoDB vs Cassandra

Use Cassandra when: write throughput is in the millions per second; multi-region active-active writes are required; the access pattern is highly predictable (table-per-query); and data retention TTLs are simple. Use MongoDB when: query patterns evolve frequently; complex aggregations and joins are needed; document flexibility is valued; team size is small to medium; or you need full ACID transactions across documents.

Quick Check

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

Lesson Recap

In this lesson you learned: Cassandra's leaderless architecture enables massive write throughput and true active-active multi-region writes that MongoDB's primary-secondary model cannot match, Cassandra's query model is schema/table-first while MongoDB supports rich ad-hoc queries, and the decision between them comes down to write scale requirements, query flexibility needs, and team operational capacity. Next up we compare MongoDB with DynamoDB.

เริ่มต้นได้ฟรี

เรียนรู้ JavaScript ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
30
บทเรียน
120

คำถามที่พบบ่อย

บทเรียน “MongoDB เทียบกับ Cassandra: การเขียนข้อมูลในระดับทั่วโลก” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “MongoDB เทียบกับ Cassandra: การเขียนข้อมูลในระดับทั่วโลก” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส MongoDB Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส MongoDB Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “MongoDB เทียบกับ Cassandra: การเขียนข้อมูลในระดับทั่วโลก”

ผู้เรียนจะเปรียบเทียบรูปแบบความสอดคล้องของชุดแบบจำลองของ MongoDB กับความสอดคล้องในที่สุดที่ปรับระดับได้และการจำลองแบบไร้ผู้นำของ Cassandra สำหรับงาน IoT ที่มีการเขียนข้อมูลจำนวนมาก คุณปฏิบัติ MongoDB Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน MongoDB Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน MongoDB Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “MongoDB เทียบกับ Cassandra: การเขียนข้อมูลในระดับทั่วโลก” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน MongoDB Academy นี้ได้ไหม

ได้ บทเรียน MongoDB Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. MongoDB เทียบกับ Redis: เอกสารกับแคชคีย์-ค่า
  2. MongoDB เทียบกับ Cassandra: การเขียนข้อมูลในระดับทั่วโลก
  3. MongoDB เทียบกับ DynamoDB: ข้อแลกเปลี่ยนของระบบคลาวด์เนทีฟ
  4. เมื่อใดควรใช้ฐานข้อมูลกราฟอย่าง Neo4j
← กลับไปที่ MongoDB Academy