Let's Take a Look at NoSQL MongoDB
This post was migrated from Tistory. You can find the original here.
Getting started
Looking at messenger services today, there doesn’t seem to be much need for other tables to reference a Message table as a foreign key.
If Message never needs to be used as an FK, wouldn’t it make sense to use NoSQL, which generally has faster I/O, instead of a relational database?
Let’s take a look at MongoDB.
A Document is structured as JSON {}.
Fields aren’t fixed like columns — they’re free-form key : value pairs.
A value can be a boolean, an array ([ ]), null, or even a nested document ({ }).
Document
1
2
3
4
5
6
7
8
9
10
11
12
{
_id: ObjectId('64f9eb1cdb80f40007827e02'),
text: '안녕하세요',
'boolean': true,
array: [
1,
2,
3,
4
],
empty: null
}
A single Document has fields like the ones above (a key named _id with a value of type ObjectId).
ObjectId is a unique key similar to a PK.
The difference is that a PK is assigned directly by the DBMS, whereas an ObjectId is generated on the client side.
The Router (mongos) can look at the ObjectId to figure out which shard the data lives on and request it from there — it seems the key is generated client-side specifically so that sharded data can be retrieved quickly.
An ObjectId is 12 bytes of binary data, made up as follows:
Unix Timestamp (4 bytes) + Random Value (5 bytes) + Count (3 bytes)
MongoDB’s distributed system
Replication
A Master-Slave setup.
If the Master fails, a new Master is elected from among the Slaves.
Sharding
A method for storing large volumes of data by distributing the database across software to handle the load.
Sharding approaches include splitting by table and splitting a table itself.
Looking at the image above helps make sense of what was said earlier:
“The Router (mongos) can look at the ObjectId to request data from the shard where it actually lives.”
Map Reduce
A technique that uses one or more machines to distribute the computation of data and then merge the results back together, splitting the process into a Map stage and a Reduce stage.
References
https://kciter.so/posts/about-mongodb
https://www.youtube.com/watch?v=81JnYGT2HVQ&list=PL9mhQYIlKEheyXIEL8RQts4zV_uMwdWFj&index=4

