当前位置:网站首页>Distributed common interview questions

Distributed common interview questions

2022-06-23 09:41:00 Linging_ twenty-four

1. How to prevent form from submitting repeatedly

  1. front end . Wait after each click X Seconds to click .
  2. Add a unique index to the database
  3. When the server returns to the form page , Mr. Hui becomes a subToken Keep in session or redis, When the form is submitted, carry token, If token Agreement , Follow up , And send the... In the server token Delete . If token non-existent , Then it indicates that it is a duplicate submission .

2. How to design a seckill system

  1. front end .
    Before the second kill , Button gray , The front-end request does not give the real address . Front end timing request back-end interface , If it's second kill time , Then return to the real address of the front end , Front release button , Wait after each click XX Seconds to click .

  2. The server : The service is used nginx Clustering 、redis Also do cluster

  3. Current limiting : Before the second kill , Store the number of second kill tokens in redis in , It can be used list, Every time you ask, go redis Take out the token from , If you get it, it means that the second kill is successful , Then go to the database and place an order , If not , It means that the goods are sold out .

  4. Message middleware . If the number of second kills is large , For example, tens of thousands , After the second kill is successful , Put successful requests into mq perhaps kafka Middleware , Then get the request from the message queue .

  5. service degradation . Just in case , We still need to do service degradation .

3. Interface idempotent design of distributed system

  1. only id. Every operation , Both generate unique information based on actions and content id, Judge before execution id Whether there is , If not, follow up , And save to the database or redis etc. .
  2. The server provides sending token The interface of , Get before calling the service interface token, Then call the business interface request. , hold token Carry the past , Server judgment token Whether there is redis in , Presence indicates first request , You can continue to do business , After the completion of the business , Finally, we need to redis Medium token Delete .
  3. Create de duplication table . Save the uniquely identified fields in the business to the de duplication table , If... Exists in the table , It means it has been processed
  4. version control . Add version number , When the version number matches , To update the data .
  5. State control . For example, the order has status : Paid 、 Did not pay 、 In the payment 、 Failure to pay , It can be modified to medium payment only when it is in unpaid status .

4. Manage distributed session Four ways of ( Single sign on )

  1. session Copy
    Application server on Web Container of Session Copy function , Synchronization between several servers in the cluster Session object .

    The plan is simple , And read from this machine session It's also pretty quick , But there are very obvious defects : It can only be used when the cluster size is relatively small ( Enterprise application system , Few users , This pattern is relatively common ), When the cluster scale is large , Cluster servers need a lot of communication Session Copy , Take up a lot of resources from servers and networks , The system is heavily burdened . And because of the user's session Information is backed up on every server , Under a large number of user visits , There may be insufficient server memory session Usage .

  2. session Conversation retention
    Session persistence is using the original address of load balancing Hash Algorithm implementation , Load balancing servers will always come from the same IP The requests for are distributed to the same server .

    Although this scheme ensures that every user can get their own session, And a lot of users are not afraid to visit , But this session keeping does not meet the needs of high availability of the system . This scheme has a fatal flaw : Once a server goes down , All on this server session Information doesn't exist , The user requests to switch to another server , And other servers don't have their corresponding session Information makes it impossible to complete related business . So this approach is basically not going to be adopted .

  3. cookie Record
    take session perhaps token Record on the client side , Every time the server is requested, it will session perhaps token Send it to the server in the request , The server can verify whether to log in .

    utilize cookie Record session There are many shortcomings : such as cookie There is a limit to the size of information that can be recorded ; For example, every request must be transmitted cookie Affect performance ; such as cookie It can be modified or cracked , Lead to cookie Don't store important information , The safety factor is not enough . But because of cookie Simple and easy to use , Supports linear scaling of servers , And most of it session The information is relatively small , So in fact, many websites will use more or less cookie To record some of the unimportant session Information .

  4. session share
    Use redis Save all of the system session,redis As a storage center , Multiple machines of the distributed system can be shared , When requested , Go to redis To find out if there is session, You can determine whether the user logs in .

    This method is applicable to redis High availability of , Need to build redis colony .

5. How to guarantee the order of request execution

 Generally speaking , In terms of business logic, it is best to design the system without the guarantee of this order , Because once the sequential guarantee is introduced , It will lead to the increase of system complexity , Efficiency will be reduced , There will be too much pressure on hot data .

First use consistency hash Load balancing strategy , Will be the same id All requests are distributed to the same machine for processing , For example, an order can be made according to the order id. If the processing machine is multithreaded , Memory queues can be introduced to process , Will be the same id The request was passed hash To the same queue , A queue only corresponds to one processing thread .
It is best to combine multiple operations into one operation .

————————————————
Copyright notice : This paper is about CSDN Blogger 「 A cat called a dog 」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/zgsxhdzxl/article/details/104414475

原网站

版权声明
本文为[Linging_ twenty-four]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230929349295.html