Home > Article > Backend Development > Analysis of some answers to "2019 Xiaomi Spring Shanghai PHP Intern Recruitment Interview Questions"
Related recommendations: "2019 PHP interview questions summary (collection)"
1 丶How to achieve load balancing in Nginx
This is relatively simple
1. Polling
This is the default strategy, which allocates each request to a different server one by one in order. , if the server hangs up, it can be automatically eliminated.
upstream fengzp.com { server 192.168.99.100:42000; server 192.168.99.100:42001; }
2. Minimum connections
Assign requests to the server with the fewest connections
upstream fengzp.com { least_conn; server 192.168.99.100:42000; server 192.168.99.100:42001; }
3. Weight
Use weight to specify the server access ratio. The default weight is 1. The following configuration will cause the access ratio of server2 to be twice that of server1.
upstream fengzp.com { server 192.168.99.100:42000 weight=1; server 192.168.99.100:42001 weight=2; }
4.ip_hash
Each request will be allocated according to the hash value of the access ip, so that consecutive web requests from the same client will be distributed to the same server for processing , which can solve the session problem. If the server hangs up, it can be automatically eliminated.
upstream fengzp.com { ip_hash; server 192.168.99.100:42000; server 192.168.99.100:42001; }
ip_hash can be used in combination with weight.
2 丶Commonly used commands in Linux
Reference article:https://www.php.cn/linux-415502.html
3 丶Commonly used components in WeChat mini programs
view 丶text 丶button 丶navigator 丶scroll-view...etc.
4丶How to configure virtual host with Nginx
Reference article:https://www.php.cn/php-weizijiaocheng-387454.html
5 Differences between TP5 and Laravel framework
Reference article:https://www.php.cn/phpkj/thinkphp/422769.html
6丶Data migration in TP5 and Laravel framework
Reference article:
https://www.php.cn/js-tutorial-386843 .html
https://www.php.cn/phpkj/laravel/414216.html
7 丶Explanation of RBAC model
What is RBAC
RBAC (role-based access control): English name Rose base Access Controller. This blog introduces the permission system design of this model. The direct association between users and permissions has been cancelled, and user permissions have been granted indirectly through user-associated roles and role-associated permissions. This achieves decoupling. RBAC is divided into the following versions during its development process. RBAC0, RBAC1, RBAC2, RBAC3.
8 丶Process of order module
9 丶Operations after successful order payment
10 丶Settings Email activation connection expiration time
When writing the activation code into the database, write the timestamp expiration time
11 丶The difference between Redis and Mongodb
1. Performance
is relatively high, and performance should not be a bottleneck for us.
Generally speaking, redis and memcache are similar in terms of TPS.
2. Convenience of operation
redis added its own VM features after version 2.0, breaking through the limitations of physical memory; you can set the expiration time for the key value ( Similar to memcache).
mongoDB is suitable for the storage of large amounts of data. It relies on the operating system VM for memory management and consumes a lot of memory. The service should not be combined with other services.
4. Availability (single point problem)
For single point problem:
redis relies on the client to implement distributed reading and writing; main When replicating from a slave, each time the slave node reconnects to the master node, it must rely on the entire snapshot. There is no incremental replication. Due to performance and efficiency issues, the single-point problem is more complicated; automatic sharding is not supported, and a consistent hash mechanism needs to be set by the program.
An alternative is to use redis's own replication mechanism and use active replication (multiple storage), or change to incremental replication (which needs to be implemented by yourself). Consistency issues and performance issues trade off.
mongoDB supports master-slave, replicaset (using paxos election algorithm internally, automatic failure recovery), auto sharding mechanism, and shields the failover and sharding mechanism from the client.
5. Reliability (persistence)
For data persistence and data recovery, redis supports (snapshot, AOF): relies on snapshots for persistence, aof enhancement While improving reliability, it also has an impact on performance. MongoDB has adopted the binlog method since version 1.8 to support persistence reliability.
6. Data consistency (transaction support)
redis transaction support is relatively weak and can only ensure that each operation in the transaction is executed continuously. MongoDB does not support transactions.
7. Application scenarios
redis: more performance operations and calculations with smaller amounts of data
MongoDB: Mainly solves the problem of access efficiency of massive data
12 丶The difference between redis and memcached
Reference article:https://www. php.cn/mysql-tutorials-410551.html
13 丶Queue 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 into it, and consumer B pops the data, but the queue is still empty. So it's one to one.
As for first-in-first-out or first-in-last-out, etc., you can follow the function lpush (push one data from the left side of the queue, that is, the head of the queue) rpush (push one data from the right side of the queue, that is, the tail of the queue) lpop (same as management) rpop etc. to control.
Blocking version:
But the above command returns immediately, regardless of whether there is data or not. There is an enhanced version of lpop for fetching data, blpop (block left pop) blocking version,
Usage: blpop key1 key2 ... keyn 10
Pre-obtain the values of multiple keys at the same time, and set the timeout to 10s. If all keys or some keys have values, they will be returned immediately. If If all keys have no value, block for 10 seconds and return
2. Publisher-subscriber mode.
Concept:
Three users A, B, and C subscribe to a channel named msg at the same time, and then the publisher publishes a data to the channel of msg, then A ,Three users B and C will receive the data.
Note:
Obviously, three users ABC need to be blocked. How to receive subscribed data must rely on the callback function registered in redis.
The published data will not be reproduced in redis, which means that after it is published, A, B, and C did not receive it due to 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 the TP framework
16 丶The dependency injection of the TP framework
is no different from Laravel
17 丶MySQL read-write separation operation
Reference article:https://www.php.cn/mysql-tutorials-360278.html
18 丶The difference between database varchar and char
varchar will reclaim unused space
19 丶The difference 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 (indexed sequential access method). It is a 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 selects, insert MyISAM is more suitable.
2. InnoDB: an engine that supports transaction security. Supporting foreign keys, row locks, and transactions is its biggest feature. If there are a large number of updates and inserts, it is recommended to use InnoDB, especially for multiple concurrency and high QPS situations.
1. Table lock differences
MyISAM:
myisam only supports table-level locks. When users operate myisam tables , select, update, delete, and insert statements will automatically lock the table. If the locked table meets insert concurrency, new data can be inserted at the end of the table. You can also lock the table through the lock table command. This operation can mainly simulate transactions, but it is very expensive and is generally only used in experimental demonstrations.
InnoDB:
Innodb supports transactions and row-level locks, which is the biggest feature of innodb.
ACID properties of transactions: atomicity, consistent, isolation, durable.
Several problems caused by concurrent transactions: lost updates, dirty reads, non-repeatable reads, and phantom reads.
2. Database file differences
MyISAM:
myisam belongs to the heap table
myisam is on the disk There are three files on the storage, each file name starts with the table name and the extension indicates the file type.
.frm is used to store table definitions
.MYD is used to store data
.MYI is used to store table indexes
myisam tables also support three Different storage formats:
Static table (default, but please note that there must be no spaces at the end of the data, it will be removed), dynamic table, and compressed table.
InnoDB:
innodb is an index-organized table
innodb has two storage methods, shared table space storage and multi-table space storage
The table structure of the two storage methods is the same as myisam, starting with the table name and the extension is .frm.
If you use a shared table space, then the data files and index files of all tables are stored in one table space. One table space can have multiple files. Set the location and location of the shared table space through the innodb_data_file_path and innodb_data_home_dir parameters. Name, generally the name of the shared table space is ibdata1-n.
If multiple table spaces are used, then 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 myisam engine must be an index. If It is a combined index. The automatic growth does not need to be the first column. It can be sorted and then incremented based on the previous columns.
The automatic growth of the innodb engine must be an index. 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 to exist.
Myisam’s indexes are all addresses where rows are 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)
The innodb data is part of the primary index, appended The index holds the value of the main index.
3. About the count () function
myisam saves the total number of rows in the table. If count(*) from table; is selected, the value will be taken out directly
innodb does not save the total number of rows in the table. If you use select count(*) from table; it will traverse the entire table, which consumes a lot of money. However, after adding the where condition, myisam and innodb handle it in the same way.
4. Full-text index
myisam supports full-text index of FULLTEXT type
innodb does not support full-text index of FULLTEXT type (has been supported since 5.6) , but innodb can use the sphinx plug-in to support full-text indexing, and the effect is better. (sphinx is an open source software that provides API interfaces in multiple languages and can optimize various mysql queries).
5.delete from table
When using this command, innodb will not re-create the table, but delete the data one by one. If you want to clear it on innodb It is best not to use this command when saving tables with large amounts of data. (It is recommended to use truncate table, but the user needs to have the permission to drop this table).
6. Index saving location
The indexes of myisam are saved in .MYI files with table names.
Innodb indexes and data are stored together in the table space.
20 丶There are several indexes in MySQL
1. Ordinary index
The most basic index is just to speed up improve query speed.
2. Unique index
is similar to an ordinary index, except that the column value of the index must be unique, but null values are allowed, that is, null. If Combination index, the combination of column values must be unique.
3. Primary key index
is our commonly used primary key id. It is a special unique index that does not allow null values. It is usually created at the same time when creating a table. Create primary key index.
Features:
1) A table has only one primary key index
2) The primary key requires auto-increment
4. Combined index
That is, an index established by multiple fields
5. Full-text index
fulltext
myisam engine support
6. Foreign keys
Things to note when establishing foreign keys:
1) The table engines must be the same
2) The 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 appear in the referenced field
The above is the detailed content of Analysis of some answers to "2019 Xiaomi Spring Shanghai PHP Intern Recruitment Interview Questions". For more information, please follow other related articles on the PHP Chinese website!