I. background
At present, most of the systems we develop need to record business logs (including but not limited to operation logs). For example, when an electronic commerce sufficient item is checked out, the relevant purchase information is required, you should record the payment information, because you may have to reconcile with the customer one day in the future. for another example, everyone transfers funds to a bank at ordinary times. The bank must keep the information related to your transfers in the back of the Bank, such as the transfer account, transfer time, and transfer amount, transfer to account and so on. banks often say how long it will take (for example, 5 minutes) to settle the account. If not, you can submit a ticket to the bank for reconciliation.
Ii. Status quo
At the beginning of the design, most systems use the Log action as a synchronization record. Below we will describe it with a pseudo code.
Using (transcation ){
Book. Pay (); // purchase and pay for a book
Operate. Add (); // Add a transaction log.
}
Iii. Problem Analysis
1. If a connection is used, the transaction will be a local transaction to ensure maximum performance,That is, if n objects save data together (n> = 2), a distributed transaction will occur, and the performance will be reduced by more than 10 times.The response time also slows down.
2.The data connection under concurrent access opens two connections in this simplest transaction, one for book and one for operate. consider.. NET data connection pool is 100 by default. If you only consider this simple business operation, strong concurrency can only support simultaneous access by 50 customers.
3. Network Access: database access is a very time-consuming operation. The execution time of a normal transaction is doubled, and the customer experience is inefficient.
4. Each business operation must be called, log functions,As a result, logs cannot be written in batches, resulting in poor performance.
5.Business logs may be stored in the same database as the production database, which is not conducive to maintenance, query optimization, and operations such as data partitions when the data volume is huge. This is not conducive to submitting the production database performance.
Iv. Solutions
1. Two data writing methods are supported. One is synchronous log writing, that is, the previous method, the other is asynchronous log, which is a packaging of synchronous logs.
2. asynchronous logs use a log file to ensure asynchronous writing. For example, the log file is operate. log. when the system calls a log, the asynchronous LOG method writes the log to operate. log file, and then return.
3. There is an independent thread in the background that reads all log files every X seconds and calls the batch write method in the synchronization log class to write data to the log database.
4. The pseudocode is as follows:
Using (transcation ){
Book. Pay (); // purchase and pay for a book
Operate. aysadd (); // Add a transaction log asynchronously.
}
Aysadd (){
Filestream stream = file. Open (PATH );
Stream. writeline (); // asynchronously write a log file
}
Threadproc (){
While (true ){
Filestream stream = file. Open (PATH );
Stream. readallline (); // synchronously reads files
Operate. batchsave (); // save log information in batches
Thread. Sleep (n); // sleep for n seconds, continue to save data in batches
}
}
V. Solution Analysis and Assurance System
1. If it is not stored synchronously, what should I do if my system is powered off or IIS restarts? A: when instantiating the log class, operate. log is read first and saved to the database to ensure that the log files are not lost.
2. What if write failure occurs during file writing? A: As mentioned here, asynchronous logs are a packaging of the Synchronization Model and a typical decorator mode. if the file fails to be written, the data will be saved in synchronization mode without any business data loss.
3. Do you have synchronous or asynchronous write? What if the file is written asynchronously? If you are writing logs synchronously, what is the performance improvement compared with the previous one?
A: 1. synchronous write of log files, that is, you must ensure that log files can be written to text files before committing the entire transaction.
2. This problem occurs only when data is written asynchronously. The log file is lost due to sudden power failure.
3. A performance test is conducted to write 1,000,000 service logs per second to files, which greatly improves system performance. submit log files in batches to the database, share a connection, and form a batch of SQL statements, which can save performance consumption by an order of magnitude.