Why would the count be lost under high concurrency when the visit volume is written to the database in real time each time, and what does the aggregation rewrite solve?
Why does the real-time writing of access volume cause the loss of counting under high concurrency? The reason lies in this writing method, which is 'read-modify-write': first read the current value, add one and write it back. Two requests read the same old value at the same time, add one after writing back, and the number written to the database is the same, and the increment is overwritten once. The higher the concurrency, the greater the probability of overlapping reads, and the lost are not just a few sporadic ones.
What section is lost?
The lost is the increment, not the entire data block. It is manifested as the statistical number being significantly lower than the actual request volume in the logs, and the deviation expands with the increase in traffic; it is not noticeable on a single page, and aggregated reports may show situations such as 'Today's visits are less than the details'.
The second type of loss is more concealed: accounts are settled without being written to disk. The memory has already marked this batch of increments as processed, but it has not been written to persistent storage yet. At this point, if the process exits or the machine restarts, this increment will be permanently lost. In the public version records, some people have written these two things into the same fix: PbootCMS recorded in V3.2.21 that 'changed the real-time writing of access volume to reliable increment aggregation backwriting, and fixed the problem of increment loss and settlement without being written to disk in concurrent scenarios.'
What did the aggregation backwriting change?
Aggregate rewrite changes 'write once per access' to 'accumulate a segment of increment and write once in batch'. The effect has two layers: first, the number of write times decreases, and the contention of hot rows is reduced; second, read-modify is moved out of the request path and modified to accumulate in a single-threaded or controlled task, possibly eliminating the overwritten data.
After the modification, still need to pay attention to three things.
| Link | The problems to be solved | Common practice |
|---|---|---|
| Accumulate | Concurrent overwrite on the request path | Increment first into memory or queue, do not modify the line directly in the request |
| Disk write | Charge off without persistence | Write successfully before clearing the buffer, replay on failure |
| Recovery | Where did the increment received after the crash go | Keep the data waiting to be rewritten and the queue position that can be replayed |
| Search | Detailed and summary口径 are inconsistent | The settlement is based on a fixed time point, and the report indicates the cut-off time |
The order is very important: first write to disk, then settle accounts. Otherwise, it will return to the old problem of not writing to disk and settling accounts
How to divide the storage tier
Statistical writes should be separated from business tables. High-frequency append data such as access details and spider logs, if挤在同一个存储里and business tables are stored in the same storage, high-frequency writes will drag down the overall performance, and additional risks will occur during concurrent directory creation or the first write. Place log data in independent text or dedicated storage, segmented by time, and only keep transactionally guaranteed data in business tables, with no interference between the two types of reads.
The crawling traffic within the site should be separated from the real visitors. The anti-crawling and interference codes are aimed at the batch transfer of content, and the criteria include visitor behavior characteristics; mixing this traffic with the normal crawling of search engines into a single number will already lead to an inaccurate statistical scope.
How should the official carrying scope be read?
The official statement of AnQiCMS is that the page loading speed is significantly improved compared to traditional PHP CMS, and it can handle about 5 million PV on a single machine, with memory usage reduced by about 80% compared to PHP CMS. These numbers are approximations, not benchmarking indicators: they describe the level of difference under the same resources and cannot be directly converted to 'how many concurrent connections' or 'how many times'.
What truly determines whether the statistical write can withstand it is the other links in this chain: how many read requests are blocked by the cache, how much write amplification the database has, and how often the aggregation task runs. When you see any number of 'how much a single machine can bear,' you should also ask what other variables are on the same chain.
Common questions
Should the statistics be precise to each visit?It depends on the purpose. For operational trends, you can just look at the aggregated values; for counting involving billing or settlement, there must be a detailed source that can be accounted for, not just relying on memory accumulation.
What are the problems with putting log and transaction data together?High-frequency addition will amplify the write pressure and lengthen the transaction, and the retention period and cleaning methods of the two types of data are also different. It is better to execute the strategies separately after they are separated.
Will the push and statistics affect each other?They will share the exit bandwidth and queue. The link push and active push should be queued according to the crawling rhythm to avoid competing for the same resource with high-frequency statistical writing.