Netty interview questions and answers (a)

basis

  1. TCP and UDP difference?
    1. TCP is a connection-oriented (between the client and the server must first establish a connection before data transmission), UDP-free (no need to establish a connection before sending data) connection
    2. TCP provides reliable service (TCP data transmission without errors, no loss, will not be repeated, and arrive in order.); The UDP provides a simple transaction-oriented unreliable transport.
    3. UDP has better real-time, high work efficiency than TCP for relatively high communications or broadcast communications for high-speed transmission and real-time. With the improvement of network speed, UDP use more and more.
    4. Not a TCP connection can only point to point, UDP support one to one, and many-to-many interactive communication.
    5. TCP go more system resources, UDP less demanding on system resources
    6. UDP program structure more simple
    7. TCP is a stream mode, UDP datagram mode
  2. How TCP protocol to ensure reliable transmission?
    By TCP sequence number, checksum, acknowledgment signal, retransmission control, connection management, window control, flow control, congestion control for reliability.
    Reference: https://www.jianshu.com/p/6aac4b2a9fd7

  3. TCP handshake, waving mechanisms?
    Reference: https://blog.csdn.net/zengrenyuan/article/details/80313449

  4. What's stick package TCP / unpacking causes and the solution is?
    TCP is a byte-stream, although the data exchange between the application layer and the TCP transport layer is a data block sizes, but these data blocks TCP only as a series of unstructured byte stream, no borders. Further the frame structure can be seen from the TCP in the TCP header length field data not shown. Common causes of occurrence:
    • Data to be transmitted is greater than the size of the TCP send buffer space remaining, unpacking will occur.
    • Data to be transmitted is larger than the MSS (maximum packet size), TCP will be unpacked before transmission.
    • Data to be transmitted is smaller than the size of the TCP send buffer, the TCP data buffer multiple writes once sent out will occur stick package.
    • Data receiving side application layer did not read the received data buffer, stick package will occur.
    The key problem is how to add boundary information for each data packet, commonly used method is as follows:
    • Transmitting end for each data packet to add pack header, the header should contain at least a length of the packet, so that the receiving terminal after receiving the data by reading the packet length field of the header, they know the actual length of each data packet a.
    • Each sending end is a fixed length packet is encapsulated (not 0 may be prepared by filling up), so that the receiving end of each fixed length data read from the receive buffer to each data packet naturally split open.
    • You may be disposed in a boundary between the data packet, such as adding special symbols, so that the receiving end this boundary can be a different data packet split open.
  5. Netty stick pack / unpack how to deal with, what to achieve?
    • Message length, the fixed length packet size, enough space completion, sender and receiver to follow the same convention, so that even a stick package acquiring fixed length packets is achieved by programming the receiver can distinguish.
    • Trailer add special separators, end of message, for example, each have a carriage return (e.g. FTP protocol) or specify special character as a delimiter message, the recipient through a special separator segmented packets distinguished.
    • Message into the message header and a message body, message header information includes information indicating the total length (or the length of the message body) fields
      Reference: https://www.cnblogs.com/sidesky/p/6913109.html
  6. The difference between synchronous and asynchronous, blocking and non-blocking?
    • Sync: issue a function call when, in the absence of the results obtained, the call will not return. One thing that is a must do, such as one done before in order to do the next thing. E.g. ordinary B / S mode (synchronous): Submit Request -> waiting for the server process -> returns processed during the client browser can not do anything.

    • Asynchronous: When an asynchronous procedure call is issued, the caller can not immediately get results. The actual processing of the call after completion member, to inform the caller via a state, and a notification callback. For example ajax request (asynchronous): Request by event triggers -> server processing (which is the browser can still be used for other things) -> processed

    • Blocking: blocking calls means before the call returns, the current thread is suspended (thread into the non-executable state, in this state, cpu will not give the thread allocation of time slices that thread pauses). Function will return only after getting results. Maybe someone will call and synchronous call blocking equate, in fact, he is different. For synchronous calls, many times the current thread is still active, but does not return from the current function logically, it will seize the cpu to perform additional logic will automatically detect whether io ready.

    • Non-blocking: that before not get the results immediately, the function does not block the current thread, and will return immediately.

  7. Talk about network IO model?
    The nature of the network IO read the socket, socket linux system are abstracted as the stream, the operation can be understood as IO convection.
    • Blocking IO (bloking IO)
    • Nonblocking IO (non-blocking IO)
    • Multiplexing IO (multiplexing IO)
    • Signals driven IO (signal-driven IO)
    • Asynchronous IO (asynchronous IO)
      Reference: https://www.jianshu.com/p/a95bcb116765
  8. BIO. NIO. AIO, respectively, what is?
    • BIO: synchronous and blocking, server model to achieve a connection to a thread that has a client connection request on the server side need to start a thread for processing, if the connection does not do anything to cause unnecessary overhead of thread, of course, can improved thread pooling mechanism. The number of connected mode applies to relatively small BIO and fixed architecture, server resources in this way is relatively high, limited concurrent applications, the JDK1.4 previously only option, but the program intuitive and easy to understand.
    • NIO: synchronous non-blocking, the server requests a mode of realization of a thread, i.e. the connection request sent by the client are registered to the multiplexer, the multiplexer is connected to the polling I / O request when a start thread for processing. NIO connection number suitable for multi-mode and connected to short (light operation) architecture, such as chat server, limited concurrent applications, more complex programming, starts the JDK1.4 support.
    • AIO: asynchronous nonblocking server mode is a valid request to achieve a thread, the client I / O requests are completed by the OS to the notification server and then to start the application process .AIO threads to the number of connected mode using a multi-connector and a relatively long (re-operation) architecture, such as the album server, call the OS to fully participate in concurrent operation, programming is more complex, JDK7 began to support.
  9. select. poll. epoll mechanism and the difference?
    Reference: https://www.cnblogs.com/aspirant/p/9166944.html

  10. Netty's talk about what you know about?

  11. Netty with Java NIO What is the difference, why not just use JDK NIO libraries?
    • NIO libraries and API complicated, cumbersome to use, you need to master Selector, ServerSocketChannel, SocketChannel, ByteBuffer and so on.
    • You need to have additional skills to pave the way for other such familiar Java multi-threaded programming. This is because the NIO programming related to the Reactor mode, you must be very familiar with multithreading and network programming, in order to write high-quality NIO program.
    • Reliability capacity filled, workload and very difficult. For example a client face reconnect disconnected, network glitches, half a pack reader, a cache failure, network congestion, and exception handling problems stream, characterized by the NIO programming function is relatively easy to develop, but the reliability of the capacity filled operation the amount and difficulty are very large.
    • The JDK NIO BUG, e.g. epoll bug, it causes the polling Selector empty, resulting in 100% CPU. Examples of official certification based on the above reasons, in most scenarios, not recommended for direct use of JDK NIO library, unless you are proficient NIO programming or have special needs. In the vast majority of business scenario, we can use the NIO Framework NIO Netty be programmed either as a client or as a server, it supports both UDP and asynchronous file transfers, very powerful.
      Netty characteristics are summarized as follows:
    • API uses a simple, low development threshold
    • Powerful, a variety of pre codecs, supports popular protocols
    • Customization ability, can be flexibly expanded communications framework through ChannelHandler
    • High performance, by comparison with other industry mainstream NIO framework, the best overall performance of Netty
    • Mature, stable, Netty fixed all JDK NIO BUG has been found, business developers do not need to worry about the NIO BUG
    • Community activists, short version iterations, BUG discovered can be repaired in time, while more new features will be added
    • Experienced a large-scale commercial application of the test, the quality has been verified.
      Reference: https://www.520mwx.com/view/20451
  12. Netty components which, respectively, have any relevance?
    • Channel
      based IO operations, such as binding, connection, and so reading and writing depends on the primitives provided by the underlying network transport, network programming in Java, the base class is the Socket core, and the Channel Netty the API provides a set, greatly simplifies the complexity of the operation directly with the Socket, and many Channel super class, such as EmbeddedChannel, LocalServerChannel, NioDatagramChannel, NioSctpChannel, NioSocketChannel like.

    • EventLoop
      EventLoop process defined events during the connection of the core abstraction, after further discussion, the relationship between the Channel, EventLoop, Thread and EventLoopGroup shown below:

    • ChannelHandler ChannelPipeline and
      from the application developer opinion, is the most important component ChannelHandler, wherein storing user logic for processing inbound and outbound data. The method is invoked ChannelHandler network events, ChannelHandler can be used almost any type of operations, such as data from one format to another format or process thrown. For example, it ChannelInboundHandler sub-interface, and receiving inbound event data so as to be user-defined logic, or refresh the data ChannelInboundHandler response when the client terminal is connected.
      ChannelPipeline provides a container ChannelHandler chain and defines an API for propagation along the chain flow inbound and outbound events. When you create a Channel, it will automatically create a subsidiary ChannelPipeline.
    • Bootstrap and of ServerBootstrap
      Netty boot class network-layer applications to provide a container, which involves binding a process to a given port or connection process to another process running on the specified port on the specified host. Guide is divided into categories to guide ServerBootstrap guide Bootstrap client and server.

Reference: https://www.cnblogs.com/leesf456/p/6831661.html

  1. Netty's talk about the implementation process?
    1. Creating ServerBootStrap examples
    2. Reactor Set and binding thread pool: EventLoopGroup, EventLoop is registered to handle all Selector top of this thread's Channel
    3. Setting and service side of the binding channel
    4. Creating ChannelPipeline and handler handle network events, network time as a stream in which the flow, handler completion majority of custom: such as codec SSl safety certification
    5. Binding and start listening port
    6. When the rotational training to ready the channel, by the Reactor thread: NioEventLoop execution method pipline in the final scheduling and execution channelHandler

advanced

  1. Netty high performance reflected in what areas?
    • Transmission: IO model largely determines the performance of the frame, as compared to the bio, netty recommended asynchronous communication mode, a thread can be complicated because nio process N client connections and read and write operations, which solves the fundamental IO a traditional synchronous blocking a connection threading model, architecture, performance, elastic scalability and reliability have been greatly improved. As shown in the code, and use the NioEventLoopGroup NioSocketChannel to improve transmission efficiency.
    • Protocol: what kind of communication protocols, system performance is extremely important, to provide support for the default Netty Google Protobuf also be extended by Netty codec interface, the user may implement other performance framework sequences.
    • Thread: netty use the Reactor threading model, but different Reactor model, the performance impact is very large, the following describes the common thread of Reactor model, there are three, are as follows:
      • Reactor single-threading model: Model single-threaded, i.e. the thread receiving client server TCP connection as NIO, and initiates a TCP connection to a server as a client NIO, i.e., read or peer communication request response message, transmits a communication end ED message request or response message. Theoretically a separate thread can handle all IO operations related, but a process hundreds of threads NIO link can not support the performance, CPU load NIO thread even 100%, can not meet the massive coded message, decode, read and send, and because when the NIO thread overloaded, processing speed will slow down, which causes a large number of client connection timeout, timeout after tend to be retransmitted, which is even more heavy load NIO thread, final will lead to a large number of message processing backlog and overtime, NIO thread will be the system performance bottleneck.
      • Reactor Model Multithreading: NIO thread has a dedicated TCP connection request for monitoring the server, receiving client; network IO operations (read and write) is responsible for a NIO thread pool, thread pool may be employed to achieve the standard JDK thread pool. But when one million concurrent client connections, a thread that listens for and accepts nio was not enough, so there is a master-slave multi-threading model.
      • Master-slave Reactor multi-threading model: the use of master-slave NIO threading model, can solve a server monitor thread can not effectively deal with inadequate performance problems for all client connections, that monitor server, TCP connection receives the client's request and give a thread pool. Therefore, you can see in the code, we choose the server end is this way, and the threading model is also recommended. Create a startup class in different EventLoopGroup instance and by the appropriate configuration parameters, you can support the three Reactor threading model.
  2. Netty threading model is kind of how?
    Reference: https://segmentfault.com/a/1190000015484187#articleHeader2

  3. Netty zero-copy mention is reflected in where, what is the difference in the operating system?
    Zero-copy that data during operation, data need not be copied from a buffer memory area to another memory area. Less a copy of memory, enhance the efficiency of the CPU to get. Zero-copy on the OS level to avoid back and forth generally refers to copy data between user mode (User-space) and the kernel mode (Kernel-space). Zero-copy of Netty entirely in user mode (Java level) is more biased in favor of optimizing the operation data.
    • Netty ByteBuffer receive and transmit using DIRECT BUFFERS, the direct use of the heap memory for Socket reader, does not require a secondary copy of the byte buffer. If conventional heap memory (HEAP BUFFERS) Socket for reading and writing, the JVM will heap Buffer direct copy memory before writing the Socket. Compared to the outer direct memory heap, a message multiple copies of the buffer memory during transmission.
    • Buffer objects Netty provides a composition, can be polymerized ByteBuffer plurality of objects, as the user can easily operate the combination as the operation of a Buffer Buffer, avoid the traditional manner by the memory copy several small Buffer Buffer merge into one large.
    • Netty transferTo file transfer method using, it can transmit data directly to the target file buffer Channel, avoiding the problems of the conventional memory copy mode write cycle caused by.
      Reference: https://www.jianshu.com/p/a199ca28e80d
  4. Netty memory pool is how to achieve?
    https://blog.csdn.net/kuangzhanshatian/article/details/80881904
    https://www.jianshu.com/p/8d894e42b6e6

  5. Netty object pool is how to achieve?
    Reference: https://www.jianshu.com/p/3bfe0de2b022

Guess you like

Origin www.cnblogs.com/xiaoyangjia/p/11526197.html