Running MongoDB in Production Tim Vaillancourt Sr Technical - PowerPoint PPT Presentation
Running MongoDB in Production Tim Vaillancourt Sr Technical Operations Architect, Percona `whoami` { name: tim, lastname: vaillancourt, employer: percona, techs: [ mongodb, mysql, cassandra,
Security: External Authentication β LDAP Authentication β Supported in PSMDB and MongoDB Enterprise β The following components are necessary for external authentication to work β LDAP Server: Remotely stores all user credentials (i.e. user name and associated password). β SASL Daemon: Used as a MongoDB server-local proxy for the remote LDAP service. β SASL Library: Used by the MongoDB client and server to create authentication mechanism-specific data. β Creating a User: db.getSiblingDB("$external").createUser( {user : christian, roles: [{role: "read", db: "test"} ]} ); β Authenticating as a User: db.getSiblingDB("$external").auth({ mechanism:"PLAIN", user:"christian", pwd:"secret", digestPassword:false}) β Other auth methods possible with MongoDB Enterprise 22
Security: SSL Connections and Auth β SSL / TLS Connections β Supported since MongoDB 2.6x β May need to complile-in yourself on older binaries β Supported 100% in Percona Server for MongoDB β Minimum of 128-bit key length for security β Relaxed and strict (requireSSL) modes β System (default) or Custom Certificate Authorities are accepted β SSL Client Authentication (x509) β MongoDB supports x.509 certificate authentication for use with a secure TLS/SSL connection as of 2.6.x. β The x.509 client authentication allows clients to authenticate to servers with certificates rather than with a username and password. β Enabled with: security.clusterAuthMode: x509 23
Security: Encryption at Rest β MongoDB Enterprise β Encryption supported in Enterprise binaries ($$$) β Percona Server for MongoDB β Use CryptFS/LUKS block device for encryption of data volume β Documentation published (or coming soon) β Completely open-source / Free β Application-Level β Selectively encrypt only required fields in application β Benefits β The data is only readable by the application (reduced touch points) β The resource cost of encryption is lower when itβs applied selectively β Offloading of encryption overhead from database 24
Security: Network Firewall β MongoDB only requires a single TCP port to be reachable (to all nodes) β Default port 27017 β This does not include monitoring tools, etc β Percona PMM requires inbound connectivity to 1-2 TCP ports β Restrict TCP port access to nodes that require it! β Sharded Cluster β Application servers only need access to βmongosβ β Block direct TCP access from application -> shard/mongod instances β Unless βmongosβ is bound to localhost! β Advanced β Move inter-node replication to own network fabric, VLAN, etc β Accept client connections on a Public interface 25
More on Security (some overlap) Room: Field Suite #2 Time: Tuesday, 17:25 to 17:50 26
Monitoring
Monitoring: Methodology β Monitor often β 60 - 300 seconds is not enough! β Problems can begin/end in seconds β Correlate Database and Operating System together! β Monitor a lot β Store more than you graph β Example: PMM gathers 700-900 metrics per polling β Process β Use to troubleshoot Production events / incidents β Iterate and Improve monitoring β Add graphing for whatever made you SSH to a host β Blind QA with someone unfamiliar with the problem 28
Monitoring: Important Metrics β Database β Operation counters β Cache Traffic and Capacity β Checkpoint / Compaction Performance β Concurrency Tickets (WiredTiger and RocksDB) β Document and Index scanning β Various engine-specific details β Operating System β CPU β Disk β Bandwidth / Util β Average Wait Time β Memory and Network 29
Monitoring: Percona PMM β Open-source monitoring from Percona! β Based on open-source technology Prometheus β Grafana β Go Language β β Simple deployment β Examples in this demo are from PMM! β Correlation of OS and DB Metrics β 800+ metrics per ping 30
Architecture and High-Availability
High Availability β Replication β Asynchronous β Write Concerns can provide psuedo-synchronous replication β Changelog based, using the βOplogβ β Maximum 50 members β Maximum 7 voting members β Use βvote:0β for members $gt 7 β Oplog β The βoplog.rsβ capped-collection in βlocalβ storing changes to data β Read by secondary members for replication β Written to by local node after βapplyβ of operation 32
Architecture β Datacenter Recommendations β Minimum of 3 x physical servers required for High-Availability β Ensure only 1 x member per Replica Set is on a single physical server!!! β EC2 / Cloud Recommendations β Place Replica Set members in odd number of Availability Zones, same region β Use a hidden-secondary node for Backup and Disaster Recover in another region β Entire Availability Zones have been lost before! 33
Hardware
Hardware: Mainframe vs Commodity β Databases: The Past β Buy some really amazing, expensive hardware β Buy some crazy expensive license β Donβt run a lot of servers due to above β Scale up: β Buy even more amazing hardware for monolithic host β Hardware came on a truck β HA: When it rains, it pours β Databases: A New Era β Everything fails, nothing is precious β Elastic infrastructures (βThe cloudβ, Mesos, etc) β Scale up: add more cheap, commodity servers β HA: lots of cheap, commodity servers - still up! 35
Hardware: Block Devices β Isolation Run Mongod dbPaths on separate volume β Optionally, run Mongod journal on separate volume β β RAID Level RAID 10 == performance/durability sweet spot β RAID 0 == fast and dangerous β β SSDs Benefit MMAPv1 a lot β Benefit WT and RocksDB a bit less β Keep about 30% free for internal GC on the SSD β 36
Hardware: Block Devices β EBS / NFS / iSCSI Risks / Drawbacks β Exponentially more things to break β Block device requests wrapped in TCP is extremely slow β You probably already paid for some fast local disks β More difficult (sometimes nearly-impossible) to troubleshoot β MongoDB doesnβt really benefit from remote storage features/flexibility β β Built-in High-Availability of data via replication β MongoDB replication can bootstrap new members β Strong write concerns can be specified for critical data 37
Hardware: CPUs β Cores vs Core Speed β Lots of cores > faster cores (4 CPU minimum recommended) β Thread-per-connection Model β CPU Frequency Scaling β βcpufreqβ: a daemon for dynamic scaling of the CPU frequency β Terrible idea for databases or any predictability! β Disable or set governor to 100% frequency always, i.e mode: βperformanceβ β Disable any BIOS-level performance/efficiency tuneable β ENERGY_PERF_BIAS β A CentOS/RedHat tuning for energy vs performance balance β RHEL 6 = βperformanceβ β RHEL 7 = βnormalβ (!) β My advice: use βtunedβ to set to βperformanceβ 38
Hardware: Network Infrastructure β Datacenter Tiers β Network Edge β Public Server VLAN β Servers with Public NAT and/or port forwards from Network Edge β Examples: Proxies, Static Content, etc β Calls backends in Backend VLAN β Backend Server VLAN β Servers with port forwarding from Public Server VLAN (w/Source IP ACLs) β Optional load balancer for stateless backends β Examples: Webserver, Application Server/Worker, etc β Calls data stores in Data VLAN β Data VLAN β Servers, filers, etc with port forwarding from Backend Server VLAN (w/Source IP ACLs) β Examples: Databases, Queues, Filers, Caches, HDFS, etc 39
Hardware: Network Infrastructure β Network Fabric β Try to use 10GBe for low latency β Use Jumbo Frames for efficiency β Try to keep all MongoDB nodes on the same segment β Goal: few or no network hops between nodes β Check with βtracerouteβ β Outbound / Public Access β Databases donβt need to talk to the internet* β Store a copy of your Yum, DockerHub, etc repos locally β Deny any access to Public internet or have no route to it β Hackers will try to upload a dump of your data out of the network!! β Cloud? β Try to replicate the above with features of your provider 40
Hardware: Why So Quick? β MongoDB allows you to scale reads and writes with more nodes β Single-instance performance is important, but deal-breaking β You are the most expensive resource! β Not hardware anymore 41
Tuning MongoDB
Tuning MongoDB: MMAPv1 β A kernel-level function to map file blocks to memory β MMAPv1 syncs data to disk once per 60 seconds (default) Override with βsyncDelay <seconds> flag β If a server with no journal crashes it can lose 1 min of β data!!! β In memory buffering of Journal Synced every 30ms βjournalβ is on a different disk β Or every 100ms β Or 1/3rd of above if change uses Journaled write β concern (explained later) 43
Tuning MongoDB: MMAPv1 β Fragmentation Can cause serious slowdowns on scans, range β queries, etc db.<collection>.stats() β Shows various storage info for a collection β Fragmentation can be computed by dividing β βstorageSizeβ by βsizeβ Any value > 1 indicates fragmentation β Compact when you near a value of 2 by rebuilding β secondaries or using the βcompactβ command WiredTiger and RocksDB have little-no fragmentation β due to checkpoints / compaction 44
Tuning MongoDB: WiredTiger β WT syncs data to disk in a process called βCheckpointingβ: Every 60 seconds or >= 2GB data changes β β In-memory buffering of Journal Journal buffer size 128kb β Synced every 50 ms (as of 3.2) β Or every change with Journaled write concern (explained β later) In between write operations while the journal records β remain in the buffer, updates can be lost following a hard shutdown! 45
Tuning MongoDB: RocksDB β Level-based strategy using immutable data level files β Built-in Compression β Block and Filesystem caches β RocksDB uses βcompactionβ to apply changes to data files Tiered level compaction β Follows same logic as MMAPv1 for journal β buffering β MongoRocks β A layer between RocksDB and MongoDBβs storage engine API β Developed in partnership with Facebook 46
Tuning MongoDB: Storage Engine Caches β WiredTiger β In heap β 50% available system memory β Uncompressed WT pages β Filesystem Cache β 50% available system memory β Compressed pages β RocksDB β Internal testing planned from Percona in the future β 30% in-heap cache recommended by Facebook / Parse 47
Tuning MongoDB: Durability β storage.journal.enabled = <true/false> Default since 2.0 on 64-bit builds β Always enable unless data is transient β Always enable on cluster config servers β β storage.journal.commitIntervalMs = <ms> Max time between journal syncs β β storage.syncPeriodSecs = <secs> Max time between data file flushes β 48
Tuning MongoDB: Donβt Enable! β βcpuβ β External monitoring is recommended β βrestβ β Will be deprecated in 3.6+ β βsmallfilesβ β In most situations this is not necessary unless β You use MMAPv1, and β It is a Development / Test environment β You have 100s-1000s of databases with very little data inside (unlikely) β Profiling mode β2β β Unless troubleshooting an issue / intentional 49
Tuning Linux
Tuning Linux: The Linux Kernel β Linux 2.6.x? β Avoid Linux earlier than 3.10.x - 3.12.x β Large improvements in parallel efficiency in 3.10+ (for Free!) β More: https://blog.2ndquadrant.com/postgresql-vs-kernel-versions/ 51
Tuning Linux: NUMA β A memory architecture that takes into account the locality of memory, caches and CPUs for lower latency β But no databases want to use it :( β MongoDB codebase is not NUMA βawareβ, causing unbalanced memory allocations on NUMA systems β Disable NUMA In the Server BIOS β Using βnumactlβ in init scripts BEFORE βmongodβ β command (recommended for future compatibility) : numactl --interleave=all /usr/bin/mongod <other flags> 52
Tuning Linux: Transparent HugePages β Introduced in RHEL/CentOS 6, Linux 2.6.38+ β Merges memory pages in background (Khugepaged process) β Decreases overall performance when used with MongoDB! β βAnonHugePagesβ in /proc/meminfo shows usage β Disable TransparentHugePages! β Add βtransparent_hugepage=neverβ to kernel command-line (GRUB) Reboot the system β β Disabling online does not clear previous TH pages β Rebooting tests your system will come back up! 53
Tuning Linux: Time Source β Replication and Clustering needs consistent clocks β mongodb_consistent_backup relies on time sync, for example! β Use a consistent time source/server β βItβs ok if everyone is equally wrongβ β Non-Virtualized β Run NTP daemon on all MongoDB and Monitoring hosts β Enable service so it starts on reboot β Virtualised β Check if your VM platform has an βagentβ syncing time β VMWare and Xen are known to have their own time sync β If no time sync provided install NTP daemon 54
Tuning Linux: I/O Scheduler β Algorithm kernel uses to commit reads and writes to disk β CFQ βCompletely Fair Queueβ β Default scheduler in 2.6-era Linux distributions β Perhaps too clever/inefficient for database workloads β Probably good for a laptop β β Deadline Best general default IMHO β Predictable I/O request latencies β β Noop Use with virtualised servers β Use with real-hardware BBU RAID controllers β 55
Tuning Linux: Filesystems β Filesystem Types Use XFS or EXT4, not EXT3 β EXT3 has very poor pre-allocation performance β Use XFS only on WiredTiger β EXT4 βdata=orderedβ mode recommended β Btrfs not tested, yet! β β Filesystem Options Set βnoatimeβ on MongoDB data volumes in β/etc/fstabβ: β Remount the filesystem after an options change, or reboot β 56
Tuning Linux: Block Device Readahead β Tuning that causes data ahead of a block on disk to be read and then cached β Assumption: There is a sequential read pattern β Something will benefit from the extra cached blocks β β Risk Too high waste cache space β Increases eviction work β MongoDB tends to have very random disk patterns β β A good start for MongoDB volumes is a β32β (16kb) read-ahead β Let MongoDB worry about optimising the pattern 57
Tuning Linux: Block Device Readahead β Change ReadAhead Add file to β/etc/udev/rules.dβ β /etc/udev/rules.d/60-mongodb-disk.rules: β # set deadline scheduler and 32/16kb read-ahead for /dev/sda ACTION=="add|change", KERNEL=="sda", ATTR{queue/scheduler}="deadline", ATTR{bdi/read_ahead_kb}="16" Reboot (or use CLI tools to apply) β 58
Tuning Linux: Virtual Memory Dirty Pages β Dirty Pages Pages stored in-cache, but needs to be written to β storage β Dirty Ratio Max percent of total memory that can be dirty β VM stalls and flushes when this limit is reached β Start with β10β, default (30) too high β β Dirty Background Ratio Separate threshold forbackground dirty page β flushing Flushes without pauses β Start with β3β, default (15) too high β 59
Tuning Linux: Swappiness β A Linux kernel sysctl setting for preferring RAM or disk for swap Linux default: 60 β To avoid disk-based swap: 1 (not zero!) β To allow some disk-based swap: 10 β β0β can cause more swapping than β1β on recent β kernels β More on this here: https://www.percona.com/blog/2014/04/28/oom-rela tion-vm-swappiness0-new-kernel/ 60
Tuning Linux: Ulimit β Allows per-Linux-user resource constraints Number of User-level Processes β Number of Open Files β CPU Seconds β Scheduling Priority β And othersβ¦ β β MongoDB Should probably have a dedicated VM, container or server β Creates a new process β β For every new connection to the Database β Plus various background tasks / threads β Creates an open file for each active data file on disk β 64,000 open files and 64,000 max processes is a good start 61
Tuning Linux: Ulimit β Setting ulimits β /etc/security/limits.d file β Systemd Service β Init script β Ulimits are set by Percona and MongoDB packages! β Example on left: PSMDB RPM (Systemd) 62
Tuning Linux: Network Stack β Defaults are not good for > 100mbps Ethernet β Suggested starting point: β Set Network Tunings: β Add the above sysctl tunings to /etc/sysctl.conf β Run β/sbin/sysctl -pβ as root to set the tunings β Run β/sbin/sysctl -aβ to verify the changes 63
Tuning Linux: More on this... https://www.percona.com/blog/2016/08/12/tuning-linux-for-mongodb/ 64
Tuning Linux: βTunedβ β Tuned A βframeworkβ for applying tunings to Linux β RedHat/CentOS 7 only for now β Debian added tuned, not sure if compatible yet β Cannot tune NUMA, file system type or fs mount opts β Syctls, THP, I/O sched, etc β My apology to the community for writing βTuning Linux for MongoDBβ: β β https://github.com/Percona-Lab/tuned-percona-mongodb 65
Troubleshooting βThe problem with troubleshooting is trouble shoots backβ ~ Unknown
Troubleshooting: Usual Suspects β Locking Collection-level locks β Document-level locks β Software mutex/semaphore β β Limits Max connections β Operation rate limits β Resource limits β β Resources Lack of IOPS, RAM, CPU, network, etc β 67
Troubleshooting: MongoDB Resources β Memory β CPU System CPU β FS cache β Networking β Disk I/O β Threading β β User CPU (MongoDB) Compression (WiredTiger and RocksDB) β Session Managemen β BSON (de)serialisation β Filtering / scanning / sorting β 68
Troubleshooting: MongoDB Resources β User CPU (MongoDB) Optimiser β Disk β Data file read/writes β Journaling β Error logging β Network β Query request/response β Replication β β Disk I/O β Journaling β Oplog Reads / Writes β Background Flushing / Compactions / etc 69
Troubleshooting: MongoDB Resources β Disk I/O β Page Faults (data not in cache) β Swapping β Network β Client API β Replication β Sharding β Chunk Moves β Mongos -> Shards 70
Troubleshooting: db.currentOp() β A function that dumps status info about running operations and various lock/execution details β Only queries currently in progress are shown. β Provided Query ID number can be used to kill long running queries. β Includes β Original Query β Parsed Query β Query Runtime β Locking details β Filter Documents β { "$ownOps": true } == Only show operations for the current user β https://docs.mongodb.com/manual/reference/method/db.currentOp/#examples 71
Troubleshooting: db.stats() β Returns β Document-data size (dataSize) β Index-data size (indexSize) β Real-storage size (storageSize) β Average Object Size β Number of Indexes β Number of Objects 72
Troubleshooting: db.currentOp() 73
Troubleshooting: Log File β Interesting details are logged to the mongod/mongos log files Slow queries β Storage engine details (sometimes) β Index operations β Sharding β Chunk moves β Elections / Replication β Authentication β Network β Connections β β Errors β Client / Inter-node connections 74
Troubleshooting: Log File - Slow Query 2017-09-19T20:58:03.896+0200 I COMMAND [conn175] command config.locks appName: "MongoDB Shell" command: findAndModify { findAndModify: "locks", query: { ts: ObjectId('59c168239586572394ae37ba') }, update: { $set: { state: 0 } }, writeConcern: { w: "majority", wtimeout: 15000 }, maxTimeMS: 30000 } planSummary: IXSCAN { ts: 1 } update: { $set: { state: 0 } } keysExamined: 1 docsExamined: 1 nMatched: 1 nModified: 1 keysInserted: 1 keysDeleted: 1 numYields: 0 reslen: 604 locks: { Global: { acquireCount: { r: 2, w: 2 } }, Database: { acquireCount: { w: 2 } }, Collection: { acquireCount: { w: 1 } }, Metadata: { acquireCount: { w: 1 } }, oplog: { acquireCount: { w: 1 } } } protocol: op_command 106ms 75
Troubleshooting: Operation Profiler β Writes slow database operations to a new MongoDB collection for analysis Capped Collection βsystem.profileβ in each database, default 1mb β The collection is capped, ie: profile data doesnβt last forever β β Support for operationProfiling data in Percona Monitoring and Management in current future goals β Enable operationProfiling in βslowOpβ mode Start with a very high threshold and decrease it in steps β Usually 50-100ms is a good threshold β Enable in mongod.conf β operationProfiling: slowOpThresholdMs: 100 mode: slowOp 76
Troubleshooting: Operation Profiler β Useful Profile Metrics op/ns/query: type, namespace and query of a profile β keysExamined: # of index keys examined β docsExamined: # of docs examined to achieve result β writeConflicts: # of Write Concern Exceptions β encountered during update numYields: # of times operation yielded for others β locks: detailed lock statistics β 77
Troubleshooting: .explain() β Shows query explain plan for query cursors β This will include β Winning Plan β Query stages β Query stages may include sharding info in clusters β Index chosen by optimiser β Rejected Plans 78
Troubleshooting: .explain() and Profiler 79
Troubleshooting: Cluster Metadata β The βconfigβ database on Cluster Config servers β Use .find() queries to view Cluster Metadata β Contains actionlog (3.0+) β changelog β databases β collections β shards β chunks β settings β mongos β locks β lockpings β 80
Troubleshooting: Percona PMM QAN β The Query Analytics tool enables DBAs and developers to analyze queries over periods of time and find performance problems. β Helps you optimise database performance by making sure that queries are executed as expected and within the shortest time possible. β Central, web-based location for visualising data. β Agent collected from MongoDB Profiler (required) from agent. β Great for reducing access to systems while proving valueable data to development teams! β Query Normalization β ie:β{ item: 123456 }β -> β{ item: ##### }β. β Command-line Equivalent: pt-mongodb-query-digest tool 81
Troubleshooting: Percona PMM QAN 82
Troubleshooting: mlogfilter β A useful tool for processing mongod.log files β A log-aware replacement for βgrepβ, βawkβ and friends β Generally focus on β mlogfilter --scan <file> β Shows all collection scan queries β mlogfilter --slow <ms> <file> β Shows all queries that are slower than X milliseconds β mlogfilter --op <op-type> <file> β Shows all queries of the operation type X (eg: find, aggregate, etc) β More on this tool here https://github.com/rueckstiess/mtools/wiki/mlogfilter 83
Troubleshooting: Common Problems β Sharding β removeShard Doesnβt Complete β Check the βdbsToMoveβ array of the removeShard response mongos> db.adminCommand({removeShard:"test2"}) { "msg" : "draining started successfully", "state" : "started", "shard" : "test2", "note" : "you need to drop or movePrimary these databases", "dbsToMove" : [ "wikipedia" ], "ok" : 1 } β Why? mongos> use config switched to db config mongos> db.databases.find() { "_id" : "wikipedia", "primary" : "test2" , "partitioned" : true } 84
Troubleshooting: Common Problems β Sharding β removeShard Doesnβt Complete β Try β Use movePrimary to move database(s) Primary-role to others β Run the removeShard command once the shard being removed is NOT primary for any database β This starts ths draining of the shard β Run the same removeShard command to check on progress. β If the draining and removing is complete this will respond with success β Jumbo Chunks β Will prevent balancing from occurring β config.chunks collection document will contain jumbo:true as a key/value pair β Sharding βsplitβ commands can be used to reduce the chunk size (sh.splitAt, etc) β https://www.percona.com/blog/2016/04/11/dealing-with-jumbo-chunks-in-mongodb/ 85
Schema Design & Workflow βThe problem with troubleshooting is trouble shoots backβ ~ Unknown
Schema Design: Data Types β Strings β Only use strings if required β Do not store numbers as strings! β Look for {field:β123456β} instead of {field:123456} β β12345678β moved to a integer uses 25% less space β Range queries on proper integers is more efficient β Example JavaScript to convert a field in an entire collection β db.items.find().forEach(function(x) { newItemId = parseInt(x.itemId); db.containers.update( { _id: x._id }, { $set: {itemId: itemId } } ) }); 87
Schema Design: Data Types β Strings β Do not store dates as strings! β The field "2017-08-17 10:00:04 CEST" stores in 52.5% less space! β Do not store booleans as strings! β βtrueβ -> true = 47% less space wasted β DBRefs β DBRefs provide pointers to another document β DBRefs can be cross-collection β NumberLong (3.4+) β Higher precision for floating-point numbers 88
Schema Design: Indexes β MongoDB supports BTree, text and geo indexes Default behaviour β β Collection lock until indexing completes β {background:true} Runs indexing in the background avoiding pauses β Hard to monitor and troubleshoot progress β Unpredictable performance impact β β Avoid drivers that auto-create indexes β Use real performance data to make indexing decisions, find out before Production! β Too many indexes hurts write performance for an entire collection β Indexes have a forward or backward direction β Try to cover .sort() with index and match direction! 89
Schema Design: Indexes β Compound Indexes β Several fields supported β Fields can be in forward or backward direction β Consider any .sort() query options and match sort direction! β Composite Keys are read Left -> Right β Index can be partially-read β Left-most fields do not need to be duplicated! β All Indexes below are duplicates: β {username: 1, status: 1, date: 1, count: -1} β {username: 1, status: 1, data: 1} β {username: 1, status: 1 } β {username: 1 } β Use db.collection.getIndexes() to view current Indexes 90
Schema Design: Query Efficiency β Query Efficiency Ratios β Index: keysExamined / nreturned β Document: docsExamined / nreturned β End goal: Examine only as many Index Keys/Docs as you return! β Tip: when using covered indexes zero documents are fetched (docsExamined: 0) ! β Example: a query scanning 10 documents to return 1 has efficiency 0.1 β Scanning zero docs is possible if using a covered index! 91
Schema Workflow β MongoDB optimised for single-document operations β Single Document / Centralised Greate cache/disk-footprint efficiency β Centralised schemas may create a hotspot for write locking β β Multi Document / Decentralised β MongoDB rarely stores data sequentially on disk β Multi-document operations are less efficient β Less potential for hotspots/write locking β Increased overhead due to fan-out of updates β Example: Social Media status update, graph relationships, etc β More on this later.. 92
Schema Workflow β Read Heavy Workflow Read-heavy apps benefit from pre-computed results β Consider moving expensive reads computation to insert/update/delete β Example 1: An app does βcountβ queries often β Move .count() read query to a summary document with counters β Increment/decrement single count value at write-time β Example 2: An app that does groupings of data β Move .aggregate() read query that is in-line to the user to a backend summary worker β Read from a summary collection, like a view β β Write Heavy Workflow Reduce indexing as much as possible β Consider batching or a decentralised model with lazy updating (eg: social media β graph) 93
Schema Workflow β Batching Inserts/Updates Requires less network commands β Allows the server to do some internal batching β Operations will be slower overall Suited for queue worker scenarios batching many changes Traditional user-facing database traffic should aim to operate on a single (or few) document(s) β Thread-per-connection model β 1 x DB operation = 1 x CPU core only β Executing Parallel Reads β Large batch queries benefit from several parallel sessions β Break query range or conditions into several client->server threads β Not recommended for Primary nodes or Secondaries with heavy reads 94
Schema Workflow β No list of fields specified in .find() β MongoDB returns entire documents unless fields are specified β Only return the fields required for an application operation! β Covered-index operations require only the index fields to be specified β Using $where operators β This executes JavaScript with a global lock β Many $and or $or conditions β MongoDB (or any RDBMS) doesnβt handle large lists of $and or $or efficiently β Try to avoid this sort of model with β Data locality β Background Summaries / Views 95
Fan Out / Fan In β Fan-Out Systems β Decentralised β Data is eventually written in many locations β Complex write path (several updates) β Good use-case for Queue/Worker model β Batching possible β Simple read path (data locality) β Fan-In β Centralised β Simple Write path β Possible Write locking β Complex Read Path β Potential for latency due to network 96
Data Integrity βThe problem with troubleshooting is trouble shoots backβ ~ Unknown
Data Integrity: `whoami` (continued) β Very Paranoid β Previous RDBMs β Online Marketing / Publishing β Paid for clicks coming in β Downtime = revenue + traffic (paid for) loss β Warehousing / Pricing SaaS β Store real items in warehouses/stores/etc β Downtime = many businesses (customers)/warehouses/etc at stand-still β Integrity problems = β Orders shipped but not paid for 2010 β Orders paid for but not shipped, etc β Moved on to Gaming, Percona β So why MongoDB? 98
Data Integrity: Storage and Journaling β The Journal provides durability in the event of failure of the server β Changes are written ahead to the journal for each write operation β On crash recovery, the server Finds the last point of consistency to disk β Searches the journal file(s) for the record matching the checkpoint β Applies all changes in the journal since the last point of consistency β β Journal data is stored in the βjournalβ subdirectory of the server data path (dbPath) β Dedicated disks for data (random I/O) and journal (sequential I/O) improve performance 99
Data Integrity: Write Concern β MongoDB Replication is Asynchronous β Write Concerns Allow control of data integrity of a write to a Replica Set β β Write Concern Modes β βw: <num>β - Writes much acknowledge to defined number of nodes β βmajorityβ - Writes much acknowledge on a majority of nodes β β<replica set tag>β - Writes acknowledge to a member with the specified replica set tags β Durability β By default write concerns are NOT durable β βj: trueβ - Optionally, wait for node(s) to acknowledge journaling of operation β In 3.4+ βwriteConcernMajorityJournalDefaultβ allows enforcement of βj: trueβ via replica set configuration! β Must specify βj: falseβ or alter βwriteConcernMajorityDefaultβ to disable 100
Recommend
More recommend
Explore More Topics
Stay informed with curated content and fresh updates.