Database Connection Management
Applications communicate with MySQL databases through a dedicated MySQL driver that establishes TCP/IP connections. Rather than creating new connections for each request, connection pooling maintains reusable connections to avoid the overhead of repeated connection establishment and teardown.
Popular Java connection pools include Druid, C3P0, and DBCP. MySQL itself maintains a server-side connection pool to efficiently handle concurrent requests.
Request Processing Architecture
MySQL processes incoming requests using dedicated threads. The execution flow involves several key components:
SQL Interfcae
The initial entry point that receives SQL statements from client connections and routes them for processing.
Query Parser
Translates human-readable SQL in to internal MySQL representations. For example:
SELECT user_name, user_age, user_gender FROM user_data WHERE user_id = 1001;
The parser validates syntax and converts this in to executable instructions.
Query Optimizer
Determines the most efficient execution plan by calculating costs:
- I/O Cost: Disk-to-memory data transfer (typically 1 per page)
- CPU Cost: Data processing operations (typically 0.2 per row)
The optimizer selects indexes and join strategies that minimize total cost.
Storage Engine Operations
Buffer Pool Management
InnoDB maintains a Buffer Pool that caches frequently accessed data pages in memory. When processing an update:
UPDATE user_profiles SET profile_name = 'Updated Name' WHERE profile_id = 2002;
The storage engine:
- Checks Buffer Pool for the target data
- Loads from disk if not present
- Applies exclusive locks during modification
Transaction Logging
Undo Logging
Records pre-modification data state to enable transaction rollback. Essential for maintaining ACID compliance.
Redo Logging
InnoDB-specific physical logging that records post-modification states. Provides crash recovery capability through write-ahead logging.
Redo log buffering and flushing behavior is controlled by innodb_flush_log_at_trx_commit:
- 0: No immediate disk flush
- 1: Synchronous disk write
- 2: OS cache buffering
Binary Logging
MySQL's binary log (binlog) provides logical logging for replication and point-in-time recovery. Key characteristics:
| Aspect | Redo Log | Binary Log |
|---|---|---|
| Scope | InnoDB-specific | MySQL-level |
| Content | Physical changes | Logical operations |
| Format | Circular writing | Append-only |
| Purpose | Crash recovery | Replication/Recovery |
Binlog flushing is controlled by sync_binlog:
- 0: OS buffered
- 1: Synchronous disk write
Binlog Formats
Statement-based: Records SQL statements (minimal logging but potential replication inconsistencies)
Row-based: Records row changes (verbose but precise)
Mixed: Hybrid approach combining both methods
Transaction Commit Process
During commit, MySQL performs atomic operations:
- Flushes redo log buffer to disk
- Writes to binary log
- Records binlog position in redo log
- Adds commit marker to redo log
This ensures durability even if crashes occur between writes.
Data Synchronization
A background thread periodically flushes dirty pages from Buffer Pool to disk, maintaining consistency between memory and persistent storage.