What are Databases and How They Work
What is a Database?
A database is an organised collection of data that is stored and accessed electronically. Every application you use daily relies on one — your bank stores your transactions in a database, Instagram stores your photos and follower relationships, and Paystack stores every payment record.
Without databases, data would vanish the moment a server restarts. Databases give us a reliable, structured way to persist (save) information so we can retrieve it later, update it, search through it, and delete it when no longer needed.
Why Not Just Use Files?
You might wonder: why not just write data to a text file? For tiny projects, you could — but files break down quickly:
- You cannot easily search for a specific record among millions of entries
- Two users writing to the same file at the same time can corrupt it
- There is no way to enforce rules like "every user must have an email address"
- Files have no concept of relationships between data
Databases solve all of these problems. They are purpose-built engines optimised for reading and writing data safely and efficiently.
Types of Databases
1. Relational Databases (SQL)
Relational databases store data in tables — rows and columns, much like a spreadsheet. Tables can be linked together through shared keys.
Examples: PostgreSQL, MySQL, SQLite
Paystack, a Nigerian payment company, uses PostgreSQL to store transaction records. The rigid structure and strong consistency guarantees make it ideal for financial data where accuracy is critical.
users table:
| id | name | email |
|----|---------|------------------ |
| 1 | Amaka | amaka@example.com |
| 2 | Chukwu | chukwu@example.com |
transactions table:
| id | user_id | amount | status |
|----|---------|--------|---------|
| 1 | 1 | 5000 | success |
| 2 | 2 | 2500 | pending |
The user_id column in the transactions table references the id in the users table — that is a foreign key relationship.
2. Document Databases (NoSQL)
Document databases store data as flexible JSON-like documents rather than fixed rows. Each document can have different fields.
Examples: MongoDB, Firestore
Twitter (now X) and many social media platforms use document databases because user profiles and posts vary wildly in structure. One user might have a bio; another might not. Document databases handle this naturally.
{
"_id": "64abc123",
"username": "amaka_codes",
"bio": "Backend developer in Lagos",
"posts": 142,
"verified": true
}
3. Key-Value Databases
Key-value databases are the simplest type — you store a value under a key, like a dictionary. They are extremely fast.
Examples: Redis, DynamoDB
Redis is commonly used to cache (temporarily store) data so your API does not hit the main database on every request. If fetching a user's profile from MongoDB takes 50ms, caching it in Redis means subsequent fetches take under 1ms.
4. Time-Series Databases
Time-series databases are optimised for data that changes over time — sensor readings, stock prices, application metrics.
Examples: InfluxDB, TimescaleDB
A company monitoring server CPU usage every second would store those readings in a time-series database for fast querying by time range.
How Data is Stored and Retrieved
Storage
Databases write data to disk (persistent storage) in structured formats. Most databases maintain a write-ahead log (WAL) — they record every change before applying it, so they can recover from crashes without losing data.
Indexes
An index is a separate data structure that makes searching fast. Without an index, finding a user by email means scanning every row in the database. With an index on the email column, the database jumps directly to the matching record.
Think of a book's index at the back — instead of reading every page to find "MongoDB", you look up the letter M and get the exact page numbers.
Without index: scan 1,000,000 rows → slow
With index on email: jump directly to match → fast
Queries
You interact with a database by sending queries — instructions that describe what data you want.
SQL (Structured Query Language) query:
SELECT name, email FROM users WHERE id = 1;
MongoDB query (JavaScript object syntax):
db.users.findOne({ _id: "64abc123" });
Both return the same concept — a specific record — but the syntax differs between database types.
Choosing the Right Database
There is no universally "best" database. The choice depends on your data:
| Use case | Recommended type |
|---|---|
| Financial records, user accounts | Relational (PostgreSQL) |
| Social profiles, product catalogues | Document (MongoDB) |
| Sessions, caching, real-time data | Key-Value (Redis) |
| IoT sensors, analytics metrics | Time-Series (InfluxDB) |
Many production applications use multiple databases together — for example, PostgreSQL for core user data, MongoDB for product content, and Redis for caching.
Practice Exercise
Build a simple in-memory "database" using JavaScript to understand core concepts:
- Create an array called
usersto hold user objects - Write a function
findById(id)that searches the array and returns the matching user - Write a function
insert(user)that adds a user to the array - Write a function
deleteById(id)that removes a user by their ID - Test each function in your browser's console
This exercise mirrors exactly what a real database engine does — it just stores data in memory rather than on disk.
Try it yourself
Key Takeaways
- A database is an organised, persistent store of data that applications can query, update, and delete reliably.
- Relational databases (PostgreSQL, MySQL) store data in tables and are ideal for structured data requiring strict consistency.
- Document databases (MongoDB) store flexible JSON-like documents and suit applications with varying data structures.
- Indexes make searches fast by allowing the database to jump directly to matching records instead of scanning every row.
- Many production systems combine multiple database types — for example, PostgreSQL for core data, MongoDB for content, and Redis for caching.
Quick Quiz
1.What is the primary purpose of a database index?
2.Which type of database would be most appropriate for storing financial transaction records that require strict consistency?
3.Why would a developer use Redis alongside MongoDB in the same application?
4.What problem does a database solve that a plain text file cannot?
Ready to go further?
CareerEx gives you structured 12-week training, live classes every Saturday and Sunday, real tutor feedback, and a certificate. Join the next cohort.
Join CareerEx