Java Autumn Job Interview for Server-side Development 11

After working hard for so many years, looking back, it is almost all long setbacks and sufferings. For most people's life, smooth sailing is only occasional, and frustration, unbearable, anxiety and confusion are the main theme. We take the stage we did not choose, play the script we did not choose. Keep going!

Table of contents

1. What is the specific implementation process of MySQL's multi-version concurrency control? How to ensure repeatable reading?

2. Redis expired key cleanup strategy? Elimination strategy for redis insufficient memory cache?

3. The search process of the jump list? How to adjust after inserting and deleting elements?

4. Distributed transactions?

5. In addition to using three-way handshake and four-way wave to ensure reliability, what else can TCP guarantee reliability?

6. Is it okay to merge the four waves of TCP into three?

7. https encryption process? How to obtain the public key, private key, and symmetric key?

8. What is the difference between non-repeatable read and phantom read?

9. What is the underlying principle of the string type and zset type of redis?

10. What aspects should be considered in the test of the registration login page?

11. How does MySQL solve phantom reading?

12. MySQL sub-database sub-table problem?

13. Why wave four times and wait for 2MSL?

14. Let's talk about process scheduling? Talk about the process scheduling algorithm?

15. Symmetric encryption and asymmetric encryption?

16. Cookie and session?

17. Tell me about the familiar design patterns?

18. Handwritten DCL, by the way, why do you write it this way?

19. Tell me about the underlying implementation principles of volatile and synchronized?

20. What methods are there under the Object class?

21. Talk about redis cluster?

22. How to control the file size of file upload and download?

23. How to deploy front-end and back-end projects in practical applications?

24. How to maintain the consistency of database and redis?

25. Tell me about several working modes of RabbitMQ?

26. Tell me about netty, do you know the difference between NIO, BIO, and AIO?

27. What are the methods of communication between threads?

28. Tell me about springcloud?

29. What did the Mathematical Modeling National Competition do? Specifically talk about the details? something innovative?

30. Tell me about the details of the quick sort algorithm?

31. What is the most impressive article on your personal blog? Tell me about the details?


1. What is the specific implementation process of MySQL's multi-version concurrency control? How to ensure repeatable reading?

Reference link: Detailed explanation of MVCC, simple and easy to understand-CSDN blog

Mvcc, that is, multi-version concurrency control, is a means to improve reading efficiency and concurrency without locking when reading data. The read mentioned by mvcc is a snapshot read , which is an ordinary select statement . Snapshot reads do not need to be locked when reading and writing, but historical data may be read. Another way to read data is current read , which is a pessimistic lock operation. It will lock the currently read data, so the read data is the latest .

Snapshot reads under read committed and repeatable read isolation levels are all implemented based on MVCC!

 The implementation of mvcc is based on undolog , version chain and readview .

In the data stored by mysql, in addition to the fields we explicitly define, mysql will implicitly define several fields for us.

  • trx_id: transaction id, which will be incremented by 1 every time a transaction operation is performed.

  • roll_pointer: rollback pointer, used to find the previous version of data, combined with undolog for rollback

First look at readview:

When we use select to read data, there will be many versions of the data at this moment, but we don't know which version to read. At this time, we rely on readview to limit the version we read. Only through readview can we know ourselves Which version can be read .

A readview snapshot mainly contains the following four fields:

m_ids: Active transactions refer to transactions that have not been committed yet.

max_trx_id: For example, the transaction id in m_ids is (1, 2, 3), then the next transaction id that should be allocated is 4, and max_trx_id is 4.

creator_trx_id: The id of the transaction that executes the select read operation.

 How mvcc implements the isolation level of RC and RR:

(1) Under the isolation level of RC, each snapshot read will generate and obtain the latest readview.

(2) Under the isolation level of RR, readview is created only in the first snapshot read of the same transaction, and the same readview is used for each subsequent snapshot read, so each query result is the same.

Phantom reading problem:
Snapshot reading: Through mvcc, the isolation level of RR solves the problem of phantom reading, because the same readview is used every time.
Current read: Through next-key lock (row lock + gap lock), the RR isolation level cannot solve the phantom read problem.
 

2. Redis expired key cleanup strategy? Elimination strategy for redis insufficient memory cache?

Redis uses two strategies, lazy expiration and regular expiration, to clean up expired keys.

  1. Lazy Expiration: When an expired key is accessed, Redis checks to see if the key has expired and deletes it if so. The advantage of this strategy is that it can save CPU time and memory, but the disadvantage is that there may be a large number of expired keys that have not been cleaned up in time.

  2. Periodic expiration: By default, Redis checks whether a batch of random keys are expired every 1 second, and deletes them if any expired keys are found. The advantage of this strategy is that it can ensure that expired keys are cleaned up within a certain period of time, but the disadvantage is that some CPU time and memory may be wasted, because not all expired keys need to be cleaned up at this time.

Redis provides 8 out-of-memory cache elimination strategies.

noeviction: When the memory usage exceeds the configuration, an error will be returned, and no keys will be evicted.

allkeys-lru: When adding a key, if the limit is exceeded, the key that has not been used for the longest time is first evicted through the LRU algorithm.

volatile-lru: If the limit is exceeded when adding a key, the key that has not been used for the longest time is first evicted from the key set with an expiration time set.

allkeys-random: If the limit is exceeded when adding a key, it will be randomly deleted from all keys.

volatile-random: If the limit is exceeded when adding a key, it will be randomly evicted from the collection of expired keys.

volatile-ttl: Evict keys that are about to expire from keys configured with an expiration time.

volatile-lfu: Evict the least frequently used key from all keys configured with an expiration time.

allkeys-lfu: Evict the least frequently used key from all keys.

These eight types can be roughly divided into 4 types, lru, lfu, random, and ttl.

lru: Least Recently Used), the least recently used, the priority is to eliminate the data that has not been used recently. The core idea is "if the data has been accessed recently, the probability of being accessed in the future will be higher."

lfu: Least Frequently Used, the least frequently used method (according to the counter, the key with the least number of times is eliminated).

ttl: Time To Live, time to live, those that are about to expire will be eliminated first.

random: random.

3. The search process of the jump list? How to adjust after inserting and deleting elements?

Jump list, also known as jump list, jump list, adds the function of "jump" on the basis of ordered linked list.
The jump table adds a multi-level index to the original ordered linked list, and quickly searches through the index; it can support fast deletion, insertion and search operations. The jump list is actually a linked list with forward pointers added, which is a randomized data structure. SortedSet in Redis and MemTable in LevelDB both use jump tables. Compared with the balanced tree, the implementation and maintenance of the skip list will be simpler, and the average time complexity of searching, deleting, and adding the skip list is O(logn).

The nature of the skip table:

The jump list is composed of many layers, and the level is randomly generated by a certain probability;
each layer is an ordered linked list, and the default is ascending;
the bottom (Level 1) linked list contains all elements;
if an element appears in In the linked list of Level i, the linked list under Level i will also appear;
each node contains two pointers, one points to the next element in the same linked list, and the other points to the element of the lower layer.
 

The search process of the jump table:

  1. Starting from the first element of the top-level linked list, search from left to right until an element greater than or equal to the target is found, or the end of the current level linked list is reached.
  2. If the element is equal to the target element, the element has been found.
  3. If the element is greater than the target element or has reached the end of the linked list, return to the previous element of the current layer, and then go to the next layer to search.

The insertion process of the skip table element:

  • When we continuously insert data into the jump table, if we do not update the index, there may be a lot of data between two index nodes. In extreme cases, the jump table will degenerate into a singly linked list.
  • The jump table maintains "balance" through a random function. When we insert data in the jump table, we insert this data into some index layers at the same time by selecting, how to select the index layer, we can use a random function to determine which levels of indexes this node is inserted into, such as randomly generated k, then add this index to the index from the first level to the kth level.

The deletion process of the skip table:

        When deleting a node in the jump list, if this node also appears in the index, we not only need to delete the node in the original linked list, but also delete the node in the index. Because the deletion operation in the singly linked list needs to get the predecessor node of the deleted node, and then complete the deletion through pointer operation. So when looking for the node to be deleted, be sure to get the predecessor node (except for the doubly linked list). Therefore, the time complexity of the deletion operation of the skip list is O(logn).
 

4. Distributed transactions?

Reference Blog: Detailed Explanation of Seven Common Distributed Transactions (2PC, 3PC, TCC, Saga, Local Transaction Table, MQ Transaction Message, Best Effort Notification) - Zhang Weipeng's Blog - CSDN Blog

Transaction is to ensure the data consistency of a single database, and distributed transaction is to ensure the data consistency of multiple databases.

Distributed transaction : In a distributed system, an operation needs to be completed by multiple services. This kind of transaction completed by different services through the network is called a distributed transaction. Seven common distributed transactions (2PC, 3PC, TCC, Saga, local transaction table, MQ transaction message, best effort notification)

1) 2PC, two-phase commit, divides the transaction commit process into two stages : resource preparation and resource commit , and the transaction coordinator coordinates all transaction participants. If all transaction participants reserve resources successfully in the preparation stage, then Commit the resource in the second phase, otherwise the transaction coordinator rolls back the resource.

2) 3PC, the three-phase commit protocol, is an improved version of the two-phase commit protocol. There are two changes in the three-phase commit:

  • (1) Introduce a timeout mechanism in both the coordinator and the participant
  • (2) A preparation phase is inserted between the first phase and the second phase to ensure that the state of each participating node is consistent before the final submission phase.

3) TCC (Try Confirm Cancel) is a two-stage commit at the application layer, so it is highly intrusive to the code. Its core idea is: for each operation, the corresponding confirmation and compensation operations must be implemented, that is, every part of the business logic Each branch needs to implement the three operations of try, confirm, and cancel. In the first stage, the business code is arranged to call the Try interface to reserve resources. When the Try interface of all participants succeeds, the transaction coordinator submits the transaction and calls the participating The confirm interface of the participant actually submits the business operation, otherwise the cancel interface of each participant is called to roll back the transaction, and since confirm or cancel may be retried, the corresponding part needs to support idempotence.

4) The core idea of ​​a Saga transaction is to split a long transaction into multiple local short transactions and submit them normally in sequence. If all short transactions are executed successfully, then the distributed transaction is submitted; if a participant fails to execute a local transaction, the The Saga transaction coordinator coordinates and calls the compensation operation in reverse order, rolls back the committed participants, and returns the distributed transaction to the original state. The basic protocol of Saga transaction is as follows:

(1) Each Saga transaction consists of a series of idempotent ordered sub-transactions (sub-transaction) Ti.
(2) Each Ti has a corresponding idempotent compensation action Ci, which is used to undo the result caused by Ti.
        Compared with the TCC transaction compensation mechanism, TCC has a reserved (Try) action, which is equivalent to first submitting a draft and then submitting it; Saga transactions have no reserved action and are submitted directly

5) The core idea of ​​the local message table is to split distributed transactions into local transactions for processing. There are two main roles in this scheme: transaction active party and transaction passive party. The active initiator of the transaction needs to create an additional transaction message table, complete the business processing and record the transaction message in the local transaction, and poll the data of the transaction message table to send the transaction message, and the transaction passive party consumes the transaction in the transaction message table based on the message middleware .

        In this way, data inconsistency caused by the following two situations can be avoided: business processing succeeds, transaction message sending fails, business processing fails, transaction message sending succeeds.

6) The MQ-based distributed transaction scheme is essentially an encapsulation of the local message table, and the overall process is consistent with the local message table. The only difference is that the local message table is stored inside the MQ instead of the business database.

7) Best effort notification, also known as periodic proofreading, is a further optimization of the MQ transaction scheme. It adds an interface for message collation on the active side of the transaction. If the passive side of the transaction does not receive the message sent by the active side, it can call the interface of message collation provided by the active side of the transaction to actively obtain it.

5. In addition to using three-way handshake and four-way wave to ensure reliability, what else can TCP guarantee reliability?

In addition to TCP's three-way handshake and four-way handshake, TCP also uses the following mechanisms to ensure the reliability of data transmission:

  1. Data Checksum (Checksum): Each TCP segment contains a 16-bit checksum field, which is used to detect whether the data is wrong or tampered with during transmission.

  2. Window Control: TCP uses the sliding window protocol for flow control and congestion control to ensure that the data transmission speed between the sender and the receiver matches, avoiding data loss and network congestion.

  3. Timeout and Retransmission: When the data packet is not confirmed or lost, TCP will start the timeout retransmission mechanism and resend the unconfirmed data packet until it gets the confirmation information from the other party.

  4. Flow Control (Flow Control): TCP uses a sliding window mechanism for flow control to ensure that the sender does not send too much data that the receiver cannot process. When the receiver's window size is 0, the sender needs to wait for the receiver's window to become larger before sending data.

  5. Congestion Control: Slow start, congestion avoidance, fast retransmission, and fast recovery. TCP uses the congestion window mechanism for congestion control. When the network is congested, it will reduce the sending speed of the sender to avoid congestion.

    Slow Start (Slow Start), Congestion Avoidance (Congestion Avoidance), Fast Retransmit (Fast Retransmit) and Fast Recovery (Fast Recovery) are the four components of the TCP congestion control algorithm. Slow start: When starting to send data, TCP will start the slow start algorithm, every time a round trip time RTT (Round Trip Time) passes, the congestion window cwnd will double. This can quickly occupy network bandwidth and speed up data transmission. Congestion avoidance: When the congestion window cwnd reaches a threshold ssthresh, TCP will stop slow start and enter the congestion avoidance state. At this time, every time an RTT passes, the congestion window cwnd will only increase by 1, and the data transmission rate will be slowly increased to avoid network congestion. Fast retransmission: When the sender receives 3 repeated ACK confirmation messages in a row, it means that the receiver has lost some data packets, and TCP will immediately retransmit these data packets without waiting for timeout retransmission. Fast recovery: When the sender performs fast retransmission, it will halve the congestion window cwnd and set the threshold ssthresh to half of the current congestion window. However, in order to avoid wasting network resources, TCP will try to recover quickly, that is, immediately enter the congestion avoidance state after restoring the congestion window cwnd, and continue to send data.

Through the cooperation of the above mechanisms, TCP can ensure the reliability of data transmission, and can adapt itself to adjustments when the network environment changes, improving the efficiency and reliability of data transmission.

6. Is it okay to merge the four waves of TCP into three?

In some cases, a TCP four-wave can be turned into a TCP three-wave.

When the server "has no data to send" and "enables the TCP delayed confirmation mechanism" during the TCP waving process, then the second and third waving will be combined for transmission, so there will be three wavings.
 

7. https encryption process? How to obtain the public key, private key, and symmetric key?

HTTPS (also known as HTTP Secure) is a secure version of the HTTP protocol that encrypts communication content using SSL/TLS.

The HTTPS encryption process is as follows:

  1. The client initiates an HTTPS request to the server, and the request contains a random number and a list of supported encryption algorithms.

  2. The server responds to the client's request, returning its own certificate and public key , as well as the selected encryption algorithm.

  3. After the client receives the response from the server, it first verifies the legality and validity of the certificate, and then obtains the public key of the server from the certificate .

  4. The client uses the corresponding algorithm to generate a random symmetric encryption key, encrypts it with the public key of the server, and then sends the encrypted symmetric key to the server.

  5. The server receives the encrypted symmetric key sent by the client, decrypts it with the private key, and obtains the symmetric key.

  6. The server uses a symmetric key to encrypt the data to be sent to the client and sends it to the client.

  7. The client receives the encrypted data sent by the server, uses the symmetric key to decrypt it, and obtains the plaintext data.

How the client and server obtain the public key, private key and symmetric key are as follows:

  • Obtain certificate and public key: The server needs to apply for and obtain a digital certificate, including its public key and identity information. Certificates can be obtained from a Certificate Authority (CA) or other trusted source. After the client receives the certificate from the server, it extracts the public key from the certificate.

  • Obtaining the private key: The server can obtain the private key from the certificate to generate and keep the private key properly. The private key is only owned by the server and is used to decrypt the symmetric key encrypted by the client.

  • Obtain a symmetric key: the client and the server negotiate and generate a random number as a symmetric key, and the client generates the symmetric key. And use asymmetric encryption algorithm (such as RSA) for encryption and decryption to realize the safe transmission of symmetric key.

In encrypted communication, the client needs to generate keys for encrypting and decrypting data. There are generally the following methods for generating keys:

  1. Random number generation method: The client can use a pseudo-random number generator (PRNG) to generate a sequence of random numbers and generate a symmetric key based on the sequence. This method is simple and fast, but has low security and is vulnerable to attacks.

  2. Cryptographic hash function method: the client can use a cryptographic hash function (such as SHA-256) to hash a random number or password to obtain a digest value as a symmetric key. This method has high security and unpredictability, but has high computational complexity.

  3. Key exchange protocol method: The client can negotiate with the server through a key exchange protocol (such as the Diffie-Hellman protocol) to generate a shared symmetric key. This method is a method with a large amount of calculation and high security, and is widely used in network encrypted communication.

  4. Other methods: The client can also use other methods to generate keys, such as random number generators based on physical properties (such as thermal noise, radioactive decay, etc.), or combine multiple methods to generate keys to improve security and unpredictability .

In short, key generation is an important link in encrypted communication, and the client needs to choose an appropriate method to generate a safe and reliable key.

8. What is the difference between non-repeatable read and phantom read?

Non-repeatable reads and phantom reads are both problems in database transaction concurrency control, but they occur for different reasons. Phantom reading and non-repeatable reading both read another transaction that has been committed (this is different from dirty reading). The difference is that non-repeatable reading queries the same data item, while phantom reading targets a batch The data as a whole (such as the number of data).

Non-repeatable read refers to reading the same row of data multiple times in a transaction, but the results obtained for each read are different . This is due to other concurrent transactions modifying the row of data between the transaction's two reads .

Phantom reading means that during the execution of a transaction, the result sets obtained by multiple queries are different , that is, "phantom" data appears. This is because other concurrent transactions insert new eligible data between the two queries of the transaction. due to row data .

In short, non-repeatable reads are caused by update operations, while phantom reads are caused by insert or delete operations.

9. What is the underlying principle of the string type and zset type of redis?

The underlying data structures of Redis's string type and zset type are different.

The underlying implementation of the String type is Simple Dynamic String (SDS), which is similar to the character array in C language, but supports automatic expansion and provides some string manipulation functions. In Redis, the value of string type can be a string or a number, and supports multiple operations, such as setting, getting, appending, and auto-incrementing.

The Zset type is an ordered collection based on the Skip List, which is a data structure that can take into account both search efficiency and insertion efficiency. In Redis, the value of zset type is an ordered list of strings/numbers, each element has a score (score), and operations such as sorting, range search, and deletion can be performed according to the score.

In short, the underlying data structures of the string and zset types of Redis are SDS and jump table respectively, each of which provides efficient storage and access methods for different scenarios and operations.

10. What aspects should be considered in the test of the registration login page?

Generally, tests are carried out from functions, performance, security, usability, compatibility, interface, and localization.

1. Functional test

1. Enter the correct user name and password, click the submit button, and verify whether you can log in correctly;

2. If you enter a wrong user name or password, the verification login will fail, and a corresponding error message will be prompted;

3. Can you jump to the correct page after successful login;

4. Check whether you can choose a different login method to log in (such as using a mobile phone number, WeChat scan code, etc.);

5. When there is a verification code, it is necessary to consider whether the text is difficult to recognize, and whether the color, refresh or change buttons are easy to use;

6. After successful login, whether to remember the user name and password function;

7. After the login fails, the password cannot be remembered;

8. When entering the password, whether there is a prompt message when the uppercase keyboard is turned on;

9. Whether the password is not displayed in plain text, whether it is replaced by symbols such as asterisks or dots;

10. Are the links on the login page such as registration, forgot password, logout and login with another account correct;

11. Enter nothing, click the submit button, and check the prompt information;

2. Performance test

1. Open the login page, check whether the required time is within the required time;

2. After entering the correct user name and password, check whether the time when the login is successful and jumps to the new page is within the required time;

3. Simulate a large number of users logging in at the same time, and check whether the login and jump can be performed normally under certain pressure;

3. Safety test

1. Whether the cookie generated after successful login is httponly (otherwise it is easy to be stolen by the script);

2. Whether the user name and password are encrypted and sent to the server;

3. The user name and password input boxes should shield SQL injection attacks;

4. In the input box of user name and password, the input script should be prohibited (to prevent XXS attack);

5. Prevent brute force cracking and detect whether there is a time limit for wrong login;

6. Whether to support multiple users to log in on the same machine;

7. Can the same user log in on multiple machines;

4. Usability testing

1. Whether it can be fully operated with the keyboard, and whether there are shortcut keys;

2. Enter the user name and password and press Enter to check whether you can log in;

5. Compatibility test

1. Can the display and function be normal under different browsers;

2. Can the display and function be normal under different versions of the same browser;

3. Whether different operating systems can display and function normally;

4. Whether the display can be normal and the function is normal under different resolutions;

6. Interface testing

1. Whether the interface layout is reasonable, whether the textbox and buttons are neat;

2. Whether the length and height of the textbox and buttons meet the requirements;

3. Whether the design style of the interface is consistent with the design style of the UI;

4. Whether the pictures, colors, fonts, and hyperlinks are all displayed correctly;

7. Localization testing

1. Whether the display of the page is correct under different language environments;
 

11. How does MySQL solve phantom reading?

The default isolation level adopted by MySQL is repeatable read. Under this isolation level, different read modes adopt different solutions to the phantom read problem:

Snapshot reads do not need to be locked when reading and writing, but historical data may be read.

Another way to read data is current read , which is a pessimistic lock operation. It will lock the currently read data, so the read data is the latest .

For snapshot reads (ordinary select statements), phantom reads are resolved through MVCC.
For the current read (select ... for update and other statements), the phantom read is solved through the next-key lock (record lock + gap lock).
However, it is emphasized that MySQL does not completely solve the problem of phantom reading at the level of repeatable reading, especially in the scenario where snapshot reading and current reading of a transaction are used interspersed, phantom reading still occurs .

12. MySQL sub-database sub-table problem?

Let's first look at why we need to divide the database and table:

Taking MySQL as an example, the performance of a single database is better when the amount of data is less than 50 million. After the threshold is exceeded, the performance will decrease significantly as the amount of data increases. If the data volume of a single table exceeds 1000w, the performance will also drop seriously . This will lead to a longer query time, and when the number of concurrent operations reaches a certain amount, it may be stuck, and even the system will be dragged down.

​Can we improve data processing capabilities by improving server hardware capabilities? Yes, but this solution is very expensive, and there is an upper limit to improving the hardware. Then can we disperse the data in different databases, so that the data volume of a single database and table can be reduced, so as to achieve the purpose of improving the performance of the database operation? Yes, this is database sub-database sub-table.

​Splitting databases and tables is to split larger databases and data tables according to a certain strategy . The purpose is to: reduce the amount of data in each library and each table, reduce the burden on the database, improve the efficiency of the database, and shorten the query time. In addition, because the transformation of sub-database and sub-table is controllable, and the underlying layer is still based on RDBMS, the operation and maintenance system of the entire database and related infrastructure are reusable.
 

The way of sub-database sub-table:

Vertical table split: Distribute the fields of a table into multiple tables, and each table stores a part of the fields. The fields of a wide table can be split into multiple tables according to the principles of access frequency, business coupling, and whether it is a large field. This can not only make the business clear, but also improve some performance. After splitting, try to avoid joint checking from a business perspective, otherwise the performance will outweigh the gains.
Vertical sub-database: Classify the tables according to the business and distribute them to different databases. Each database can be placed on a different server, so as to achieve the effect of multiple servers sharing the pressure . Multiple tables can be classified according to business coupling and tightness, and stored in different libraries. These libraries can be distributed on different servers, so that the access pressure is loaded by multiple servers, greatly improving performance, and at the same time improving the business clarity of the overall architecture. Different business libraries can customize optimization schemes according to their own conditions. But it needs to solve all the complexities that come with crossing libraries.
Horizontal sub-database: Split the data of the same table into different databases according to certain rules, and each database can be placed on a different server. The data of a table (according to the data row) can be divided into multiple different libraries, and each library has only part of the data of this table. These libraries can be distributed in different servers, so that the access pressure is loaded by multiple servers, and the performance is greatly improved. It not only needs to solve all the complex problems brought by cross-library, but also solve the problem of data routing.
Horizontal table splitting: In the same database, the data in the same table is split into multiple tables according to certain rules. It is possible to divide the data of one table (by data row) into multiple tables of the same database, and each table only has part of the data of this table. This can improve performance slightly, and it is only used as a supplementary optimization of horizontal database partitioning.

13. Why wave four times and wait for 2MSL?

MSL Maximum Segment Lifetime

Two reasons: 1) Ensure that the last ACK segment sent by A can reach B. 2) Prevent "invalid connection request segment" from appearing in this connection.

1. In order to ensure that the last ACK segment sent by the client can reach the server. Because this ACK may be lost, the server in the LAST-ACK state cannot receive the confirmation message for the FIN-ACK. The server will retransmit the FIN-ACK after a timeout, and then the client will retransmit the confirmation again, restarting the waiting timer. Finally, both the client and the server can be shut down normally. Assuming that the client does not wait for 2MSL, but releases the close directly after sending the ACK, once the ACK is lost, the server cannot normally enter the closed connection state.

2. It can also prevent invalid segments. After the client sends the last ACK, after passing through 2MSL, all the message segments generated during the duration of this connection can disappear from the network. From guaranteeing that after the connection is closed, there will be no message segments still lingering in the network to harass the server.

Note: The timeout retransmission timer starts immediately after the server sends the FIN-ACK. The client starts the time-waiting timer immediately after sending the last ACK

14. Let's talk about process scheduling? Talk about the process scheduling algorithm?

The process scheduling algorithm is also called the CPU scheduling algorithm. When the CPU is idle, the operating system selects a process in the ready state from the ready queue according to a certain algorithm, and allocates a CPU to it.

(1) First come first serve scheduling algorithm (FCFS)

The first-come-first-serve scheduling algorithm is a non-preemptive scheduling algorithm. As the name implies, it selects the process that first enters the queue from the ready queue each time, and then keeps running until the process exits or is blocked, and then continues to select the first process from the queue. process then runs.

(2) Short job priority scheduling algorithm

The short job first (SJF) scheduling algorithm is to give priority to the process with the shortest running time to run, which helps to improve the throughput of the system.

(3) Shortest remaining time first algorithm

The shortest remaining time first algorithm (SRTN) is a preemptive version of the shortest job first.

When a new process arrives, compare its total running time with the remaining running time of the current process. If the new process needs less time, suspend the current process and run the new process, otherwise the new process waits.

(4) High Response Ratio Priority Scheduling Algorithm The
High Response Ratio Priority (HRRN) scheduling algorithm mainly balances short jobs and long jobs, because the above first-come-first-serve algorithm and short job priority scheduling algorithm do not balance short jobs well. and long assignments. How does the high response ratio priority scheduling algorithm do it?

Each time a process is scheduled, first calculate the response ratio priority, and then put the process with the highest response ratio priority into operation. The formula for calculating the response ratio priority: priority = (waiting time + required service time) / required service time
priority Right = (waiting time + required service time) / required service time
Priority = (waiting time + required service time) / required service time
(5) time slice round-robin scheduling algorithm

The oldest, simplest, fairest and most widely used algorithm is the time slice round-robin (RR) scheduling algorithm.

(6) Highest priority scheduling algorithm

The priority of all processes in the time slice round-robin scheduling algorithm above is the same. However, for multi-user computer systems, there are different views. They hope that the scheduling has priority, that is, they hope that the scheduler can start from the ready queue. The highest priority process is selected to run, which is called the highest priority (HPF) scheduling algorithm.

(7) Multi-level feedback queue scheduling algorithm

Multilevel Feedback Queue (Multilevel Feedback Queue) scheduling algorithm is the synthesis and development of "Time Slice Round Robin Algorithm" and "Highest Priority Algorithm".

  • "Multi-level" means that there are multiple queues, and the priority of each queue is from high to low, and the higher the priority, the shorter the time slice.
  • "Feedback" means that if a new process joins a high-priority queue, immediately stop the currently running process and switch to running the high-priority queue;

15. Symmetric encryption and asymmetric encryption?

1. Difference: Encryption is generally divided into two types, symmetric encryption and asymmetric encryption. Symmetric encryption uses the same key for encryption and decryption, such as DES, 3DES (TripleDES) and AES.
Asymmetric encryption means that encryption and decryption do not use the same key, such as RSA algorithm, DSA algorithm, ECC algorithm, DH algorithm, etc.
In asymmetric encryption, the secret key used for encryption is called the public key, and the secret key used for decryption is called the private key. Both the public key and the private key are generated in pairs, the public key is distributed to others for encryption, and the private key is used for decryption.
2. Advantages and disadvantages:
Symmetric encryption: fast decryption, but poor confidentiality.
Asymmetric Encryption: The encryption algorithm has good confidentiality, and it eliminates the need for end users to exchange keys. But the speed of encryption and decryption is much lower than that of symmetric encryption.
 

16. Cookie and session?

You can refer to this blog: cookie and session

(1) Session is a mechanism for the server to store data. It has a key-value pair structure and is mainly used to store identity-related information. The session is saved on the server side (the client only saves a sessionID). Cookie is a mechanism for the client (browser) to store data. The key-value pair structure can store identity information and key information. They are all programmers Custom cookies are saved on the client side.

(2) The session saves the object, and the cookie saves the string . The storage limit of cookies is no more than 4KB, while the capacity of sessions is unlimited.

(3) The session cannot distinguish between paths. During the same user accessing the same web application, all sessions can be accessed in any path. If the path parameter is set in the cookie, it cannot be accessed under other paths.

(4) The default method for session to save SeesionID is Cookie. If the client disables Cookie, the server should use other alternative methods to replace SessionID.

(5) The session will be closed after the session ends, but the cookie can be persisted and stored on the local hard disk of the client for a long time. So session is also more secure.

17. Tell me about the familiar design patterns?

Singleton mode, proxy mode, factory mode.

Refer to this blog: Server Development Java Interview Review 1_java server interview_nuist__NJUPT's Blog-CSDN Blog

18. Handwritten DCL, by the way, why do you write it this way?

There are hungry man mode and lazy man mode in the singleton mode. The lazy man mode may have thread safety problems, and it is instantiated by double checking. The singleton mode needs to be instantiated only once during the running of the program. For the first time, it is judged that the object is not initialized, and the lock operation is performed. The scope of the lock is a singleton object. Only one thread can obtain this lock resource at a time to ensure that the instantiation process will not be parallelized.

Let me talk about why the volatile keyword is added, and the object instantiation process:

Apply for memory space
Initialize according to the object type
Memory address assignment object references,
but JVM execution will have instruction reordering, steps 2 and 3 may be executed out of order, no problem will occur under a single machine, multi-threaded situation, A thread finds that it is not instantiated, go to apply Lock instantiation executes to 1 and 3, and thread B starts to judge. At this time, uniqueInstance is not empty, but it has not been initialized. After B thread gets it, it may throw a null pointer.
 

//使用双重检查锁的方式保重线程安全
class Singleton3{
    //使用双重检查进行初始化的实例必须使volatile关键字修饰
    private volatile static Singleton3 instance3 = null ;
    public Singleton3 getInstance3(){
        if(instance3 == null){
            synchronized (Singleton3.class){
                if(instance3 == null){
                    instance3 = new Singleton3();
                }
            }
        }
        return instance3 ;
    }
}

19. Tell me about the underlying implementation principles of volatile and synchronized?

Volatile is a lightweight synchronized that ensures the visibility of shared variables in multiprocessor development. Will not cause thread context switching and scheduling. Both volatile and synchronized are keywords used to achieve multi-thread synchronization in Java, but their underlying implementation principles are different.

1. The underlying implementation principle of volatile

The main function of the volatile keyword is to ensure the visibility of variables and prohibit the rearrangement of instructions, that is, when a thread modifies a variable modified by volatile, other threads can immediately see the latest value. This is because the JVM will insert memory barriers before and after the code that reads and writes volatile variables during compilation. For write operations, it will force the cache to be refreshed and written back to the main memory. For read operations, it will obtain the latest value from the main memory and store the value Flush back into the thread cache.

2. The underlying implementation principle of synchronized

The role of the synchronized keyword is to achieve coordination and mutual exclusion between threads. It consists of two parts: lock acquisition and release. In Java, each object has a monitor lock (monitor). When entering the synchronized block, it will try to acquire the lock. If the lock has already been acquired by other threads, the current thread will block until the lock is released. At the bottom of the JVM, the synchronized block is implemented through the monitorenter and monitorexit instructions, where the monitorenter instruction is used to acquire a lock and the monitorexit instruction is used to release a lock. When a thread executes the monitorenter instruction, if the lock of the object is not acquired by other threads, the thread will set the lock holder to itself and set the counter to 1; if the lock of the object has been acquired by other threads , the thread will be blocked until the lock is acquired. At the same time, the lock holder and the thread queue waiting for the lock are also recorded in the monitor. When a thread finishes executing the code in the synchronized block, it will execute the monitorexit instruction to release the lock and decrement the counter by 1. If the counter is 0, the lock of the object will be released by the keyword of process synchronization, but their underlying implementation principles are different. Volatile guarantees the visibility of variables and prohibits instruction rearrangement, which is realized by inserting memory barriers; while synchronized realizes the coordination and mutual exclusion between threads, and the bottom layer is realized by acquiring and releasing locks.

20. What methods are there under the Object class?

toString() method

        The toString() method can convert any object into a string and return it. The return value generation algorithm is: getClass().getName() + '@' + Integer.toHexString(hashCode()). However, subclasses generally override this method.

equals method

        The equals method in the Object class is used to compare the virtual addresses of two references. The return value is true if and only if the two references are physically the same object, false otherwise. However, subclasses generally override this method.

hashCode method

        Get the hash code value of the object in hexadecimal.

clone method

        The protection method implements the shallow copy of the object. Only when the Cloneable interface is implemented can this method be called, otherwise a CloneNotSupportedException is thrown.

getClass method

        final method to get the class object of the class.

wait method

        Causes the thread to wait until it is woken up by another thread via notify() or notifyAll. This method can only be called from a synchronous method. If the current thread is not the lock holder, this method throws an IllegalMonitorStateException. wait(long timeout) can set a timeout interval. After calling this method, the current thread will go to sleep until the following events occur:

        (1) Other threads call the notify method of the object.

        (2) Other threads call the notifyAll method of the object.

        (3) Other threads call interrupt to interrupt the thread.

        (4) The time interval is up.

        At this point, the thread can be scheduled, and if it is interrupted, an InterruptedException is thrown.

notify method

        This method wakes up a thread waiting on this object. This method can only be called inside a synchronized method or synchronized block. If the current thread is not the lock holder, this method throws an IllegalMonitorStateException.

notifyAll method

        Unblocks all threads calling wait on this object. This method can only be called inside a synchronized method or synchronized block. If the current thread is not the lock holder, this method throws an IllegalMonitorStateException.
 

21. Talk about redis cluster?

The so-called cluster is to provide the same service by increasing the number of servers, so that the servers can reach a stable and efficient state. A single redis is unstable. When the redis service goes down, there is no service available. Moreover, the read and write capabilities of a single redis are limited. The use of redis cluster can strengthen the read and write capabilities of redis, and when one server goes down, other servers can still work normally without affecting the use.

1. In the redis cluster, each redis is called a node.
2. In the redis cluster, there are two types of nodes: master node (master) and slave node (slave).
3. Redis cluster is based on redis master-slave replication.

In the master-slave replication model, there are multiple redis nodes. Among them, there is one and only one master node Master. There can be multiple slave nodes. As long as the network connection is normal, the Master will always synchronize its own data updates to the Slaves to maintain master-slave synchronization.

The master node Master is readable and writable. Read-only from the node Slave. (read-only)
Therefore, the master-slave model can improve the ability to read and ease the ability to write to a certain extent. Because there is still only one master node that can write, all read operations can be handed over to the slave node, which improves the writing ability in disguise.

When the master node goes down, the entire cluster has no writable nodes.
Since all the data of the master node is backed up on the slave node, if the master node is down, if the slave node can be turned into a master node , this problem can be solved. This is what Sentinel does.

Redis's Sentinel system is used to manage multiple Redis servers (instances). The system performs the following three tasks:
Monitoring (Monitoring): Sentinel will constantly check whether your master server and slave server are operating normally.
Notification: When there is a problem with a monitored Redis server, Sentinel can send a notification to the administrator or other applications through the API.
Automatic failover (Automatic failover): When a master server fails to work normally, Sentinel will start an automatic failover operation, which will upgrade one of the slave servers of the failed master server to a new master server, and let the failed master server Other slave servers copy the new master server instead; when the client tries to connect to the failed master server, the cluster will also return the address of the new master server to the client, so that the cluster can use the new master server to replace the failed server.

In sentinel mode, there is still only one Master node. When the concurrent write requests are large, the sentinel mode cannot relieve the write pressure.
Only the master node has the ability to write. If multiple master nodes can be configured in a cluster, the writing pressure can be relieved. This is the redis-cluster cluster mode.

1. A distributed network service cluster composed of multiple Redis servers;
2. There are multiple Master master nodes in the cluster, and each master node can read and write;
3. The nodes will communicate with each other and be connected in pairs;
4. Redis cluster has no central node.

22. How to control the file size of file upload and download?

 In springboot, set the file storage path in the application.yml configuration file (here it is assumed to be in the local E:\ptms), and limit the upload file size (here set to 40M).

23. How to deploy front-end and back-end projects in practical applications?

The front end is deployed to nginx, and the back end is deployed to tomcat.

Refer to the blog: 1. Front-end and back-end project deployment_ Front-end and back-end deployment_I am a rookie, I like blogs of C and V-CSDN blog

24. How to maintain the consistency of database and redis?

Generally, to perform update and delete operations, the cache and database need to be modified to ensure the consistency of the cache and database. Generally, there are four main strategies to consider:

Update the cache first, then update the database; update the database first, then update the cache; delete the cache first, then update the database; update the database first, then delete the cache.

1. If the Redis cache is deleted, another thread reads it before writing to the MySQL database, and finds that the cache is empty, then reads the data from the database and writes it to the cache. At this time, the cache contains dirty data.

2. If the library is written first, before the cache is deleted, the thread that writes the library is down, and the cache is not deleted, data inconsistency will also occur. Because writing and reading are concurrent, there is no way to guarantee the order, and there will be a problem of data inconsistency between the cache and the database.

solution:

The first solution: using a delayed double-deletion strategy

Set the cache expiration time: delete the cache, write to the database, delay, and then delete the cache.

In theory, setting an expiration time for the cache is a solution to ensure eventual consistency. All write operations are subject to the database. As long as the cache expiration time is reached, subsequent read requests will naturally read new values ​​from the database and then backfill the cache.

Disadvantages of the program

Combined with the double deletion strategy + cache timeout setting, the worst case is that the data is inconsistent within the timeout period, and the time-consuming of writing requests is increased.
 

The second solution: update the cache asynchronously (based on the synchronization mechanism of subscribing to binlog)

Read Redis: Hot data is basically written in Redis
Write MySQL: Additions, deletions and changes are all operating MySQL
Update Redis data: MySQ data operates binlog to update to Redis
Redis update

After reading the binlog, analyze it, and use the message queue to push and update the redis cache data of each station.

In this way, once new operations such as writing, updating, and deleting are generated in MySQL, binlog-related messages can be pushed to Redis, and Redis can update Redis according to the records in binlog.

In fact, this mechanism is very similar to the master-slave backup mechanism of MySQL, because the master-slave backup mechanism of MySQL also achieves data consistency through binlog.
 

25. Tell me about several working modes of RabbitMQ?

Rabbitmq has six working modes: simple mode, work mode, publish/subscribe subscription mode, routing mode, topic mode, RPC mode.

The simple mode is a message in a queue, which can only be consumed by one consumer.

The working mode of Work is one producer, multiple consumers, and each consumer gets a unique message.

The publish/subscribe subscription mode is for a message sent by a producer to be obtained by multiple consumers.

The routing routing mode determines which queue to send the message sent by the producer mainly according to the defined routing rules.

The topic mode is a producer, an exchange (topicExchange), fuzzy matching routing rules, multiple queues, and multiple consumers.

The RPC mode is that the client client first sends a message to the message queue, and the remote server server gets the message, then writes it into another message queue, and responds to the original client client with the message processing result.

26. Tell me about netty, do you know the difference between NIO, BIO, and AIO?

Netty is a high-performance, asynchronous event-driven NIO framework. It provides support for TCP, UDP and file transfer. ,作为一个异步NIO框架,Netty的所有IO操作都是异步非阻塞的Through the Future-Listener mechanism, users can easily obtain IO operation results actively or through the notification mechanism.

BIO g (Blocking I/O): synchronous blocking I/O mode, the reading and writing of data must be blocked in a thread and wait for
its completion. When the number of active connections is not particularly high (less than 1000 for a single machine), this model is relatively good,
allowing each connection to focus on its own I/O and the programming model is simple, and there is no need to think too much about system overload , current limiting and other
issues. The thread pool itself is a natural funnel, which can buffer some connections or requests that the system cannot handle. However, when
faced with hundreds of thousands or even millions of connections, the traditional BIO model is helpless. Therefore, we need a more efficient
I/O processing model to cope with higher concurrency.

 NIO (New I/O): NIO is a synchronous non-blocking I/O model. The NIO framework was introduced in Java 1.4, corresponding to the java.nio package, which provides Channel, Selector, Buffer and other abstractions. N in NIO can be understood as Non-blocking, not simply New. It supports buffer-oriented, channel-based I/O operations. NIO provides two different socket channel implementations, SocketChannel and ServerSocketChannel corresponding to Socket and ServerSocket in the traditional BIO model, and both channels support blocking and non-blocking modes. The use of blocking mode is just like traditional support, which is relatively simple, but the performance and reliability are not good; the non-blocking mode is just the opposite. For low-load, low-concurrency applications, you can use synchronous blocking I/O to improve development speed and better maintainability; for high-load, high-concurrency (network) applications, you should use NIO's non-blocking mode for development.
AIO (Asynchronous I/O): AIO is NIO 2. NIO 2, an improved version of NIO, was introduced in Java 7, which is an asynchronous and non-blocking IO model. Asynchronous IO is implemented based on the event and callback mechanism, that is, the application will return directly after the operation, and will not be blocked there. When the background processing is completed, the operating system will notify the corresponding thread to perform subsequent operations. AIO is the abbreviation of asynchronous IO. Although NIO provides a non-blocking method in network operations, the IO behavior of NIO is still synchronous. For NIO, our business thread is notified when the IO operation is ready, and then the thread performs the IO operation by itself, and the IO operation itself is synchronous. Checking the relevant information on the Internet, I found that the application of AIO is not very extensive at present. Netty also tried to use AIO before, but gave up again.
 

27. What are the methods of communication between threads?

When multiple threads are executed concurrently, they are randomly switched and executed in the CPU. At this time, we want multiple threads to complete a task together. At this time, we need communication between threads. Multiple threads come together To complete a task, there are generally three ways for thread communication:

Through the volatile keyword: to  volatile achieve this task through keywords, this is also the simplest way to achieve, the general idea is that volatile is shared memory , two threads share a flag bit, when the flag bit changes, different threads are executed .
Through the wait/notify method of the Object class: our thread executes many empty loops to wait for another thread to acquire the lock. This operation undoubtedly consumes CPU resources, so in order to solve this situation, we just Need a mechanism to achieve communication between threads, you can wake up other threads, instead of waiting until you get the time slice of the CPU, we all know that the Object class provides three methods of communication between threads, wait(), notify (), notifyAll(). These three methods must all be executed in a synchronized code block.
Through the await/signal method of the condition: the await operation will release the lock immediately and enter the blocking state, and the signal will wake up the head node in the waiting queue (wake up in turn if it fails).
 

28. Tell me about springcloud?

Including core components such as Eureka, Hystrix, Ribbon, Feign, Zuul, etc.

1. Eureka: Service governance components, including the registry of the server and the service discovery mechanism of the client;

2. Ribbon: load balancing service invocation component, with multiple load balancing invocation strategies;

3. Hystrix: service fault-tolerant component, which implements the circuit breaker mode and provides fault tolerance for errors and delays in dependent services;

4. Feign: a declarative service call component based on Ribbon and Hystrix;

5. Zuul: API gateway component, which provides routing and filtering functions for requests
 

29. What did the Mathematical Modeling National Competition do? Specifically talk about the details? something innovative?

slightly

30. Tell me about the details of the quick sort algorithm?

Quicksort (Quicksort) is a commonly used sorting algorithm, and its implementation idea can be summarized as the following steps:

  1. Select a pivot, usually the first or last element of the sequence to be sorted.
  2. Divide the sequence into two parts, the left part is less than or equal to the reference element, and the right part is greater than or equal to the reference element. This process is called partitioning.
  3. Recursively perform quicksort on the left and right parts.
  4. After sorting the left part by recursion, recursively sort the order of the right part. When the sorting of the data in the left and right parts is completed, the sorting of the entire array is also completed.
  5. Quick sort is the idea of ​​divide and conquer. The basic idea is to decompose a problem of size N into K smaller sub-problems, which are independent of each other and have the same nature as the original problem . Finding the solution to the subproblem will give you the solution to the original problem.

31. What is the most impressive article on your personal blog? Tell me about the details?

slightly

Guess you like

Origin blog.csdn.net/nuist_NJUPT/article/details/129940483