Backend performance becomes critical as your application scales. In ABP Boilerplate projects, inefficient data access and query handling can quickly lead to slow APIs, high memory consumption, and unnecessary database load.
The good news is that substantial performance gains can often be achieved without overhauling the architecture, simply by writing efficient queries and leveraging the framework's built-in features correctly.
1. Always Use CancellationToken
One of the most critical performance practices is passing CancellationToken to async methods. If a client navigates away, closes the page, or cancels a request, the API execution should halt immediately rather than continuing to consume server resources.
This practice helps:
- Reduce unnecessary processing
- Free up memory allocations
- Improve overall system responsiveness
2. Select Only the Required Data
Avoid fetching full entities when only a subset of fields is needed. Always utilize the Select method to project specific data into your Data Transfer Objects (DTOs).
This ensures the database retrieves only the relevant columns, resulting in:
- Reduced database load
- Lower memory consumption
- Faster API response times
3. Use Async/Await Properly
Implement asynchronous programming for database operations and external service calls to enhance scalability. However, avoid wrapping synchronous operations in async blindly.
Apply it specifically where it prevents thread blocking for I/O-bound operations. Proper asynchronous execution allows the application to handle a significantly higher volume of concurrent users efficiently.
4. Use AsNoTracking for Read-Only Queries
For read-only operations, always append .AsNoTracking() to your queries. This instructs Entity Framework Core to bypass tracking entities in the change tracker, significantly reducing memory overhead and execution time.
Best use cases include:
- GetAll APIs
- Reporting queries
- Dashboard data retrieval
5. Prefer IQueryable for Query Building
Utilize IQueryable when constructing queries dynamically step by step. This ensures that the query logic is translated into SQL and executed at the database level rather than in the application memory.
Best practice dictates keeping the query as IQueryable for as long as possible and executing it only when the final result is required.
6. Avoid Materializing to IEnumerable Too Early
Calling methods that convert data to IEnumerable prematurely forces subsequent filtering or sorting operations to occur in application memory instead of the database engine.
Use IEnumerable only when the exact dataset has already been fetched from the database and specific in-memory processing is required. Otherwise, defer execution using IQueryable.
7. Avoid Loading Entire Tables
Never retrieve complete tables when only a subset of data is required. This is essential for production systems where datasets grow continuously over time.
Always apply database-level constraints before execution:
- Filtering
- Sorting
- Searching
- Pagination
8. Use Pagination for Large Data Sets
Avoid returning massive payloads in a single API response. Consistently implement pagination to limit response sizes, accelerate load times, and provide a smoother user experience.
9. Maintain a Clean and Layered Architecture
Keep backend code well-structured and cleanly separated:
- Application services should orchestrate business logic
- Repositories should strictly handle data access
- Avoid mixing concerns across layers
Maintaining a clean architecture makes performance bottlenecks easier to identify, isolate, and resolve.
10. Write Efficient Code from the Start
While not every endpoint requires aggressive optimization, establishing a baseline of best practices is essential.
Key habits for a performant foundation include:
- Using projections (
Select) - Applying
AsNoTrackingfor reads - Implementing
CancellationToken - Avoiding unnecessary data loading
Conclusion
Backend optimization in ABP Boilerplate relies less on complex infrastructure changes and more on disciplined, efficient coding standards. By mastering efficient query writing, utilizing cancellation tokens, and properly managing data tracking, developers can drastically improve both application performance and scalability.
Efficient query writing is one of the most valuable backend skills, and mastering it will greatly enhance the quality and reliability of your software architecture.