Data Storage & Databases

Serving static pages for users can be suitable for landing pages, or for personal blogs. However, if you want to deliver personalized content you have to store the data somewhere, and there are two ways to do that:

Volatile/Temporary Memory (RAM):

  • Extremely fast but expensive.
  • Memory resets each time you restart your application so data is not retained.
  • If you don’t clean up, you can end up with stack overflow.
  • Variables are stored in RAM.

Non-volatile/Permanent Memory (hard drive): 

  • Slow but inexpensive.
  • Data has to be serialized, or translated, into a format that can be stored or transmitted and reconstructed later.   An example of serialization is JSON.stringify() and JSON.parse().
  • Data persistence is achieved by saving a program state in non-volatile memory because data is retained when you restart your application.
  • There are two ways to store data in your hard drive:
    • Database:
      • A giant excel sheet that you can add, remove, or update data that you want to persist.
      • You would not want to store large data such as asset files  in the database.
    • File System (fs):
      • Assets like images and videos are usually stored in your file system (and referenced from your database).
      • Your file system is stored in your hard drive along with your database (if you have one).

DATABASES:

noSQL:

  • use ID index to achieve O(1)
  • records are stored in JSON and need to be parsed to find particular field.
  • You don’t have to define a schema and you can store any arbitrary JSON.
  • Example: PostgresSQL

SQL:

  • use array-like techniques (fixed width data format) to achieve O(1)
  • Have to define schemas.
  • faster at computing relationships because
    • it can skip ID indexing step (ID -> Disk Location).
    • records are stored in binary format (not JSON) and fields are always in the same location in every record.
  • Example: mongoDB

Screen Shot 2017-12-22 at 10.51.08 AM.png

ORM: softwares such as Sequelize that provide Javascript-like syntax for interaction with databases.

Schemas:  description of the way data is organized in a database.

  • PRIMARY KEY:  A column with primary key means the value of the column is unique.  Primary keys from one tables are often referred to in other tables.
  • FOREIGN KEY: When the value of a column is intended to refer to a column in another table, it is called a foreign key.

Screen Shot 2017-12-22 at 11.14.19 AM

By referring to data in columns of other tables rather than duplicating the data in two tables, it’s more memory efficient.

SCHEMA & QUERY DESIGN 

join table: breaks many-to-many relationship into one-to-many relationships.

 

Advertisements
Data Storage & Databases

JS: Object (references) Comparison

When you create an object in Javascript, it stores the object at a unique location in memory.  So even when the objects look identical, they are not the same because JS is comparing to see if they are at the same memory address, not if they look the same or not.

Thus, {} === {}  would return false.

Notes: 

  • Values and variables of same type take same amount of fixed storage (allocated memory).
  • All objects, however, are not of the same size. When a new object is created, a composite memory block consisting of a series of consecutive blocks, one for each instance variable of the object, is created.  The sizes of two objects of different types can be different depending on the number and the types of their instances variables.
  • Since an object variable stores addresses, it also called a pointer variable or reference variable, and the address stored in it a pointer or reference.
JS: Object (references) Comparison