The previous articles mentioned memcache and TT. memcache memory databases are well understood. data is stored in the memory, and data is lost when the server is restarted or the memcahce process is restarted. So we also mentioned that ttserver data is permanently stored. is its data absolutely secure?
Next let's test it in detail. Start two ttservers on the test server without logging.
12 |
ttserver -port 1978 -dmn -pid /data/ttserver/test1/pid /data/ttserver/test1/casket.tch ttserver -port 1979 -dmn -pid /data/ttserver/test2/pid /data/ttserver/test2/casket.tch |
According to the test method on the Internet: Write 10000 pieces of data to two ports respectively, kill-9 PID drop process, and then start ttserver to check whether the data is lost and no loss is found, it can be determined that the ttserver will not lose data when it crashes unexpectedly.
Run the following code to write 50000 data records to the ttserver on the two ports:
<?php
$mem=new Memcache();
$mem->addServer ("127.0.0.1",1979,false,1,100);
$mem->addServer ("127.9.9.1",1978,false,1,100);
$start=microtime(true);
for($i=0;$i<50000;$i++){
$mem->add("test_".$i,"INFO 127122222 a b c d{....}");
}
print_r($mem->getExtendedStats());
echo microtime(true)-$start;
?>
During the execution, unplug the machine, restart the machine, and then start ttserver. We found that ttserver on both ports only contains 16000 pieces of data (my test results ).
Therefore, ttserver crashes unexpectedly. For example, if the process is killed-9, data will not be lost. If the system loses power, data will be lost. This should be because ttserver only writes data to the file system and is not flushed to the disk.
How to solve the problem (refer to online materials)
The ttserver actually provides a synchronization command sync, which can flush data to the disk. However, every call will cause a sharp decline in performance. In order not to reduce performance and ensure data reliability, logs can be recorded to recover data from logs when an unexpected power failure occurs. You can also use master-slave replication, master data loss, Slave Data is still (recommended), or you can regularly execute the sync command.
In fact, this principle is very similar to that of the MySQL database transaction acid principle at the isolation level. TC supports asynchronous writing mechanism, that is, the written data may not be flushed to the disk, and synchronization can be performed after certain conditions (such as the interval. The advantage of Asynchronization is fast, and the disadvantage is that some data may be lost in an emergency. Most write methods of TC are synchronous, and asynchronous data is written to the disk during the operation.
If the data is not particularly large (generally, the memory size should not exceed the limit. According to the operating system, a 32-bit operating system cannot exceed 2 GB for a single file, and a 64-bit operating system is not limited)
Note:Serialization Problems
In practical applications, the memcache protocol is more commonly used to use ttserver. When we retrieve data from it, it will be a serialized string without automatic deserialization, but it is automatically deserialized on the memcached server. In this way, we first extract the data, such:a:1:{s:5:"value";s:14:"this is a value";}
And then use the unserialize () function for deserialization.