As a Web Application developers , The most common work scenario in daily work is to develop interfaces for front-end calls or associated system calls . The interface has written so much , I don't know if you have summarized what details need to be paid attention to when developing an interface . Here are some of my own summaries , Use these precautions every time you write an interface “ scanning ” The next interface , Your interface quality will rise a step .
Can complete the normal business function
There should be no need to say more about this point , Our interface is to complete the business function , This is a must .
But if your interface only considers the normal flow , Once anything goes wrong, it doesn't work , So your interface is not a qualified interface .
If all user requests meet the interface expectations 、 All network calls don't timeout 、 No system will down machine , All systems can carry large concurrent traffic , So the programmer's workload will be reduced by a large part of the .“ Programmers are working on exceptions most of the time ”, I think you must have heard this sentence .
In fact, the following is the focus of this article , How to make your interface more robust 、 Higher performance and higher scalability , This is what we can't ignore in developing interfaces .
In every aspect of your interface , You have to think about whether there will be exceptions . If something goes wrong , What are you going to do with ？ Do you want to roll back your data ？Service Is it something that has been turned on ？ Is the isolation level of the transaction correct ？ Does your code have a global exception handling mechanism ？
Interface parameter checking mechanism
Many system exceptions are caused by incorrect parameters passed by the user, resulting in format errors 、 Null pointer error, etc . So we're going to kill the mistakes at the source , Strictly check the parameters passed by the user , stay controller Layer to do parameter verification . This can be combined with Spring Of Validate Mechanism .
Print accurate and reasonable logs
The principle of logging here is not to make redundant logs , The printed log enables you to see the running status of the system , The problem is to be able to locate the problem quickly .
Interface permission configuration
What roles of your interface users can access , Whether the user can access without logging in .
Data access issues
If your interface is a query interface , You should not find data that the current user does not have access to .
Interface performance optimization
- Use the cache ;
- Database indexing ;
- Try not to invoke the database in the loop ;
- Try not to associate multiple tables with queries ;
- Consider using asynchronous threads ;
- Upgrade system configuration （ A dead end method ）
There are many ways to improve interface performance , Here are just a few general Columns . Combine your specific business scenarios , Sometimes adjusting the business execution process can also greatly improve the user experience . For example, there are two steps in the process of credit extension and account opening , Among them, credit is a time-consuming operation , Then we can consider triggering the credit operation when the user submits the ID card material , The user submitted the material , The credit operation may have been completed .
Consider whether your interface should be idempotent
Interface self-test problem
Many developers don't want to test scenarios to test their interfaces . I think we have to think of a complete test scenario , It's even more than testing . My suggestion is that you should test yourself in the development environment , I want a variety of test scenarios , Make all kinds of data for testing , At the same time, save the data you made , Convenient for subsequent use .
Some other details
- When it comes to distributed systems, data consistency should be considered .
- Scalability optimization , Is your code very extensible .
- The development environment changed the code , Be sure to test in the test environment .（ Because the test environment and development environment may be very different , such as jdk edition , Code doesn't have to run ）