Optimisation
[!note]
Only start optimising once you've identified the bottleneck
1. Caching
Reduce the query from read if we have read bottleneck
2. Connection Pool
Reuse a pool for database connection instead of creating a new connection each time.
Problem
When using serverless architecture, it's harder to manage connection pool. For example AWS lambda will create a new connection each time.
The solution is using RDS Proxy which manage the connection for us
3. Avoid N+1 Query problem
N+1 problem normally happens in ORM or the application where to do the 1
queries it needs to trigger another n
queries.
To avoid this, we can manually define and optimise our query
4. Pagination
If api return a large number of responses it can slow things down. As a result, breaking into smaller pages will be better
5. Optimise JSON serialiser
Consider using a fast JSON serialiser to optimise the JSON serialisation.
If internal APIs and microservices talk to eachother, consider using gRPC
6. Compression
Compress the payload before transfering to the network
7. Asynchronous logging
Time taken to write log can add up.
A separate logging thread can send them to the log server.
However there is a small chance you might loose some lost if the application crashed before the logs have been written.