Module 6 - Introduction To Databases
Module 6 - Introduction To Databases
Introduction to databases
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Contents
Objective:
Topics:
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
When do we need to handle data?
Serving & Saving “content”
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Separating code from data
Analyse a few examples, and see what bits of the application we would put in code and what bits we
classify as data
- Flipkart
- Gmail
- Facebook
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Separating code from data
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Separating code from data
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Separating code from data
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Our current understanding of data
We covered 3 examples of using data:
1. Article templating
2. Counter incrementing
3. Adding to a names array
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Our current understanding of data
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Our current understanding of data
In all 3 examples, we stored the data as a data-structure in our code. However, some problems:
1. Article data was a part of the source code. What if we had 10,000 articles? What if it was more than
a few GB? We can’t load that much as a part of our program source code! (RAM would be uselessly
consumed)
2. Code maintenance and data maintenance are 2 separate problems which would get coupled
3. The counter and the names array is not persisted. Every single time the server restarts, we lose that
information.
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
So. How do we solve this?
Extremely simple ideas!
1. Separate the data files from the code files and allow storage of large varying volumes of data
2. Load data on demand to reduce pressure on resources
3. Save data back to the data files as required
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Implementation
We can implement a database ourselves.
Just store our data in separate files. Split them across multiple files if the files get too large. We can read
the file to load data and save data.
1) Find data according to a particular property. Eg: All users with age greater than 21
2) Writing/updating data concurrently on the same file
3) Multiple people updating the same data. Eg: Wikipedia article being updated in parallel
4) Your server/hardware/OS crashes while you are writing information to the file
5) How would you secure access to these files?
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Common problems should be solved once
In a high-performance and scalable way:
This is why software to manage databases exist. One should almost NEVER EVER implement one’s own
database.
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Terminology
A database is a term that is used to refer to a collection of data. Excel sheets, a folder full of files, a CSV
files are all databases.
In app development a database is commonly used to refer to a database managed by a specific software
like SQLite, MySQL, Postgres or MongoDB. Databases can be created by server-side or client-side
software.
These software that manage one or more databases for you are called DBMSs (DataBase Management
Systems).
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Using a database
1. We already understood that if we have data in the right data structures in our server side code, we
can achieve whatever we want
a. Articles object
b. Counter integer
c. Names array
2. We now just need to get the right data into our data structures
3. Process:
a. Connect to the database
b. Whenever required, query from the database
c. Load the results into a data-structure that your server-side code can manipulate
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Web Frameworks & ORMs
1. This process is also very common within the same web framework. So web-frameworks often come
with tools to help you do this querying database, loading results business
2. For many frameworks, these tools fall in a category called ORMs (Object-Relational Mappings)
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Backend architecture (before database)
Web server
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Backend architecture (with database)
query processing
response request
Web server DBMS
Files
Disk
Computer (server machine)
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)
Key takeaways
● A database is a vital component of a modern app.
○ It is easy to separate portions of the app that should be a part of the application source code and portions of
the app that should be maintained as data.
● A database management system (the software that manages the database) should be used, as far
as possible, instead of trying to handle data operations yourself
● Modern DBMSs solve common problems across apps in a performant and efficient way
○ Storing high volumes of data, or rapidly growing data
○ Querying data
○ Managing concurrent access
○ Managing access control
○ Managing storage & recovery from failure and corruption
Introduction to Modern Application Development Dr Gaurav Raina (IIT Madras), Tanmai Gopal (Hasura)