I have a server running Spring boot + JPA + Hibernate. I am using MySQL database (Using InnoDb engine by default). The implementation draws inspiration from many articles I had search on Internet. I have implemented REST API to facilitate building a website dynamically. I wanted to log all the API requests into a log (audit log). So when the API is called, I store the request method name and few parameters into auditlog table in MySql. Just before I return from the API, I store the response as well by updating the same record.
I was reviewing the code logs of Hibernate when I make API requests using the web application client as well as Postman. I noticed that for every API, it takes on an average 150ms - 200ms for inserts and updates. This is proving to be costly for APIs which fetch very less information.
So I want to know how I can speed up the inserts so that my inserts/updates take less than 10 -20 ms.
My Auditlog entity is
@Entity
@Table(name="auditlog")
public class AuditLog{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(nullable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
@CreatedDate
private Date created_at;
@Column(nullable = false)
@Temporal(TemporalType.TIMESTAMP)
@LastModifiedDate
private Date updated_at;
@NotBlank
private String methodName;
private String param1;
// Text field with private information like password masked
@Column(length = 65535, columnDefinition = "text")
private String request;
// Text field
@Column(length = 65535, columnDefinition = "text")
private String response;
private Integer result;
... // getters and setters
}
My AuditLogRepository is :
public interface AuditLogRepository extends JpaRepository<AuditLog, Long>{
}
In my REST API controller I am doing the following
...
AuditLog logEntry = new AuditLog();
// set all the values except generated ones like id, created_at and updated_at
logEntry.setMethodName(...);
logEntry.setParam1(...);
logEntry.setRequest(...);
// Save into the table using autowired repoitory
auditLogRepoitory.saveAndFlush(logEntry);
// ... do the operation of the API
// Update the logEntry
logEntry.setResult(...);
logEntry.setResponse(...);
auditLogRepoitory.saveAndFlush(logEntry);
...
Please help me in improving the insert and updates to the table. Or please help in improving the code so that I can make APIs response faster.
Thanks, Sri Prad