Ten PHP advanced engineering classic interview questions

1. How to implement PHP to issue cookies to clients without using its own cookie function. For distributed systems, how to save the session value.

This question is a little convoluted. The basic knowledge of cookie and SESSION was also tested. The server notifies the client to save the cookie through the set cookie command.

Just use the header function according to the rules such as domain path expiration time.

Distributed system session, centralized processing. According to our company's architecture, in order to achieve high availability and high disaster tolerance, a distributed signature verification service is provided. See redis's distributed service architecture for details.

2. The user ID is stored in the database, and there are many lines of fee deduction. The user's wallet is stored in redis. Now we need to write a script to synchronize the fee deduction records in the database to redis and execute it every 5 minutes. What issues should be considered?

Idea: producer and consumer model. This question does not mention other states, such as will the data in the database increase in real time? Whether there are other services reading or writing in each wallet in REDIS. Something. When the database and REDIS are put together, either the data consistency or the lock occurs, resulting in reduced efficiency.

3. According to the access.log file, the qps in the last 5 seconds are counted and displayed in the following format, 01 1000 (the difficulty lies in the 01 serial number)  

tail -f access.log | awk -F '[' '{print $2}' | awk '{print $1}' | uniq -c

4. How to synchronize redis, how to synchronize, how to roll back synchronization, and how to deal with data exceptions. At the same time, you will ask MYSQL about the synchronization method and related exceptions

Simple principle of master-slave synchronization in redis cluster

Redis's replication function is based on the persistence strategy of memory snapshot, that is, no matter what your persistence strategy is, as long as redis's replication function is used, memory snapshot will occur.

After Slave is started and connected to the Master, it will actively send a SYNC command (first, the Master will start a background process and save the data snapshot to the file [rdb file] and the Master will send a SYNC command to Slave

Ping command to judge the survival status of the Slave. When the Slave is alive, the Master will send the data file to the Slave and send all write commands to the Slave).

Slave will first save the data file locally and then load the data into memory.

When you connect for the first time or reconnect after a failure, you will first judge the survival status of the Slave, synchronize all data, and then only synchronize the write operation of the Master (send the command to the Slave)


When the Master synchronizes data, if the amount of data is large and the Master itself will only enable one background process to synchronize multiple Slave, the Master will be under too much pressure and the recovery time of Slave will be very slow!

Advantages of redis master-slave replication:

(1) In a Redis cluster, the master is responsible for writing requests and the slave is responsible for reading requests. On the one hand, this greatly reduces the pressure on the master server by distributing the read requests to other machines. On the other hand, the slave focuses on providing
Read service, which improves response and read speed.

(2) In a Redis cluster, if the Master goes down, the slave can intervene and replace the master. Therefore, the whole Redis service will not be unable to provide services, which makes the whole Redis service safe enough.

(3) Increasing the level of Slave machines can improve performance

5. If one of the two mysql servers hangs, how can the business end switch senselessly and ensure that the data of the podium server is consistent under normal circumstances

If it is not a core business, stop writing first, pull up the standby machine, check the logs of the two machines, compensate the data and start writing.

If it is the core business, now all write operations are on the normal machine. Pull up the standby machine of the good machine as the host.

The above are all emergency operations. In fact, the disaster recovery design of database is much more complex.

If the interviewer asks you what to do if the data of the standby machine is inconsistent, you should bravely accept it. How many write operations do you have per second. According to the million level table, the write efficiency is 1000 per second. The normal design is distributed on two machines, 500 per machine. For this level of data synchronization, the probability of difference is negligible. One has a problem, and the other can resist.

(for normal operation, stop writing first, switch and start writing when the data is consistent. Our company makes these switches around 4:00 a.m., and there are only a dozen write operations per second in the core business. The delay is less than 20 seconds).

6. Please write down at least three methods or functions for intercepting file name suffixes (both PHP native functions and self implemented functions)

echo substr(strrchr($file, '.'), 1);
echo substr($file, strrpos($file, '.')+1);
$arr=explode('.', $file);
echo $arr[count($arr)-1];
$arr=explode('.', $file);
echo end($arr);
echo strrev(explode('.', strrev($file))[0]);
echo pathinfo($file)['extension'];
echo pathinfo($file, PATHINFO_EXTENSION);

7. Write a function to get all the pictures in the content of an article and download them

function download_images($article_url = '', $image_path = 'tmp'){
    // Get article class content
    $content = file_get_contents($article_url);
    // Using regular expressions to get picture links
    $reg_tag = '/<img.*?\"([^\"]*(jpg|bmp|jpeg|gif|png)).*?>/';
    $ret = preg_match_all($reg_tag, $content, $match_result); 
    $pic_url_array = array_unique($match_result1[1]);
    // Create path
    $dir = getcwd() . DIRECTORY_SEPARATOR .$image_path;
    mkdir(iconv("UTF-8", "GBK", $dir), 0777, true);
    foreach($pic_url_array as $pic_url){
        // Get file information
        $ch = curl_init($pic_url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_NOBODY, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE );
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $fileInfo = curl_exec($ch);
        $httpinfo = curl_getinfo($ch);
        // Get picture file suffix
        $ext = strrchr($pic_url, '.');
        $filename = $dir . '/' . uniqid() . $ext; 
        // Save picture information to file
        $local_file = fopen($filename, 'w');
        if(false !== $local_file){
            if( false !== fwrite($local_file, $filecontent) ){

10 bottles of water, one of which is poisonous. After drinking the poisonous water, the mice will die 24 hours later. Q: at least a few mice can find out which bottle of water is poisonous after 24 hours.


Binary problem. Schrodinger's mouse.

A mouse has two states, life and death, corresponding to 01. Assuming that the number of mice is a, there is 2 ^ a > = 10; A=4;

The idea is very simple. The number of ten bottles of medicine is 0,1,10,11.... 1001;

0 don't drink. The first mouse drank all bits of 1: 13579, the second drank ten bits of 1, the third and hundred bits of 1, and the fourth drank thousands of bits of 1.

After 24 hours, look, the dead is 1 and the alive is 0. Stand up in the order of mice... If the first and third are dead, it's 0101, that's 5 there's a problem.

Tags: PHP

Posted on Wed, 06 Oct 2021 22:04:19 -0400 by serverman