Hypersistence blog - Key takeaways 2
CDC(Change Data Capture)
*CDC(change data capture) works effectively by decoding the underlying transaction log and extract the CDC events from it. The extracted events are propagated to Apache Kafka, which further exchange messages b/w various other modules of a large enterprise system.
*Envers is the hibernate native implementation for CDC. change events triggered outside of the persistence captured will not be captured by Envers. AuditReaderFactory class takes a JPA EntityManager or Hibernate Session to generate an AuditReader object.
Connection Pooling
*Opening/closing connections are quite an expensive operation. The most obvious reasons for reusing a database connection would be:- reducing the application and database management system OS I/O overhead for creating/destroying a TCP connection
- reducing JVM object garbage
- discarding all overflowing traffic, therefore decreasing availability
- queuing requests and wait (for as long as a timeout threshold) for busy resources to become available
*If there is a traffic spike the connection pool will level it instead of saturating all available database resources.
*The waiting step and the timeout mechanism are safety hooks, preventing excessive database server load. If one application gets way too much database traffic, the connection pool is going to mitigate it, therefore, preventing it from taking down the database server (hence affecting the whole enterprise system).
*To configure a pool we may need to pay attention to many pool settings such as:
- minimum size
- maximum size
- max idle time
- acquire timeout
- timeout retry attempts
*Sizing connection pools are not an upfront design decision. In large enterprise systems, you need adaptability and monitoring is the first step to taking the right decisions.
List<PostComment> comments = entityManager.createQuery(
"select pc " +
"from PostComment pc " +
"where pc.review = :review", PostComment.class)
.setParameter("review", review)
.getResultList();
for(PostComment comment : comments) {
LOGGER.info("The post title is '{}'", comment.getPost().getTitle());
}
Because the post association is not initialized, Hibernate must fetch the Post entity with a secondary query, and for N PostComment entities, N more queries are going to be executed (hence the N+1 query problem).
N+1 problem
*The N+1 query issue happens when you forget to fetch an association and then you need to accessList<PostComment> comments = entityManager.createQuery(
"select pc " +
"from PostComment pc " +
"where pc.review = :review", PostComment.class)
.setParameter("review", review)
.getResultList();
for(PostComment comment : comments) {
LOGGER.info("The post title is '{}'", comment.getPost().getTitle());
}
Because the post association is not initialized, Hibernate must fetch the Post entity with a secondary query, and for N PostComment entities, N more queries are going to be executed (hence the N+1 query problem).
Comments
Post a Comment