Analysis of some answers to “interview questions for recruitment of PHP interns in Shanghai in the spring of 2019”


1. How does nginx achieve load balancing

This is still relatively simple

1. Polling

This is the default policy. Each request is assigned to different servers one by one in order. If the server fails, it can be automatically rejected.

upstream {   

2. Minimum connection

Assign requests to the server with the least number of connections

upstream {   

3. Weight

Use weight to specify the server access ratio, which is 1 by default. The following configuration will make server2 access twice as much as Server1.

upstream {   
    server weight=1; 
    server weight=2;  


Each request will be allocated according to the hash value of the access IP, so that the continuous web requests from the same client will be distributed to the same server for processing, which can solve the problem of session. If the server fails, it can be automatically eliminated.

upstream {   

Ip_ Hash can be used in combination with weight.

2. Linux commands

I won’t say much about it

3. Components commonly used in wechat applets

View, text, button, navigator, scroll view, etc

4. How does nginx configure a virtual host

EN 23333333

5. TP5 and laravel framework differences

EN 23333333

6. Data migration in TP5 and laravel framework

This community has documentation

7. Explanation of RBAC model

What is RBAC

RBAC (role based access control): English nameRose base Access Controller。 This blog introduces the authority system design of this model. The direct association between user and permission is cancelled, and the user’s permission is given indirectly through the method of user associated role and role associated permission. So decoupling is realized. RBAC is divided into the following versions in the development process. RBAC0、RBAC1、RBAC2、RBAC3。

8. Process of order module

9. Operation after successful order payment

10. Set the expiration time of mailbox activation connection

When the activation code is written to the database, the timestamp + expiration time is written

11. The difference between redis and mongodb

1. Performance

Both are relatively high. Performance should not be a bottleneck for us.

In general, redis is similar to Memcache in TPS.

2. Convenience of operation

Redis has added its own VM features after version 2.0, breaking through the limitation of physical memory; you can set the expiration time for key value (similar to Memcache).

Mongodb is suitable for the storage of large amount of data. It relies on the VM of the operating system for memory management. It also eats more memory. Services should not be combined with other services.

4. Availability (single point problem)

For single point problems:

Redis relies on the client to realize distributed read-write. During master-slave replication, every time the slave node reconnects the master node, it depends on the entire snapshot, and there is no incremental replication. Due to performance and efficiency problems, the single point problem is relatively complex. It does not support automatic sharding and needs to rely on the program to set a consistent hash mechanism.

One alternative is to do active replication (multiple copies of storage) by itself, or change to incremental replication (which needs to be implemented by ourselves) instead of the replication mechanism of redis itself, and the consistency problem and performance tradeoff can be achieved.

Mongodb supportmaster-slave,replicaset(Paxos election algorithm is used internally, and automatic fault recovery is adopted). Auto sharding mechanism shields the client from the mechanism of fault transfer and segmentation.

5. Reliability (persistence)

For data persistence and data recovery, redis supports (snapshot, AOF): depending on snapshot for persistence, AOF enhances reliability while affecting performance. Mongodb has adopted binlog mode to support the reliability of persistence since version 1.8.

6. Data consistency (transaction support)

Redis transaction support is relatively weak, which can only guarantee the continuous execution of each operation in the transaction. Mongodb does not support transactions.

7. Application scenarios

Redis: more performance operation and operation with less data

Mongodb: mainly to solve the problem of access efficiency of massive data

12. Difference between redis and memcached

13. Queues in redis

There are two ways to implement the redis queue

1. Producer consumer model.

Normal version:

For example, in a queue, producer a pushes a data, and consumer B pops the data, and the queue is still empty. So it’s one-on-one.

As for FIFO or FIFO, the functions lpush (from the left side of the queue, that is, the first queue to push a data) rpush (from the right side of the queue, that is, push a data at the end of the queue) lpop (similarly) rpop can be used to control.

Blocked version:

However, the above commands are returned immediately. No matter whether there is data or not, there is an enhanced version of lpop for fetching data, blpop (block left pop) blocking version,

How to use: blpop key1 key2… Keyn 10

At the same time, get the values of multiple keys in advance, and set the timeout time to 10s. If all keys have value, some keys will return immediately. If all keys have no value, return will be blocked for 10 seconds

2. Publisher subscriber mode.


Three users a, B and C subscribe to a channel named MSG at the same time, and then the publisher publishes a data in the MSG channel. Then, users a, B and C will receive the data.


Obviously, three users ABC need to be blocked. How to receive subscription data depends on the callback function registered in redis.

The released data will not be reproduced in redis, which means that after the release, a, B, C have not received it for various reasons.

14. Data types in redis

Redis supports five data types: string (string), hash (hash), list (list), set (set) and Zset (sorted set: ordered set).

15. Events in TP framework

16. Dependency injection of TP framework

It’s no different from laravel

17. Read write separation of MySQL

What master-slave replication, what bin log, a lot of nonsense

18. The difference between varchar and char

Varchar reclaims unused space

19. Differences between MyISAM and InnoDB

1. MyISAM: the default table type, which is based on the traditional ISAM type. ISAM is the abbreviation of indexed sequential access method, which is the standard method for storing records and files. It is not transaction safe and does not support foreign keys. If you execute a large number of select, insert MyISAM is more suitable.

2. InnoDB: an engine that supports transaction security. It supports foreign keys, row locks and transactions. If there are a large number of updates and inserts, InnoDB is recommended, especially for multiple concurrent and high QPS situations.

1、 Table lock difference


MyISAM only supports table level locking. When users operate the MyISAM table, select, update, delete, and insert statements will automatically lock the table. If the locked table meets the insert concurrency, new data can be inserted at the end of the table. You can also use the lock table command to lock the table. This operation can mainly simulate transactions, but the consumption is very large, and it is only used in the experimental demonstration.

InnoDB :

InnoDB supports transaction and row level locking, which is the biggest feature of InnoDB.

Acid attributes of transaction: atomicity, consistent, isolation, durable.

Several problems caused by concurrent transactions: update loss, dirty read, non repeatable read, unreal read.

2、 Database file differences


MyISAM belongs to heap table

MyISAM has three files on disk storage. Each file name begins with a table name, and the extension indicates the file type.

. frm is used to store the definition of the table

. MyD is used to store data

. MYI is used to store table indexes

The MyISAM table also supports three different storage formats:

Static table (default, but note that there should be no space at the end of the data, which will be removed), dynamic table, and compressed table.

InnoDB :

InnoDB belongs to the index organization table

InnoDB has two storage modes: shared table space storage and multi table space storage

The table structure of the two storage methods is the same as that of MyISAM, which starts with the table name and has an extension of. Frm.

If a shared table space is used, the data files and index files of all tables are saved in a single table space, and a table space can have multiple filesinnodb_data_file_pathandinnodb_data_home_dirThe parameter sets the location and name of the shared table space. Generally, the name of the shared table space is ibdata1-n.

If multiple table spaces are used, each table has a table space file to store the data and indexes of each table. The file name starts with the table name and has an extension of. IBD.

3、 Index differences

1. About automatic growth

The automatic growth column of the MyISAM engine must be an index. If it is a composite index, the automatic growth can not be the first column. It can be sorted and incremented according to the previous columns.

The automatic growth of InnoDB engine must be an index, and if it is a composite index, it must also be the first column of the composite index.

2. About primary keys

MyISAM allows tables without any indexes and primary keys,

The index of MyISAM is the address where the line is saved.

If the InnoDB engine does not set a primary key or a non empty unique index, it will automatically generate a 6-byte primary key (invisible to the user)

InnoDB data is a part of the primary index, and the additional index stores the value of the primary index.

3. On the count() function

MyISAM stores the total number of rows in the table, ifselect count(*) from table;This value will be taken out directly

InnoDB does not save the total number of table rows, if you useselect count(*) from table;The whole table will be traversed, and the consumption is quite large. However, after adding the where condition, MyISAM and InnoDB handle it in the same way.

4. Full text index

MyISAM supports fulltext type full-text indexing

InnoDB does not support fulltext type full-text indexing (already supported since 5.6), but InnoDB can use Sphinx plug-in to support full-text index, and the effect is better. Sphinx is an open source software, which provides API interface of many languages, and can optimize various queries of MySQL.

5、delete from table

When you use this command, InnoDB will not create a new table, but delete data one by one. If you want to empty a table with a large amount of data on InnoDB, you’d better not use this command. (truncate table is recommended, but the user should have permission to drop this table).

6. Index save location

The index of MyISAM is saved as table name +. MYI file respectively.

InnoDB indexes and data are stored in the table space together.

20. How many indexes are available in MySQL

1、 General index

The most basic index only speeds up the query speed.

2、 Unique index

Similar to the normal index, the difference is: the column value of the index must be unique, but null value is allowed. If it is a composite index, the combination of column values must be unique.

3、 Primary key index

It is a special unique index. It does not allow null values. Generally, the primary key index is created at the same time when the table is created.


1) A table has only one primary key index
2) The primary key should be increased automatically

4、 Composite index

That is, the index established by multiple fields

5、 Full text index


MyISAM engine support

6、 Foreign key

Precautions for creating foreign keys:

1) The table engine must be the same
2) Field types must be the same
3) The length must be the same
4) The storage range must be the same
5) The constraint field must have been present in the referenced field


This work adoptsCC agreementThe author and the link to this article must be indicated in the reprint

By: larravel China