preface

Hello, everyone. I am a little boy picking up field snails.

A friend interviewed the back end of the universe. Sorted out the interview questions and answers, if there are mistakes, welcome to point out ha. Gold nine silver ten sprint, interview partners refueling ah.

HTTP status code (expire, cache-control); HTTP status code (expire, cache-control)

1.1 Expire and Cache-Control Fields

  • Cache-control is an HTTP/1.1 header field that distinguishes support for caching. This property is supported by both request and response headers. Caching policies are defined by the different values it provides. There are mainlyPublic, private, no-cacheEquivalence.
  • Expires is an HTTP1.0 header field with an expiration date. If a time is set, the browser will simply read the cache within that time and no longer request it.

1.2 Common HTTP Status Codes

2. Principle of HTTPS, digital signature, and digital certificate.

2.1 the HTTPS principle

  • HTTPS = HTTP + SSL/TLS: encrypts and decrypts data using SSL or TLS and transmits data through HTTP.
  • SSL, or Secure Sockets Layer (Secure Sockets Layer Protocol), is a security protocol that provides security and data integrity for network communications.
  • TLS, or Transport Layer Security, is a successor to SSL 3.0.

  1. The user enters an HTTPS url into the browser and connects to port 443 of the server.
  2. The server must have a set of digital certificates, which can be created or applied for by the organization. The difference is that the certificates issued by the server must be authenticated by the client. The certificate is a pair of public and private keys.
  3. The server sends its digital certificate (containing the public key) to the client.
  4. After receiving the digital certificate from the server, the client checks the certificate. If the certificate fails, a warning dialog box is displayed. If the certificate is fine, a key (symmetric encryption) is generated and encrypted with the certificate’s public key.
  5. The client makes a second HTTP request in HTTPS, sending the encrypted client key to the server.
  6. After receiving the ciphertext from the client, the server uses its own private key to asymmetric decrypt the ciphertext, obtains the client key, and encrypts the returned data symmetrically with the client key. In this way, the data becomes ciphertext.
  7. The server returns the encrypted ciphertext to the client.
  8. After receiving the ciphertext returned by the server, the client uses its own key (the client key) to symmetrically decrypt the ciphertext to obtain the data returned by the server.

2.2 Digital signature and Digital Certificate

If you know how Https works, you know digital certificates. To avoid tampering with the public key, a digital certificate is introduced as follows:

Composition of digital Certificates

  • The public key and personal information are encrypted with the Hash algorithm to form a message digest. The message digest is taken to a trusted certification Authority (CA), which encrypts the message digest with its private key to form a digital signature.
  • The public key, personal information, and digital signature together form a digital certificate.

3. State of the TCP connection between the client and server: time_wait

3.1 a TCP connection

During the TCP connection, the client state is SYN_SEND and ESTABLISHED, and the server state is SYN_RCVD and ESTABLISHED.

Both the client and server are in the CLOSED state. Then the server listens for a port and enters the LISTEN state

  • The client enters the SYN_SEND state after the first handshake (SYN=1, seq=x) is sent
  • After the second handshake (SYN=1, ACK=1, SEq = Y, ACKnum=x+1) is sent, the server enters the SYN_RCVD state.
  • For the third handshake (ACK=1, ACKnum=y+1), the client enters the ESTABLISHED state after the packet is sent. When the server receives the packet, the client also enters the ESTABLISHED state. The TCP handshake is used to start data transmission.

3.2 the time_wait state

Remember the four waves of TCP,

  • After the first wave (FIN=1, SEq = U) is sent, the client enters the FIN_WAIT_1 state
  • After the second wave (ACK=1, ACK= U +1,seq = V) is sent, the server enters the CLOSE_WAIT state, and the client enters the FIN_WAIT_2 state after receiving the acknowledgement packet
  • After the third wave (FIN=1, ACK1, SEQ = W, ACK = U +1), the server enters the LAST_ACK state and waits for the last ACK from the client.
  • On the fourth wave (ACK=1, SEq =u+1, ACK= w+1), the client receives the shutdown request from the server, sends an acknowledgement packet, and enters the TIME_WAIT state, waiting for a fixed amount of time (two maximum lifetime, 2MSL, 2 Maximum Segment Lifetime); if the server does not receive an ACK from the server, the connection is CLOSED. After receiving the acknowledgement packet, the server closes the connection and enters the CLOSED state.

Why do I need to WAIT 2MSL in time-wait state

2MSL, 2 Maximum Segment Lifetime

  • One MSL ensures that the last ACK message of the active closing party in the four waves can reach the peer end
  • An MSL ensures that the peer end can receive the REtransmitted FIN packet if it does not receive an ACK

4. What is virtual memory? What is physical memory?

4.1 What is Virtual Memory?

Virtual memory is virtual memory, its core idea is to ensure that each program has its own address space, address space is divided into multiple blocks, each block has a continuous address space. At the same time, the physical space is divided into multiple blocks. The block size is the same as that of the virtual address space. The operating system automatically maps the virtual address space to the physical address space, and the program only needs to pay attention to the virtual memory.

4.2 What is Physical Memory

Physical memory refers to the memory space obtained through physical memory bars, while virtual memory refers to a region of the hard disk divided into memory.

We often say physical memory size, actually refers to the size of memory. Generally when buying a computer, we will look at the memory chip is how much capacity, say, if the memory chip size is 100 GB, that 100 GB can be used? If the address bus is only 20 bits, then its addressing space is 1MB. Even if you can install 100GB of memory, it doesn’t make sense to consider physical memory size as 1MB.

4.3 How to Map Virtual Memory to Physical Memory?

In the following figure, there is a Memory Management Unit (MMU) in the CPU. Instead of sending the virtual Memory directly to the Memory bus, the MMU first sends the virtual Memory to the physical address. The MMU maps the virtual address to the physical address. The logic of the mapping is handled automatically by other modules.

5. What is the maximum number of TCP connections that can be established on a machine, client or server

  • TCP connection client: The TCP connection theory for each IP address is limited by the IP_local_port_range parameter, as well as by 65535. However, you can configure multiple IP addresses to enhance your ability to establish connections.
  • TCP connected server machine: Each listening port is theoretically large, but this number has no practical significance. The maximum number of concurrent connections depends on the size of your memory, so each static TCP connection takes about 3

.3K memory.

6.Eureka principle, whether strong consistency, Eureka cluster. Can the service still be called if the service is down? Eureka and ZooKeeper

6.1 eureka architecture

Registries are one of the core components of distributed development, and Eureka is the registry implementation recommended by Spring Cloud.

The architecture diagram is as follows:

  • Eureka Server: Provides service registration and discovery. Data is synchronized between multiple Eureka servers to ensure consistent status
  • Service Provider: Service providers register their services with Eureka so that Service consumers can find them
  • Service Consumer: A Service Consumer that obtains a list of registered services from Eureka and is able to consume services

6.2 Eureka architecture diagram based on cluster

Eureka Server can be deployed in a cluster. Multiple nodes use replicable (asynchronous) data synchronization to ensure data consistency. As an out-of-the-box service registry, Eureka Server provides the following functions: service registration, receiving service heartbeat, service rejection, service offline, etc.

Eureka Server will synchronize the registration information to other Eureka servers. When the service consumer wants to call the service provider, it will obtain the service provider address from the service registry and then cache the service provider address locally. It is directly fetched from the local cache to complete a call.

6.3 Can Services Be Invoked When the System Is Down?

When Eureka is down, microservices can be switched on, but only if the provider’s address remains the same. If the provider changes an IP address or port, the consumer will not be able to detect the change in time.

6.4 Comparison between Eureka and ZooKeeper

  • Zookeeper guarantees CP(consistency and partition fault tolerance), but does not guarantee availability, and ZK is not available during the leader election.
  • Eureka guarantees AP (availability and partition fault tolerance). It prioritizes availability and the failure of several nodes does not affect the functioning of normal nodes.

7.Hystrix? Talk about how Hystrix works

The Hystrix workflow flow chart is as follows:

  1. Build commands

Hystrix provides two commands, HystrixCommand and HystrixObservableCommand, which can be used to wrap tasks to be executed.

  1. Execute the command

There are four ways to execute command. Respectively is:

  • R execute() : Synchronizes execution to get a single result object from the dependent service
  • Future Queue () : Executes asynchronously, returns a Future to get the result, also a single result object
  • Observable Observe () : Hot Observable. After an Observable is created, it subscribes to an Observable and returns multiple results
  • Observable toObservable() : Cold Observable, returns an Observable that executes only when subscribed and returns multiple results
  1. Check the cache

If Hystrix Cache is enabled, the system checks whether there is a Cache for executing the same command before executing the task. If so, return the cached result directly; If no result is cached but caching is enabled, the result is cached for future use.

Similar to a fuse, which will blow out to protect the circuit in case of danger, a circuit-breaker can trigger a short circuit when it reaches a threshold we set (such as a 50% request failure rate) and refuse to execute any request.

If the circuit breaker is turned on, Hystrix will not execute the command and go directly to the Fallback processing logic.

5. Check thread pool/semaphore status Hystrix isolation includes thread pool isolation and semaphore isolation. When using the Hystrix thread pool, Hystrix defaults to 10 threads per dependent service, and when all 10 threads are busy, the command is rejected. Same thing with semaphores.

6. Perform specific tasks Through HystrixObservableCommand. The construct () or HystrixCommand. The run () to run the user real tasks.

7. Calculate link health Status When a command is executed, when a command is executed, or when an exception occurs, the system records the execution status, such as success, failure, rejection, and timeout. The system periodically processes the data and determines whether to enable the circuit breaker based on the specified conditions.

8. Run the Fallback logic when the command fails. Run the specified Fallback logic when the command fails. In the figure above, disconnection, thread pool rejection, semaphore rejection, execution execution, and execution timeout are all processed by Fallback.

The original result is returned as an Observable that does some processing depending on how it is called before being returned to the user.

8. Zookeeper consistency guarantee, zAB protocol principle, ZooKeeper consistency, strong consistency or final consistency

Zab: Zookeeper Atomic Broadcast. Zookeeper uses the Zab protocol to ensure the ultimate consistency of distributed transactions.

Zab protocol is an atomic broadcast protocol specially designed for Distributed coordination service Zookeeper to support crash recovery. It is the core algorithm of Zookeeper to ensure data consistency. Zab borrows from Paxos algorithm and is a general distributed consistency algorithm.

Based on the Zab protocol, Zookeeper implements a master/slave (Leader and Follower) model to ensure data consistency among replicas in the cluster. This means that only one Leader node handles external write transaction requests and then synchronizes data to the other Follower nodes.

The Zookeeper client randomly links to a node in the Zookeeper cluster. In the case of a read request, data is directly read from the current node. If it is a write request, the node will submit the transaction to the Leader. The Leader receives the transaction, broadcasts the transaction, and if more than half of the nodes write successfully, the transaction will be committed.

The Zab protocol requires each Leader to go through three phases: discovery, synchronization, and broadcast.

  • Discover: The ZooKeeper cluster must elect a Leader process, and the Leader maintains a list of available followers clients. Clients will be able to communicate with these Follower nodes in the future.
  • Synchronization: The Leader is responsible for synchronizing its own data with the followers to achieve multi-copy storage. This is also a way to leverage high availability and partition fault tolerance in CAP. Follower writes unprocessed requests in the queue to the local transaction log after they are consumed.
  • Broadcast: The Leader can accept the client’s new transaction Proposal request and broadcast the new Proposal request to all followers.

9. Talk about the ZooKeeper election mechanism

When the server is started or running (the Leader is down), the Leader election will be held. Let’s take a look. Suppose there are five servers in the ZooKeeper cluster, and their myids are servers 1, 2, 3, 4 and 5, as shown in the figure:

9.1 Leader Election for server startup

Zookeeper cluster initialization phase, server (myID =1-5)In turn,Start: Starts to elect the ZooKeeper Leader~

  1. Server 1 (myID =1) starts. Currently, there is only one server and the Leader election cannot be completed
  2. Server 2 (myID =2) starts. The two servers can communicate with each other and enter the Leader election phase
  • 2.1. Each server issues one poll

Both server 1 and server 2 vote themselves as the Leader server. The basic elements of the vote include: myID and ZXID of the server, which we represent in the form of (myID, ZXID). Initially, both server 1 and server 2 vote for themselves, i.e. server 1 votes for (1,0) and server 2 votes for (2,0), and each sends this vote to all the other machines in the cluster.

  • 2.2 Accept votes from each server

Each server accepts votes from other servers. At the same time, the server verifies the validity of the vote, whether it was voted in this round and whether it came from a server in the LOOKING state.

  • 2.3. Processing of votes

After receiving the votes of other servers, the system will PK the votes of the other servers with its own votes. The PK rules are as follows:

  • Check the ZXID first. The server with a larger ZXID takes precedence as the leader.
  • If the zxids are the same, the myID is compared, and the server with the larger MYID acts as the leader.

Server 1’s vote is (1,0), it receives the vote is (2,0), both zxids are 0, because the received myID =2 is greater than its own myID =1, so it updates its vote to (2,0), and sends the vote again. For server 2, you no longer need to update your vote, just send out the last vote.

  • 2.4. Statistical voting

After each vote, the server counts all the votes and determines whether more than half of the machines received the same information. Server 2 received two votes, less than 3 (n/2+1,n is the total server), so it continues LOOKING

  1. Server 3 (myID =3) starts and continues to the Leader election phase
  • 3.1 In accordance with the previous process, server 1 and 2 vote for themselves first, because server 3 has the largest myID, so they vote for it instead. In this case, the server has 3 votes (greater than or equal to N /2+1), so server 3 is elected Leader. Change the status of server 1,2 to FOLLOWING, and server 3 to LEADING.
  1. Server 4 starts and initiates an election.
  • 4.1 At this time, server 1, 2 and 3 are no longer in LOOKING state and will not change the ballot information. Ballot information result: 3 votes for server 3, 1 vote for server 4. Server 4 and change the state to FOLLOWING;
  1. Server 5 starts and initiates an election.
  • Similarly, the server votes for server 3, server 5 and changes the state to FOLLOWING;
  1. After the vote, server 3 is elected Leader

9.2 Leader Election during server running

Five servers (myID =1-5) in the ZooKeeper cluster are running. Suddenly, Leader server 3 hangs, and the Leader election starts

  1. The status

After the Leader server hangs, the remaining non-Observer servers change their server state to LOOKING and begin the Leader election process.

  1. Each server initiates a vote

Each server votes for itself, and since this is run-time, each server may have a different ZXID. Assuming that the zxIDS of services 1,2,4, and 5 are 333,666,999,888, it generates a vote (1,333), (2,666), (4,999), and (5,888), and then sends this vote to all the other machines in the cluster.

  1. Accept votes from each server
  2. Processing to vote

The voting rules are the same as during the Zookeeper cluster startup. The ZXID is checked first, and the larger one acts as the Leader first. Therefore, obviously, server ZXID =999 has priority.

  1. Vote statistics
  2. Changing server state

10. Algorithm: Given a string s, find the length of the longest continuous substring that does not contain repeating characters.

This can be implemented using a sliding window as follows:

public int lengthOfLongestSubstring2(String s) { int n = s.length(); if (n <= 1) return n; int maxLen = 1; Int left = 0, right = 0; Set<Character> window = new HashSet<>(); while (right < n) { char rightChar = s.charAt(right); while (window.contains(rightChar)) { window.remove(s.charAt(left)); left++; } // maxLen = math. Max (maxLen, right-left + 1); window.add(rightChar); right++; } return maxLen; }Copy the code

Reference and thanks

  • Spring Cloud source code learning Hystrix works
  • Zookeeper – Consistency protocol :Zab protocol