Databases
Introduction to Databases
What are databases?
An essential part of backend development is managing data. For example, when a user presses like on a video on YouTube, the app needs a way to record this data, associating it with the user's account and the specific video ID.
For Backend Developers, managing data usually involves setting up databases. You can visualize these like electronic filing systems, with thousands and thousands of rows. Without databases, data would be stored haphazardly, making it difficult to find and use. With them, data can be stored in a well-organized structure that's easier to manage and maintain.
Databases are also easier for developers to build infrastructure around – like the YouTube algorithm that looks at your likes, and recommends similar videos.
Relational databases
Relational databases organize data in what we might think of as the “traditional” way – tables and rows, like you’d see on an Excel spreadsheet.
For example, a record store might have a database with album titles in Column A, artist names in Column B, and the numbers of sales in Column C. This is typically referred to as a database schema; this term describes the structure of each column of data, and the specific data types used in each one, such as text, number, or boolean.
Once the table structure has been defined, it’s possible to query the database. A query is basically a request to retrieve, update, or otherwise manage data. For example, a Backend Developer could use a query to fetch all the album orders that were made last month, or check whether an artist has been selling well over the summer.
A query is usually written in Structured Query Language, or SQL. This is a special type of programming language.
Non-relational databases
Data doesn't always neatly fit into the rows and columns of a relational database. Imagine a company like Ebay, which stores data about millions of different products with completely different properties. For example, a car that runs on diesel, or a toy suitable for 5-year-olds.
In cases like these, a Backend Developer would use a non-relational database. There are many different examples of this, but one of the most common is a document data store. In a document data store, each product on Ebay would have a 'document' attached to them, like a bucket full of data. A toy’s document might mention the age range, while a car’s document would mention fuel.
A developer will often use a database engine to create their non-relational database. Popular examples include MongoDB, DynamoDB, CouchDB.
Non-relational databases are also known as NoSQL databases, because a lot of them don't use SQL for their queries. Instead, they use query structures and syntax that are specific to the database engine of choice.
Choosing a database
The right database can make an application run smoothly and efficiently, while the wrong database can lead to performance issues, failures, errors, or just things not going as quickly as you’d like.
While traditional relational databases offer consistency and integrity, that can be difficult to scale when websites have massive amounts of data or high traffic. As the data grows, it becomes challenging to make significant changes without disrupting the system and causing downtime.
NoSQL databases, on the other hand, are often designed to scale horizontally, which makes it a lot easier to handle an ever-increasing load but at the expense of having highly structured relational data.
If data is highly structured, and the application is not expected to scale significantly, a relational database might be the best choice. If the data is less structured, and the application needs to scale rapidly, a NoSQL database is better.
Database Design and Optimization
Database design principles
After choosing between relational and non-relational databases, Backend Developers will still need to make some other design decisions. No two applications are ever quite the same, which means their database demands will be different.
For example, a developer will need to think about table design, which means choosing between the structural benefits of storing data in a single big table, versus the better performance of splitting data into separate tables.
They'll also need to think about normalization, which means minimizing data duplication as much as possible. For instance, instead of storing a user's email address twice, when they make two separate orders, the email address and both of the orders would be stored in a single row.
Database performance optimization
While setting up a database, Backend Developers need to optimize performance, and make sure it runs as smoothly and efficiently as possible.
If a database is slow to retrieve data, it can cause the application to lag, frustrating users and potentially driving them away. Think about how long you’d be happy to wait while a web page loaded for you to order ice cream for delivery.
A developer could improve this problem by working on query optimization – this reduces the time it takes to retrieve data from the database. Efficient use of hardware resources, like the servers the database is hosted on, can also ensure that the database runs smoothly, even when it's receiving a lot of queries at once.
Database management
After setting up a database, a developer's work keeps going. They will now be expected to maintain that database, and make sure it continues to function.
Over time, a database will fill up with data, and potentially start to slow down. Performance monitoring helps identify and fix performance issues like slow response times, high server load, bottlenecks, and resource limitations.
Developers should also schedule automated backups of the database, using scripts or tools that create copies of the database at specific intervals, such as daily or weekly. These backups are stored in a secure location, separate from the production environment, to prevent data loss in case of hardware failures, accidental deletions, or other unexpected issues.
Database Management and Security
Database security
Database security is a crucial part of backend development, as it protects sensitive data from unauthorized access and breaches. For example, encryption protects data by making it unreadable to anyone without the decryption key, while user authentication ensures that only authorized users can access a database.
A breach in database security can lead to significant losses, both financially and in terms of trust. In 2021, T-Mobile suffered two high profile security breaches, which resulted in the theft of sensitive customer data. This not only led to financial losses but also damaged the company's reputation.
Database security is an ongoing process. New threats are invented by fraudsters every year, and Backend Developers need to stay one step ahead. Regular audits can help to identify vulnerabilities, and ensure that a database's security measures are working as intended.
DBMs
Database Management Systems (DBMS) are software applications that provide an interface for interacting with the database, making it easier for developers to perform tasks like creating tables, inserting data, and running queries.
DBMS like MySQL and Oracle provide an interface for interacting with databases. This interface can be graphical, allowing developers to interact with the database using a visual interface, or it can be command-line based, requiring developers to type commands to interact with the database.
DBMS not only allow for data storage and retrieval, but also provide functionalities like backup and recovery, security, and data integrity. These features are crucial for maintaining the health and reliability of a database, making DBMS an essential tool for any Backend Developer.