Optimisation

[!note]
Only start optimising once you've identified the bottleneck

1. Caching

Reduce the query from read if we have read bottleneck

Pasted image 20230902110636.png

2. Connection Pool

Reuse a pool for database connection instead of creating a new connection each time.

Pasted image 20230902111138.png

Problem

When using serverless architecture, it's harder to manage connection pool. For example AWS lambda will create a new connection each time.

Pasted image 20230902111218.png

The solution is using RDS Proxy which manage the connection for us

Pasted image 20230902111300.png

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.

Pasted image 20230902111416.png

To avoid this, we can manually define and optimise our query

Pasted image 20230902111443.png

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

Pasted image 20230902111534.png

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

Pasted image 20230902111647.png

6. Compression

Compress the payload before transfering to the network

Pasted image 20230902111720.png

7. Asynchronous logging

Time taken to write log can add up.

Pasted image 20230902111808.png

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.