DEV Community

Cover image for MySQL: Complete Architecture, Internals, and Practical Guide (With Code)
Farhad Rahimi Klie
Farhad Rahimi Klie

Posted on • Edited on

MySQL: Complete Architecture, Internals, and Practical Guide (With Code)

MySQL is one of the most widely used open-source relational database management systems (RDBMS) in the world. It powers everything from small applications to massive platforms such as Facebook, YouTube (historically), and many SaaS systems.

This article provides a complete, in-depth explanation of MySQL, including:

  • Architecture
  • Internal components
  • Storage engines
  • SQL execution flow
  • Transactions & ACID
  • Indexes
  • Replication
  • Security
  • Performance optimization
  • Real SQL examples

1. What Is MySQL?

MySQL is a relational database management system that:

  • Stores data in tables (rows & columns)
  • Uses SQL (Structured Query Language)
  • Is multi-user, multi-threaded
  • Follows a client–server architecture
  • Is maintained by Oracle

2. MySQL Client–Server Architecture

MySQL follows a client–server model.

Client (App / CLI / GUI)
        |
        v
   MySQL Server
        |
        v
   Storage Engine
        |
        v
     Disk / Memory
Enter fullscreen mode Exit fullscreen mode

Clients

  • MySQL CLI
  • Backend applications (Node.js, Django, Java, PHP)
  • GUI tools (MySQL Workbench, DBeaver)

Server

  • Parses SQL
  • Optimizes queries
  • Manages transactions
  • Communicates with storage engines

3. High-Level MySQL Architecture

MySQL architecture has three main layers:

1️⃣ Client Layer

2️⃣ SQL Layer

3️⃣ Storage Engine Layer


4. SQL Layer (Core of MySQL)

This layer is engine-independent and handles SQL logic.

Components of SQL Layer

4.1 Connection Manager

  • Manages client connections
  • Thread per connection
  • Authentication & authorization
SHOW PROCESSLIST;
Enter fullscreen mode Exit fullscreen mode

4.2 Query Cache (Deprecated)

  • Cached SELECT results
  • Removed in MySQL 8.0 due to scalability issues

4.3 SQL Parser

  • Checks syntax
  • Converts SQL into a parse tree

Example:

SELECT name FROM users WHERE id = 1;
Enter fullscreen mode Exit fullscreen mode

4.4 Preprocessor

  • Checks table & column existence
  • Validates permissions

4.5 Query Optimizer

  • Chooses best execution plan
  • Uses statistics and indexes

Example:

EXPLAIN SELECT * FROM users WHERE email = 'a@test.com';
Enter fullscreen mode Exit fullscreen mode

4.6 Query Executor

  • Executes optimized plan
  • Calls storage engine APIs

5. Storage Engine Layer

Storage engines define how data is stored, indexed, and retrieved.

Storage Engine Interface

MySQL uses a pluggable engine architecture.

SHOW ENGINES;
Enter fullscreen mode Exit fullscreen mode

6. InnoDB (Default Engine)

Why InnoDB?

  • ACID compliant
  • Supports transactions
  • Row-level locking
  • Foreign keys

6.1 InnoDB Internal Architecture

Components:

  • Buffer Pool
  • Redo Log
  • Undo Log
  • Tablespaces

6.2 Buffer Pool

  • Memory cache for data & indexes
  • Critical for performance
SHOW VARIABLES LIKE 'innodb_buffer_pool_size';
Enter fullscreen mode Exit fullscreen mode

6.3 Redo Log

  • Ensures durability
  • Crash recovery

6.4 Undo Log

  • Used for rollback
  • Enables MVCC

7. Transactions and ACID

ACID Properties

Property Meaning
Atomicity All or nothing
Consistency Valid state
Isolation Concurrent safety
Durability Persistent

Transaction Example

START TRANSACTION;

UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;

COMMIT;
Enter fullscreen mode Exit fullscreen mode

Rollback:

ROLLBACK;
Enter fullscreen mode Exit fullscreen mode

8. Isolation Levels

SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
Enter fullscreen mode Exit fullscreen mode
Level Issues
READ UNCOMMITTED Dirty reads
READ COMMITTED Non-repeatable reads
REPEATABLE READ (default) Phantom reads prevented
SERIALIZABLE Full isolation

9. Indexes in MySQL

Indexes improve read performance.

Types of Indexes

  • PRIMARY KEY
  • UNIQUE
  • INDEX
  • FULLTEXT
  • SPATIAL

Create Index

CREATE INDEX idx_email ON users(email);
Enter fullscreen mode Exit fullscreen mode

B-Tree Index Structure

  • Balanced tree
  • Logarithmic lookup time

Composite Index

CREATE INDEX idx_name_age ON users(name, age);
Enter fullscreen mode Exit fullscreen mode

Order matters.


10. Constraints

CREATE TABLE users (
  id INT PRIMARY KEY AUTO_INCREMENT,
  email VARCHAR(255) UNIQUE,
  age INT CHECK (age >= 18)
);
Enter fullscreen mode Exit fullscreen mode

11. Foreign Keys

CREATE TABLE orders (
  id INT PRIMARY KEY,
  user_id INT,
  FOREIGN KEY (user_id) REFERENCES users(id)
);
Enter fullscreen mode Exit fullscreen mode

12. MySQL Data Types

Numeric

  • INT
  • BIGINT
  • DECIMAL
  • FLOAT

String

  • CHAR
  • VARCHAR
  • TEXT
  • JSON

Date/Time

  • DATE
  • DATETIME
  • TIMESTAMP

13. Views

CREATE VIEW active_users AS
SELECT id, name FROM users WHERE active = 1;
Enter fullscreen mode Exit fullscreen mode

14. Stored Procedures

DELIMITER $$

CREATE PROCEDURE GetUsers()
BEGIN
  SELECT * FROM users;
END$$

DELIMITER ;
Enter fullscreen mode Exit fullscreen mode

15. Triggers

CREATE TRIGGER before_insert_users
BEFORE INSERT ON users
FOR EACH ROW
SET NEW.created_at = NOW();
Enter fullscreen mode Exit fullscreen mode

16. Functions

CREATE FUNCTION square(x INT)
RETURNS INT
RETURN x * x;
Enter fullscreen mode Exit fullscreen mode

17. Replication

Types

  • Source–Replica
  • Asynchronous
  • Semi-synchronous

Used for:

  • Read scaling
  • Backup
  • High availability

18. MySQL Security

Authentication

  • Users & privileges
CREATE USER 'app'@'%' IDENTIFIED BY 'password';
GRANT SELECT, INSERT ON db.* TO 'app'@'%';
Enter fullscreen mode Exit fullscreen mode

Encryption

  • SSL/TLS
  • Data at rest encryption (InnoDB)

19. Performance Optimization

Best Practices

  • Proper indexing
  • Avoid SELECT *
  • Use EXPLAIN
  • Tune buffer pool
EXPLAIN ANALYZE SELECT * FROM users WHERE id = 1;
Enter fullscreen mode Exit fullscreen mode

20. MySQL Backup & Recovery

Logical Backup

mysqldump db > backup.sql
Enter fullscreen mode Exit fullscreen mode

Restore

mysql db < backup.sql
Enter fullscreen mode Exit fullscreen mode

21. MySQL vs PostgreSQL (Quick)

Feature MySQL PostgreSQL
Speed Fast reads Complex queries
JSON Good Excellent
Extensions Limited Powerful

22. When to Use MySQL?

Use MySQL if:

  • You need high performance
  • Simple relational data
  • Web applications
  • Large ecosystem support

Conclusion

MySQL is not just a simple database—it is a deep, highly optimized, production-grade system with:

  • A layered architecture
  • Sophisticated internals
  • Pluggable storage engines
  • Full transactional support
  • Proven scalability

Understanding its architecture and internals allows you to write faster queries, safer systems, and scalable applications.

Top comments (0)