


How does innodb_flush_log_at_trx_commit affect performance and durability?
Apr 06, 2025 am 12:07 AMThe value of innodb_flush_log_at_trx_commit determines how InnoDB handles the redo log flash operation: 1. When the value is 1, the disk is flushed every transaction commit to ensure the highest data persistence, but may affect performance. 2. When the value is 0, refresh it every second to improve performance but may lose the last second data. 3. When the value is 2, it is written to the operating system cache. The performance is between the first two, but there is still a risk of data loss.
introduction
In the journey of database tuning, the configuration parameter innodb_flush_log_at_trx_commit
of the InnoDB storage engine is undoubtedly a key road sign. This parameter not only affects the performance of the MySQL database, but also determines the persistence and reliability of the data. Through this article, I will take you to explore the mechanism of action of innodb_flush_log_at_trx_commit
, and share some experiences I encountered in actual projects and pitfalls to help you better understand and apply this parameter.
Review of basic knowledge
As MySQL's default storage engine, InnoDB provides transactional and crash recovery capabilities, which mainly relies on its logging system. The transaction log (redo log) records transactional modifications to data to ensure that the database can be restored to a consistent state after a database crash. innodb_flush_log_at_trx_commit
controls when these logs are written to disk.
Core concept or function analysis
Definition and function of innodb_flush_log_at_trx_commit
innodb_flush_log_at_trx_commit
is a configuration parameter that determines how InnoDB handles the redo log flush operation when the transaction is committed. It has three values: 0, 1 and 2.
Value is 1 : This is the default setting for InnoDB. The redo log is written to disk every time the transaction is committed, ensuring the highest persistence of the data. This setting can also have a certain impact on performance while ensuring data security.
Value is 0 : In this setting, the redo log will refresh to disk once a second in the background thread. This approach improves performance, but if the database crashes, data from the last second may be lost.
Value 2 : The redo log will write to the operating system cache every time the transaction is committed, and the operating system will flush it to disk regularly. This approach is somewhere between the first two, providing a certain performance improvement, but there is still a risk of data loss.
How it works
When a transaction is committed, InnoDB writes the redo log to the buffer. According to the value of innodb_flush_log_at_trx_commit
, InnoDB decides how to process the data of these buffers:
Value 1 : InnoDB will call
fsync()
to ensure that the redo log is written to disk. This method ensures that data is not lost when the database crashes or the operating system crashes.Value 0 : InnoDB does not refresh the redo log to disk immediately, but instead relies on background threads to refresh once every second. This method reduces the frequency of I/O operations, thereby improving performance.
Value is 2 : InnoDB will write redo log to the operating system's file cache and rely on the operating system's scheduling to decide when to flush data to disk. This method reduces I/O operations compared to a value of 1, but may still lose data when the operating system crashes.
Example of usage
Basic usage
Setting innodb_flush_log_at_trx_commit
in MySQL configuration file is very simple:
[mysqld] innodb_flush_log_at_trx_commit = 1
This line configuration sets innodb_flush_log_at_trx_commit
to 1, ensuring that the redo log is written to disk every time the transaction is committed.
Advanced Usage
In practical applications, we may need to dynamically adjust this parameter according to different business needs and performance requirements. For example, in a batch task, the parameter can be temporarily set to 0 or 2 to increase processing speed:
SET GLOBAL innodb_flush_log_at_trx_commit = 0; -- Execute batch task--Restore settings after the task is completed. GLOBAL innodb_flush_log_at_trx_commit = 1;
This way allows for flexible adjustments to the balance between performance and persistence without restarting the database.
Common Errors and Debugging Tips
Data Loss : When
innodb_flush_log_at_trx_commit
is set to 0 or 2, data may be lost if the database or operating system crashes. It is recommended to back up data regularly when set to 0 or 2 and switch back to 1 before critical operations.Performance Issues : If set to 1, frequent transaction commits may cause I/O bottlenecks. In this case, batch committing transactions may be considered, or using higher performance storage devices.
Performance optimization and best practices
In actual projects, I once encountered an e-commerce platform's order processing system. Due to the huge order volume and frequent transaction submissions, performance bottlenecks have been caused. We significantly improve the system throughput while maintaining acceptable persistence of data by adjusting innodb_flush_log_at_trx_commit
from 1 to 2 and combining with the method of committing transactions in batches.
Performance comparison : Through testing, we found that adjusting
innodb_flush_log_at_trx_commit
from 1 to 2 can reduce I/O operations by about 50%, thereby improving the system's response speed.Best practice : When selecting the value of
innodb_flush_log_at_trx_commit
, business requirements, data security and performance requirements need to be comprehensively considered. For high concurrency and high throughput applications, 2 or 0 can be considered, but when processing critical data, it is recommended to use 1 to ensure absolute security of the data.
Through the discussion of this article, I hope you can have a deeper understanding of innodb_flush_log_at_trx_commit
, and flexibly use it in actual projects to find the best balance between performance and persistence.
The above is the detailed content of How does innodb_flush_log_at_trx_commit affect performance and durability?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Common Database Performance Problems and Optimization Methods in Linux Systems Introduction With the rapid development of the Internet, databases have become an indispensable part of various enterprises and organizations. However, in the process of using the database, we often encounter performance problems, which brings troubles to the stability of the application and user experience. This article will introduce common database performance problems in Linux systems and provide some optimization methods to solve these problems. 1. IO problem Input and output (IO) is an important indicator of database performance and is also the most common

The limitations of MySQL technology: Why is it not enough to compete with Oracle? Introduction: MySQL and Oracle are one of the most popular relational database management systems (RDBMS) in the world today. While MySQL is very popular in web application development and small businesses, Oracle has always dominated the world of large enterprises and complex data processing. This article will explore the limitations of MySQL technology and explain why it is not enough to compete with Oracle. 1. Performance and scalability limitations: MySQL is

How to use MySQL indexes rationally and optimize database performance? Design protocols that technical students need to know! Introduction: In today's Internet era, the amount of data continues to grow, and database performance optimization has become a very important topic. As one of the most popular relational databases, MySQL’s rational use of indexes is crucial to improving database performance. This article will introduce how to use MySQL indexes rationally, optimize database performance, and provide some design rules for technical students. 1. Why use indexes? An index is a data structure that uses

RocksDB is a high-performance storage engine, which is the open source version of Facebook RocksDB. RocksDB uses technologies such as partial sorting and sliding window compression, and is suitable for a variety of scenarios, such as cloud storage, indexing, logs, caching, etc. In actual projects, RocksDB caching technology is usually used to help improve program performance. The following will introduce RocksDB caching technology and its applications in detail. 1. Introduction to RocksDB caching technology RocksDB caching technology is a high-performance cache

InnoDBBufferPool improves the performance of MySQL databases by loading data and index pages into memory. 1) The data page is loaded into the BufferPool to reduce disk I/O. 2) Dirty pages are marked and refreshed to disk regularly. 3) LRU algorithm management data page elimination. 4) The read-out mechanism loads the possible data pages in advance.

Database performance optimization skills: Comparison between MySQL and TiDB In recent years, with the continuous growth of data scale and business needs, database performance optimization has become the focus of many enterprises. Among database systems, MySQL has always been favored by developers for its wide application and mature and stable features. TiDB, a new generation of distributed database system that has emerged in recent years, has attracted much attention for its powerful horizontal scalability and high availability. This article will discuss the two typical database systems, MySQL and TiDB.

MySQL's performance under high load has its advantages and disadvantages compared with other RDBMSs. 1) MySQL performs well under high loads through the InnoDB engine and optimization strategies such as indexing, query cache and partition tables. 2) PostgreSQL provides efficient concurrent read and write through the MVCC mechanism, while Oracle and Microsoft SQLServer improve performance through their respective optimization strategies. With reasonable configuration and optimization, MySQL can perform well in high load environments.
