SlideShare a Scribd company logo
1 of 68
Download to read offline
High-Performance
Hibernate
VLAD MIHALCEA
About me
• @Hibernate Developer
• vladmihalcea.com
• @vlad_mihalcea
• vladmihalcea
Agenda
• Performance and Scaling
• Connection providers
• Identifier generators
• Relationships
• Batching
• Fetching
• Caching
Performance Facts
“More than half of application performance
bottlenecks originate in the database”
AppDynamics - http://www.appdynamics.com/database/
Google Ranking
“Like us, our users place a lot of value in speed — that's why
we've decided to take site speed into account in our search
rankings.”
https://webmasters.googleblog.com/2010/04/using-site-speed-in-web-search-ranking.html
Performance and Revenue
“It has been reported that every 100ms of latency costs
Amazon 1% of profit.”
http://radar.oreilly.com/2008/08/radar-theme-web-ops.html
Response Time and Throughput
• n - number of completed transactions
• t - time interval
𝑇𝑎𝑣𝑔 =
𝑡
𝑛
=
1𝑠
100
= 10 𝑚𝑠
𝑋 =
𝑛
𝑡
=
100
1𝑠
= 100 𝑇𝑃𝑆
Response Time and Throughput
𝑋 =
1
𝑇𝑎𝑣𝑔
“The lower the Response Time,
The higher the Throughput”
The anatomy of a database transaction
Response Time
• connection acquisition time
• statement submit time
• statement execution time
• result set fetching time
• idle time prior to releasing database connection
𝑇 = 𝑡 𝑎𝑐𝑞 + 𝑡 𝑟𝑒𝑞 + 𝑡 𝑒𝑥𝑒𝑐 + 𝑡 𝑟𝑒𝑠 + 𝑡𝑖𝑑𝑙𝑒
Agenda
• Performance and Scaling
• Connection providers
• Identifier generators
• Relationships
• Batching
• Fetching
• Caching
Connection Management
Metric DB_A (ms) DB_B (ms) DB_C (ms) DB_D (ms) HikariCP (ms)
min 11.174 5.441 24.468 0.860 0.001230
max 129.400 26.110 74.634 74.313 1.014051
mean 13.829 6.477 28.910 1.590 0.003458
p99 20.432 9.944 54.952 3.022 0.010263
𝑇 = 𝑡 𝑎𝑐𝑞 + 𝑡 𝑟𝑒𝑞 + 𝑡 𝑒𝑥𝑒𝑐 + 𝑡 𝑟𝑒𝑠 + 𝑡𝑖𝑑𝑙𝑒
Connection Providers
DataSourceConnectionProvider
Connection Provisioning
FlexyPool
• concurrent connections
• concurrent connection requests
• connection acquisition time
• connection lease time histogram
• maximum pool size
• overflow pool size
• retries attempts
• total connection acquisition time
• Java EE
• Bitronix / Atomikos
• Apache DBCP / DBCP2
• C3P0
• BoneCP
• HikariCP
• Tomcat CP
• Vibur DBCP
https://github.com/vladmihalcea/flexy-pool
FlexyPool – Concurrent connection requests
1
28
55
82
109
136
163
190
217
244
271
298
325
352
379
406
433
460
487
514
541
568
595
622
649
676
703
730
757
784
811
838
865
892
919
946
973
1000
1027
0
2
4
6
8
10
12
Sample time (Index × 15s)
Connectionrequests
max mean p50 p95 p99
FlexyPool – Pool size growth
1
28
55
82
109
136
163
190
217
244
271
298
325
352
379
406
433
460
487
514
541
568
595
622
649
676
703
730
757
784
811
838
865
892
919
946
973
1000
1027
0
1
2
3
4
5
6
Sample time (Index × 15s)
Maxpoolsize
max mean p50 p95 p99
FlexyPool – Connection acquisition time
1
28
55
82
109
136
163
190
217
244
271
298
325
352
379
406
433
460
487
514
541
568
595
622
649
676
703
730
757
784
811
838
865
892
919
946
973
1000
1027
0
500
1000
1500
2000
2500
3000
3500
Sample time (Index × 15s)
Connectionacquisitiontime(ms)
max mean p50 p95 p99
FlexyPool – Connection lease time
1
29
57
85
113
141
169
197
225
253
281
309
337
365
393
421
449
477
505
533
561
589
617
645
673
701
729
757
785
813
841
869
897
925
953
981
1009
1037
0
5000
10000
15000
20000
25000
30000
35000
40000
Sample time (Index × 15s)
Connectionleasetime(ms)
max mean p50 p95 p99
Agenda
• Performance and Scaling
• Connection providers
• Identifier generators
• Relationships
• Batching
• Fetching
• Caching
JPA Identifier Generators
𝑇 = 𝑡 𝑎𝑐𝑞 + 𝑡 𝑟𝑒𝑞 + 𝑡 𝑒𝑥𝑒𝑐 + 𝑡 𝑟𝑒𝑠 + 𝑡𝑖𝑑𝑙𝑒
• IDENTITY
• SEQUENCE
• TABLE
• AUTO
IDENTITY
• In Hibernate, IDENTITY generator disables JDBC batch
inserts
• MySQL 5.7 does not offer support for database SEQUENCE
SEQUENCE
• Oracle, PostgreSQL, and even SQL Server 2012
• May use roundtrip optimizers: hi/lo, pooled, pooled-lo
• By default, Hibernate 5 uses the enhanced sequence
generators
<property
name="hibernate.id.new_generator_mappings"
value="true"/>
SEQUENCE - Pooled optimizer (50 rows)
1 5 10 50
0
0.05
0.1
0.15
0.2
0.25
0.3
0.35
0.4
0.45
Sequence increment size
Time(ms)
TABLE
• Uses row-level locks and a separate transaction/connection
• May use roundtrip optimizers: hi/lo, pooled, pooled-lo
• By default, Hibernate 5 uses the enhanced sequence
generators
<property
name="hibernate.id.new_generator_mappings"
value="true"/>
TABLE - Pooled optimizer (50 rows)
1 5 10 50
0
0.5
1
1.5
2
2.5
3
Table increment size
Time(ms)
IDENTITY vs TABLE (100 rows)
• IDENTITY makes no use of batch inserts
• TABLE generator using a pooled optimizer with an increment
size of 100
IDENTITY vs TABLE (100 rows)
1 2 4 8 16
0
500
1000
1500
2000
2500
Thread count
Time(ms)
Identity Table
AUTO: IDENTITY vs TABLE?
• Prior to Hibernate 5, AUTO would resolve to IDENTITY if the
database supports such a feature
• Hibernate 5 uses TABLE generator if the database does not
support sequences
SEQUENCE vs TABLE (100 rows)
• Both benefiting from JDBC batch inserts
• Both using a pooled optimizer with an increment size of 100
SEQUENCE vs TABLE (100 rows)
1 2 4 8 16
0
200
400
600
800
1000
1200
Thread count
Time(ms)
Sequence Table
Agenda
• Performance and Scaling
• Connection providers
• Identifier generators
• Relationships
• Batching
• Fetching
• Caching
Relationships
𝑇 = 𝑡 𝑎𝑐𝑞 + 𝑡 𝑟𝑒𝑞 + 𝑡 𝑒𝑥𝑒𝑐 + 𝑡 𝑟𝑒𝑠 + 𝑡𝑖𝑑𝑙𝑒
Agenda
• Performance and Scaling
• Connection providers
• Identifier generators
• Relationships
• Batching
• Fetching
• Caching
Batching
𝑇 = 𝑡 𝑎𝑐𝑞 + 𝑡 𝑟𝑒𝑞 + 𝑡 𝑒𝑥𝑒𝑐 + 𝑡 𝑟𝑒𝑠 + 𝑡𝑖𝑑𝑙𝑒
• SessionFactory setting
• Session-level configuration since Hibernate 5.2
Batching - SessionFactory
<property
name="hibernate.jdbc.batch_size"
value="5"/>
• Switching from non-batching to batching
Batching - Session
doInJPA( this::entityManagerFactory, entityManager -> {
entityManager.unwrap( Session.class )
.setJdbcBatchSize( 10 );
for ( long i = 0; i < entityCount; ++i ) {
Person = new Person( i, String.format( "Person %d", i ) );
entityManager.persist( person );
if ( i % batchSize == 0 ) {
entityManager.flush();
entityManager.clear();
}
}
} );
Batching
DEBUG [main]: n.t.d.l.SLF4JQueryLoggingListener –
Name:DATA_SOURCE_PROXY,
Time:1,
Success:True,
Type:Prepared,
Batch:True,
QuerySize:1,
BatchSize:10,
Query: ["insert into Person (name, id) values (?, ?)"],
Params:[
(Person 1, 1), (Person 2, 2), (Person 3, 3), (Person 4, 4), (Person 5, 5),
(Person 6, 6), (Person 7, 7), (Person 8, 8), (Person 9, 9), (Person 10, 10)
]
Insert PreparedStatement batching (5k rows)
1 10 20 30 40 50 60 70 80 90 100 1000
0
200
400
600
800
1000
1200
1400
1600
Batch size
Time(ms)
DB_A DB_B DB_C DB_D
Update PreparedStatement batching (5k rows)
1 10 20 30 40 50 60 70 80 90 100 1000
0
100
200
300
400
500
600
700
Batch size
Time(ms)
DB_A DB_B DB_C DB_D
Delete PreparedStatement batching (5k rows)
1 10 20 30 40 50 60 70 80 90 100 1000
0
200
400
600
800
1000
1200
Batch size
Time(ms)
DB_A DB_B DB_C DB_D
Batching - Cascading
<property
name="hibernate.order_inserts"
value="true"/>
<property
name="hibernate.order_updates"
value="true"/>
Batching – @Version
<property
name="hibernate.jdbc.batch_versioned_data"
value="true"/>
• Enabled by default in Hibernate 5
• Disabled in Hibernate 3.x, 4.x, and for Oracle 8i, 9i, and
10g dialects
Agenda
• Performance and Scaling
• Connection providers
• Identifier generators
• Relationships
• Batching
• Fetching
• Caching
Fetching
𝑇 = 𝑡 𝑎𝑐𝑞 + 𝑡 𝑟𝑒𝑞 + 𝑡 𝑒𝑥𝑒𝑐 + 𝑡 𝑟𝑒𝑠 + 𝑡𝑖𝑑𝑙𝑒
• JDBC fetch size
• JDBC ResultSet size
• DTO vs Entity queries
• Fetching relationships
Fetching – JDBC Fetch Size
• Oracle – Default fetch size is 10
• SQL Server – Adaptive buffering
• PostgreSQL, MySQL – Fetch the whole ResultSet at once
• SessionFactory setting:
<property
name="hibernate.jdbc.fetch_size"
value="100"/>
Fetching - JDBC fetch size
• Query-level hint:
List<PostCommentSummary> summaries =
entityManager.createQuery(
"select new PostCommentSummary( " +
" p.id, p.title, c.review ) " +
"from PostComment c " +
"join c.post p")
.setHint(QueryHints.HINT_FETCH_SIZE, fetchSize)
.getResultList();
Fetching – JDBC Fetch Size (10k rows)
1 10 100 1000 10000
0
100
200
300
400
500
600
Fetch size
Time(ms)
DB_A DB_B DB_C DB_D
Fetching – Pagination
• JPA / Hibernate API works for both entity and native queries
List<PostCommentSummary> summaries =
entityManager.createQuery(
"select new PostCommentSummary( " +
" p.id, p.title, c.review ) " +
"from PostComment c " +
"join c.post p")
.setFirstResult(pageStart)
.setMaxResults(pageSize)
.getResultList();
Fetching – 100k vs 100 rows
Fetch all Fetch limit
0
500
1000
1500
2000
2500
3000
3500
4000
4500
5000
Time(ms)
DB_A DB_B DB_C DB_D
Fetching – Pagination
• Hibernate uses OFFSET pagination
• Keyset pagination scales better when navigating large result
sets
• http://use-the-index-luke.com/no-offset
Fetching – Entity vs Projection
• Selecting all columns vs a custom projection
SELECT *
FROM post_comment pc
INNER JOIN post p ON p.id = pc.post_id
INNER JOIN post_details pd ON p.id = pd.id
SELECT pc.version
FROM post_comment pc
INNER JOIN post p ON p.id = pc.post_id
INNER JOIN post_details pd ON p.id = pd.id
Fetching – Entity vs Projection
All columns Custom projection
0
5
10
15
20
25
30
Time(ms)
DB_A DB_B DB_C DB_D
Fetching – DTO Projections
• Read-only views
• Tree structures (Recursive CTE)
• Paginated Tables
• Analytics (Window functions)
Fetching – Entity Queries
• Writing data
• Web flows / Multi-request logical transactions
• Application-level repeatable reads
• Detached entities / PersistenceContextType.EXTENDED
• Optimistic concurrency control (e.g. version, dirty properties)
Fetching – Relationships
Association FetchType
@ManyToOne EAGER
@OneToOne EAGER
@OneToMany LAZY
@ManyToMany LAZY
• LAZY associations can be fetched
eagerly
• EAGER associations cannot be fetched
lazily
Fetching – Best Practices
• Default to FetchType.LAZY
• Fetch directive in JPQL/Criteria API queries
• Entity graphs / @FetchProfile
• LazyInitializationException
Fetching – Open Session in View Anti-Pattern
Fetching – Temporary Session Anti-Pattern
• “Band aid” for LazyInitializationException
• One temporary Session/Connection for every lazily fetched
association
<property
name="hibernate.enable_lazy_load_no_trans"
value="true"/>
Agenda
• Performance and Scaling
• Connection providers
• Identifier generators
• Relationships
• Batching
• Fetching
• Caching
Caching
𝑇 = 𝑡 𝑎𝑐𝑞 + 𝑡 𝑟𝑒𝑞 + 𝑡 𝑒𝑥𝑒𝑐 + 𝑡 𝑟𝑒𝑠 + 𝑡𝑖𝑑𝑙𝑒
Caching – Why 2nd - Level Caching
Caching – Why 2nd - Level Caching
“There are only two hard things in Computer
Science: cache invalidation and naming
things.”
Phil Karlton
Caching – Strategies
Strategy Cache type Particularity
READ_ONLY READ-THROUGH Immutable
NONSTRICT_READ_WRITE READ-THROUGH Invalidation/
Inconsistency risk
READ_WRITE WRITE-THROUGH Soft Locks
TRANSACTIONAL WRITE-THROUGH JTA
Caching – Collection Cache
• It complement entity caching
• It stores only entity identifiers
• Read-Through
• Invalidation-based (Consistency over Performance)
Caching – Read - Write Aggregates
Questions and Answers
https://leanpub.com/high-performance-java-persistence
• Performance and Scaling
• Connection providers
• Identifier generators
• Relationships
• Batching
• Fetching
• Caching

More Related Content

What's hot

Groovy concurrency
Groovy concurrencyGroovy concurrency
Groovy concurrencyAlex Miller
 
Caching In The Cloud
Caching In The CloudCaching In The Cloud
Caching In The CloudAlex Miller
 
Tips and Tricks For Faster Asp.NET and MVC Applications
Tips and Tricks For Faster Asp.NET and MVC ApplicationsTips and Tricks For Faster Asp.NET and MVC Applications
Tips and Tricks For Faster Asp.NET and MVC ApplicationsSarvesh Kushwaha
 
Parallel Query in AWS Aurora MySQL
Parallel Query in AWS Aurora MySQLParallel Query in AWS Aurora MySQL
Parallel Query in AWS Aurora MySQLMydbops
 
Asynchronous web apps with the Play Framework 2.0
Asynchronous web apps with the Play Framework 2.0Asynchronous web apps with the Play Framework 2.0
Asynchronous web apps with the Play Framework 2.0Oscar Renalias
 
Play + scala + reactive mongo
Play + scala + reactive mongoPlay + scala + reactive mongo
Play + scala + reactive mongoMax Kremer
 
The Grid the Brad and the Ugly: Using Grids to Improve Your Applications
The Grid the Brad and the Ugly: Using Grids to Improve Your ApplicationsThe Grid the Brad and the Ugly: Using Grids to Improve Your Applications
The Grid the Brad and the Ugly: Using Grids to Improve Your Applicationsbalassaitis
 
Connect 2016-Move Your XPages Applications to the Fast Lane
Connect 2016-Move Your XPages Applications to the Fast LaneConnect 2016-Move Your XPages Applications to the Fast Lane
Connect 2016-Move Your XPages Applications to the Fast LaneHoward Greenberg
 
DOSUG Taking Apache Camel For A Ride
DOSUG Taking Apache Camel For A RideDOSUG Taking Apache Camel For A Ride
DOSUG Taking Apache Camel For A RideMatthew McCullough
 
Mysql server query path
Mysql server query pathMysql server query path
Mysql server query pathWenjie Wu
 
Working With a Real-World Dataset in Neo4j: Import and Modeling
Working With a Real-World Dataset in Neo4j: Import and ModelingWorking With a Real-World Dataset in Neo4j: Import and Modeling
Working With a Real-World Dataset in Neo4j: Import and ModelingNeo4j
 
(SDD402) Amazon ElastiCache Deep Dive | AWS re:Invent 2014
(SDD402) Amazon ElastiCache Deep Dive | AWS re:Invent 2014(SDD402) Amazon ElastiCache Deep Dive | AWS re:Invent 2014
(SDD402) Amazon ElastiCache Deep Dive | AWS re:Invent 2014Amazon Web Services
 
WebObjects Optimization
WebObjects OptimizationWebObjects Optimization
WebObjects OptimizationWO Community
 
Short intro to scala and the play framework
Short intro to scala and the play frameworkShort intro to scala and the play framework
Short intro to scala and the play frameworkFelipe
 
Streamline Hadoop DevOps with Apache Ambari
Streamline Hadoop DevOps with Apache AmbariStreamline Hadoop DevOps with Apache Ambari
Streamline Hadoop DevOps with Apache AmbariAlejandro Fernandez
 
Building Scalable .NET Web Applications
Building Scalable .NET Web ApplicationsBuilding Scalable .NET Web Applications
Building Scalable .NET Web ApplicationsBuu Nguyen
 
Go faster with_native_compilation Part-2
Go faster with_native_compilation Part-2Go faster with_native_compilation Part-2
Go faster with_native_compilation Part-2Rajeev Rastogi (KRR)
 
20151010 my sq-landjavav2a
20151010 my sq-landjavav2a20151010 my sq-landjavav2a
20151010 my sq-landjavav2aIvan Ma
 

What's hot (20)

Groovy concurrency
Groovy concurrencyGroovy concurrency
Groovy concurrency
 
Caching In The Cloud
Caching In The CloudCaching In The Cloud
Caching In The Cloud
 
Tips and Tricks For Faster Asp.NET and MVC Applications
Tips and Tricks For Faster Asp.NET and MVC ApplicationsTips and Tricks For Faster Asp.NET and MVC Applications
Tips and Tricks For Faster Asp.NET and MVC Applications
 
Parallel Query in AWS Aurora MySQL
Parallel Query in AWS Aurora MySQLParallel Query in AWS Aurora MySQL
Parallel Query in AWS Aurora MySQL
 
Asynchronous web apps with the Play Framework 2.0
Asynchronous web apps with the Play Framework 2.0Asynchronous web apps with the Play Framework 2.0
Asynchronous web apps with the Play Framework 2.0
 
Play + scala + reactive mongo
Play + scala + reactive mongoPlay + scala + reactive mongo
Play + scala + reactive mongo
 
The Grid the Brad and the Ugly: Using Grids to Improve Your Applications
The Grid the Brad and the Ugly: Using Grids to Improve Your ApplicationsThe Grid the Brad and the Ugly: Using Grids to Improve Your Applications
The Grid the Brad and the Ugly: Using Grids to Improve Your Applications
 
Connect 2016-Move Your XPages Applications to the Fast Lane
Connect 2016-Move Your XPages Applications to the Fast LaneConnect 2016-Move Your XPages Applications to the Fast Lane
Connect 2016-Move Your XPages Applications to the Fast Lane
 
DOSUG Taking Apache Camel For A Ride
DOSUG Taking Apache Camel For A RideDOSUG Taking Apache Camel For A Ride
DOSUG Taking Apache Camel For A Ride
 
Mysql server query path
Mysql server query pathMysql server query path
Mysql server query path
 
Azure cosmosdb
Azure cosmosdbAzure cosmosdb
Azure cosmosdb
 
Working With a Real-World Dataset in Neo4j: Import and Modeling
Working With a Real-World Dataset in Neo4j: Import and ModelingWorking With a Real-World Dataset in Neo4j: Import and Modeling
Working With a Real-World Dataset in Neo4j: Import and Modeling
 
(SDD402) Amazon ElastiCache Deep Dive | AWS re:Invent 2014
(SDD402) Amazon ElastiCache Deep Dive | AWS re:Invent 2014(SDD402) Amazon ElastiCache Deep Dive | AWS re:Invent 2014
(SDD402) Amazon ElastiCache Deep Dive | AWS re:Invent 2014
 
WebObjects Optimization
WebObjects OptimizationWebObjects Optimization
WebObjects Optimization
 
Short intro to scala and the play framework
Short intro to scala and the play frameworkShort intro to scala and the play framework
Short intro to scala and the play framework
 
Streamline Hadoop DevOps with Apache Ambari
Streamline Hadoop DevOps with Apache AmbariStreamline Hadoop DevOps with Apache Ambari
Streamline Hadoop DevOps with Apache Ambari
 
MySQL 5.7 + JSON
MySQL 5.7 + JSONMySQL 5.7 + JSON
MySQL 5.7 + JSON
 
Building Scalable .NET Web Applications
Building Scalable .NET Web ApplicationsBuilding Scalable .NET Web Applications
Building Scalable .NET Web Applications
 
Go faster with_native_compilation Part-2
Go faster with_native_compilation Part-2Go faster with_native_compilation Part-2
Go faster with_native_compilation Part-2
 
20151010 my sq-landjavav2a
20151010 my sq-landjavav2a20151010 my sq-landjavav2a
20151010 my sq-landjavav2a
 

Viewers also liked

Transactions and Concurrency Control Patterns
Transactions and Concurrency Control PatternsTransactions and Concurrency Control Patterns
Transactions and Concurrency Control PatternsVlad Mihalcea
 
Navigating the Incubator at the Apache Software Foundation
Navigating the Incubator at the Apache Software FoundationNavigating the Incubator at the Apache Software Foundation
Navigating the Incubator at the Apache Software FoundationBrett Porter
 
Hibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance TechniquesHibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance TechniquesBrett Meyer
 
10 Things You Didn’t Know About Mobile Email from Litmus & HubSpot
 10 Things You Didn’t Know About Mobile Email from Litmus & HubSpot 10 Things You Didn’t Know About Mobile Email from Litmus & HubSpot
10 Things You Didn’t Know About Mobile Email from Litmus & HubSpotHubSpot
 
How to Earn the Attention of Today's Buyer
How to Earn the Attention of Today's BuyerHow to Earn the Attention of Today's Buyer
How to Earn the Attention of Today's BuyerHubSpot
 
25 Discovery Call Questions
25 Discovery Call Questions25 Discovery Call Questions
25 Discovery Call QuestionsHubSpot
 
Modern Prospecting Techniques for Connecting with Prospects (from Sales Hacke...
Modern Prospecting Techniques for Connecting with Prospects (from Sales Hacke...Modern Prospecting Techniques for Connecting with Prospects (from Sales Hacke...
Modern Prospecting Techniques for Connecting with Prospects (from Sales Hacke...HubSpot
 
Why People Block Ads (And What It Means for Marketers and Advertisers) [New R...
Why People Block Ads (And What It Means for Marketers and Advertisers) [New R...Why People Block Ads (And What It Means for Marketers and Advertisers) [New R...
Why People Block Ads (And What It Means for Marketers and Advertisers) [New R...HubSpot
 
What is Inbound Recruiting?
What is Inbound Recruiting?What is Inbound Recruiting?
What is Inbound Recruiting?HubSpot
 
3 Proven Sales Email Templates Used by Successful Companies
3 Proven Sales Email Templates Used by Successful Companies3 Proven Sales Email Templates Used by Successful Companies
3 Proven Sales Email Templates Used by Successful CompaniesHubSpot
 
Add the Women Back: Wikipedia Edit-a-Thon
Add the Women Back: Wikipedia Edit-a-ThonAdd the Women Back: Wikipedia Edit-a-Thon
Add the Women Back: Wikipedia Edit-a-ThonHubSpot
 
Design in Tech Report 2017
Design in Tech Report 2017Design in Tech Report 2017
Design in Tech Report 2017John Maeda
 
CodeFest 2013. Зиновьев А. — MyBatis & Hibernate, давайте жить дружно!
CodeFest 2013. Зиновьев А. — MyBatis & Hibernate, давайте жить дружно!CodeFest 2013. Зиновьев А. — MyBatis & Hibernate, давайте жить дружно!
CodeFest 2013. Зиновьев А. — MyBatis & Hibernate, давайте жить дружно!CodeFest
 
Вебинар начало
Вебинар началоВебинар начало
Вебинар началоcatarus
 

Viewers also liked (17)

Transactions and Concurrency Control Patterns
Transactions and Concurrency Control PatternsTransactions and Concurrency Control Patterns
Transactions and Concurrency Control Patterns
 
Navigating the Incubator at the Apache Software Foundation
Navigating the Incubator at the Apache Software FoundationNavigating the Incubator at the Apache Software Foundation
Navigating the Incubator at the Apache Software Foundation
 
Hibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance TechniquesHibernate ORM: Tips, Tricks, and Performance Techniques
Hibernate ORM: Tips, Tricks, and Performance Techniques
 
10 Things You Didn’t Know About Mobile Email from Litmus & HubSpot
 10 Things You Didn’t Know About Mobile Email from Litmus & HubSpot 10 Things You Didn’t Know About Mobile Email from Litmus & HubSpot
10 Things You Didn’t Know About Mobile Email from Litmus & HubSpot
 
How to Earn the Attention of Today's Buyer
How to Earn the Attention of Today's BuyerHow to Earn the Attention of Today's Buyer
How to Earn the Attention of Today's Buyer
 
25 Discovery Call Questions
25 Discovery Call Questions25 Discovery Call Questions
25 Discovery Call Questions
 
Modern Prospecting Techniques for Connecting with Prospects (from Sales Hacke...
Modern Prospecting Techniques for Connecting with Prospects (from Sales Hacke...Modern Prospecting Techniques for Connecting with Prospects (from Sales Hacke...
Modern Prospecting Techniques for Connecting with Prospects (from Sales Hacke...
 
Why People Block Ads (And What It Means for Marketers and Advertisers) [New R...
Why People Block Ads (And What It Means for Marketers and Advertisers) [New R...Why People Block Ads (And What It Means for Marketers and Advertisers) [New R...
Why People Block Ads (And What It Means for Marketers and Advertisers) [New R...
 
What is Inbound Recruiting?
What is Inbound Recruiting?What is Inbound Recruiting?
What is Inbound Recruiting?
 
3 Proven Sales Email Templates Used by Successful Companies
3 Proven Sales Email Templates Used by Successful Companies3 Proven Sales Email Templates Used by Successful Companies
3 Proven Sales Email Templates Used by Successful Companies
 
Add the Women Back: Wikipedia Edit-a-Thon
Add the Women Back: Wikipedia Edit-a-ThonAdd the Women Back: Wikipedia Edit-a-Thon
Add the Women Back: Wikipedia Edit-a-Thon
 
Design in Tech Report 2017
Design in Tech Report 2017Design in Tech Report 2017
Design in Tech Report 2017
 
Hibernate
HibernateHibernate
Hibernate
 
CodeFest 2013. Зиновьев А. — MyBatis & Hibernate, давайте жить дружно!
CodeFest 2013. Зиновьев А. — MyBatis & Hibernate, давайте жить дружно!CodeFest 2013. Зиновьев А. — MyBatis & Hibernate, давайте жить дружно!
CodeFest 2013. Зиновьев А. — MyBatis & Hibernate, давайте жить дружно!
 
JPA - Java Persistence API
JPA - Java Persistence APIJPA - Java Persistence API
JPA - Java Persistence API
 
Вебинар начало
Вебинар началоВебинар начало
Вебинар начало
 
Tdd Workshop Disscussions
Tdd Workshop DisscussionsTdd Workshop Disscussions
Tdd Workshop Disscussions
 

Similar to High-Performance Hibernate Techniques

How to ensure Presto scalability 
in multi use case
How to ensure Presto scalability 
in multi use case How to ensure Presto scalability 
in multi use case
How to ensure Presto scalability 
in multi use case Kai Sasaki
 
Apache Samza 1.0 - What's New, What's Next
Apache Samza 1.0 - What's New, What's NextApache Samza 1.0 - What's New, What's Next
Apache Samza 1.0 - What's New, What's NextPrateek Maheshwari
 
Performance eng prakash.sahu
Performance eng prakash.sahuPerformance eng prakash.sahu
Performance eng prakash.sahuDr. Prakash Sahu
 
Exploring Contact Lens and Amazon Connect
Exploring Contact Lens and Amazon ConnectExploring Contact Lens and Amazon Connect
Exploring Contact Lens and Amazon ConnectCloudHesive
 
Building Scalable Websites with Perl
Building Scalable Websites with PerlBuilding Scalable Websites with Perl
Building Scalable Websites with PerlPerrin Harkins
 
MLflow at Company Scale
MLflow at Company ScaleMLflow at Company Scale
MLflow at Company ScaleDatabricks
 
Square Peg Round Hole: Serverless Solutions For Non-Serverless Problems
Square Peg Round Hole: Serverless Solutions For Non-Serverless ProblemsSquare Peg Round Hole: Serverless Solutions For Non-Serverless Problems
Square Peg Round Hole: Serverless Solutions For Non-Serverless ProblemsChase Douglas
 
Data Analytics Service Company and Its Ruby Usage
Data Analytics Service Company and Its Ruby UsageData Analytics Service Company and Its Ruby Usage
Data Analytics Service Company and Its Ruby UsageSATOSHI TAGOMORI
 
5 Years of Progress in Active Data Warehousing
5 Years of Progress in Active Data Warehousing5 Years of Progress in Active Data Warehousing
5 Years of Progress in Active Data WarehousingTeradata
 
Make your gui shine with ajax solr
Make your gui shine with ajax solrMake your gui shine with ajax solr
Make your gui shine with ajax solrlucenerevolution
 
Dealing with and learning from the sandbox
Dealing with and learning from the sandboxDealing with and learning from the sandbox
Dealing with and learning from the sandboxElaine Van Bergen
 
6 tips for improving ruby performance
6 tips for improving ruby performance6 tips for improving ruby performance
6 tips for improving ruby performanceEngine Yard
 
2019 hashiconf consul-templaterb
2019 hashiconf consul-templaterb2019 hashiconf consul-templaterb
2019 hashiconf consul-templaterbPierre Souchay
 
WinOps Conf 2016 - Michael Greene - Release Pipelines
WinOps Conf 2016 - Michael Greene - Release PipelinesWinOps Conf 2016 - Michael Greene - Release Pipelines
WinOps Conf 2016 - Michael Greene - Release PipelinesWinOps Conf
 
Dealing with and learning from the sandbox
Dealing with and learning from the sandboxDealing with and learning from the sandbox
Dealing with and learning from the sandboxElaine Van Bergen
 
GraphConnect 2014 SF: From Zero to Graph in 120: Scale
GraphConnect 2014 SF: From Zero to Graph in 120: ScaleGraphConnect 2014 SF: From Zero to Graph in 120: Scale
GraphConnect 2014 SF: From Zero to Graph in 120: ScaleNeo4j
 
Google Cloud Platform monitoring with Zabbix
Google Cloud Platform monitoring with ZabbixGoogle Cloud Platform monitoring with Zabbix
Google Cloud Platform monitoring with ZabbixMax Kuzkin
 
Docker Logging and analysing with Elastic Stack - Jakub Hajek
Docker Logging and analysing with Elastic Stack - Jakub Hajek Docker Logging and analysing with Elastic Stack - Jakub Hajek
Docker Logging and analysing with Elastic Stack - Jakub Hajek PROIDEA
 
Docker Logging and analysing with Elastic Stack
Docker Logging and analysing with Elastic StackDocker Logging and analysing with Elastic Stack
Docker Logging and analysing with Elastic StackJakub Hajek
 

Similar to High-Performance Hibernate Techniques (20)

How to ensure Presto scalability 
in multi use case
How to ensure Presto scalability 
in multi use case How to ensure Presto scalability 
in multi use case
How to ensure Presto scalability 
in multi use case
 
Apache Samza 1.0 - What's New, What's Next
Apache Samza 1.0 - What's New, What's NextApache Samza 1.0 - What's New, What's Next
Apache Samza 1.0 - What's New, What's Next
 
Performance eng prakash.sahu
Performance eng prakash.sahuPerformance eng prakash.sahu
Performance eng prakash.sahu
 
Exploring Contact Lens and Amazon Connect
Exploring Contact Lens and Amazon ConnectExploring Contact Lens and Amazon Connect
Exploring Contact Lens and Amazon Connect
 
QSpiders - Installation and Brief Dose of Load Runner
QSpiders - Installation and Brief Dose of Load RunnerQSpiders - Installation and Brief Dose of Load Runner
QSpiders - Installation and Brief Dose of Load Runner
 
Building Scalable Websites with Perl
Building Scalable Websites with PerlBuilding Scalable Websites with Perl
Building Scalable Websites with Perl
 
MLflow at Company Scale
MLflow at Company ScaleMLflow at Company Scale
MLflow at Company Scale
 
Square Peg Round Hole: Serverless Solutions For Non-Serverless Problems
Square Peg Round Hole: Serverless Solutions For Non-Serverless ProblemsSquare Peg Round Hole: Serverless Solutions For Non-Serverless Problems
Square Peg Round Hole: Serverless Solutions For Non-Serverless Problems
 
Data Analytics Service Company and Its Ruby Usage
Data Analytics Service Company and Its Ruby UsageData Analytics Service Company and Its Ruby Usage
Data Analytics Service Company and Its Ruby Usage
 
5 Years of Progress in Active Data Warehousing
5 Years of Progress in Active Data Warehousing5 Years of Progress in Active Data Warehousing
5 Years of Progress in Active Data Warehousing
 
Make your gui shine with ajax solr
Make your gui shine with ajax solrMake your gui shine with ajax solr
Make your gui shine with ajax solr
 
Dealing with and learning from the sandbox
Dealing with and learning from the sandboxDealing with and learning from the sandbox
Dealing with and learning from the sandbox
 
6 tips for improving ruby performance
6 tips for improving ruby performance6 tips for improving ruby performance
6 tips for improving ruby performance
 
2019 hashiconf consul-templaterb
2019 hashiconf consul-templaterb2019 hashiconf consul-templaterb
2019 hashiconf consul-templaterb
 
WinOps Conf 2016 - Michael Greene - Release Pipelines
WinOps Conf 2016 - Michael Greene - Release PipelinesWinOps Conf 2016 - Michael Greene - Release Pipelines
WinOps Conf 2016 - Michael Greene - Release Pipelines
 
Dealing with and learning from the sandbox
Dealing with and learning from the sandboxDealing with and learning from the sandbox
Dealing with and learning from the sandbox
 
GraphConnect 2014 SF: From Zero to Graph in 120: Scale
GraphConnect 2014 SF: From Zero to Graph in 120: ScaleGraphConnect 2014 SF: From Zero to Graph in 120: Scale
GraphConnect 2014 SF: From Zero to Graph in 120: Scale
 
Google Cloud Platform monitoring with Zabbix
Google Cloud Platform monitoring with ZabbixGoogle Cloud Platform monitoring with Zabbix
Google Cloud Platform monitoring with Zabbix
 
Docker Logging and analysing with Elastic Stack - Jakub Hajek
Docker Logging and analysing with Elastic Stack - Jakub Hajek Docker Logging and analysing with Elastic Stack - Jakub Hajek
Docker Logging and analysing with Elastic Stack - Jakub Hajek
 
Docker Logging and analysing with Elastic Stack
Docker Logging and analysing with Elastic StackDocker Logging and analysing with Elastic Stack
Docker Logging and analysing with Elastic Stack
 

Recently uploaded

Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...Technogeeks
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecturerahul_net
 

Recently uploaded (20)

Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...What is Advanced Excel and what are some best practices for designing and cre...
What is Advanced Excel and what are some best practices for designing and cre...
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Advantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your BusinessAdvantages of Odoo ERP 17 for Your Business
Advantages of Odoo ERP 17 for Your Business
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Understanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM ArchitectureUnderstanding Flamingo - DeepMind's VLM Architecture
Understanding Flamingo - DeepMind's VLM Architecture
 

High-Performance Hibernate Techniques

  • 2. About me • @Hibernate Developer • vladmihalcea.com • @vlad_mihalcea • vladmihalcea
  • 3. Agenda • Performance and Scaling • Connection providers • Identifier generators • Relationships • Batching • Fetching • Caching
  • 4. Performance Facts “More than half of application performance bottlenecks originate in the database” AppDynamics - http://www.appdynamics.com/database/
  • 5. Google Ranking “Like us, our users place a lot of value in speed — that's why we've decided to take site speed into account in our search rankings.” https://webmasters.googleblog.com/2010/04/using-site-speed-in-web-search-ranking.html
  • 6. Performance and Revenue “It has been reported that every 100ms of latency costs Amazon 1% of profit.” http://radar.oreilly.com/2008/08/radar-theme-web-ops.html
  • 7. Response Time and Throughput • n - number of completed transactions • t - time interval 𝑇𝑎𝑣𝑔 = 𝑡 𝑛 = 1𝑠 100 = 10 𝑚𝑠 𝑋 = 𝑛 𝑡 = 100 1𝑠 = 100 𝑇𝑃𝑆
  • 8. Response Time and Throughput 𝑋 = 1 𝑇𝑎𝑣𝑔 “The lower the Response Time, The higher the Throughput”
  • 9. The anatomy of a database transaction
  • 10. Response Time • connection acquisition time • statement submit time • statement execution time • result set fetching time • idle time prior to releasing database connection 𝑇 = 𝑡 𝑎𝑐𝑞 + 𝑡 𝑟𝑒𝑞 + 𝑡 𝑒𝑥𝑒𝑐 + 𝑡 𝑟𝑒𝑠 + 𝑡𝑖𝑑𝑙𝑒
  • 11. Agenda • Performance and Scaling • Connection providers • Identifier generators • Relationships • Batching • Fetching • Caching
  • 12. Connection Management Metric DB_A (ms) DB_B (ms) DB_C (ms) DB_D (ms) HikariCP (ms) min 11.174 5.441 24.468 0.860 0.001230 max 129.400 26.110 74.634 74.313 1.014051 mean 13.829 6.477 28.910 1.590 0.003458 p99 20.432 9.944 54.952 3.022 0.010263 𝑇 = 𝑡 𝑎𝑐𝑞 + 𝑡 𝑟𝑒𝑞 + 𝑡 𝑒𝑥𝑒𝑐 + 𝑡 𝑟𝑒𝑠 + 𝑡𝑖𝑑𝑙𝑒
  • 16. FlexyPool • concurrent connections • concurrent connection requests • connection acquisition time • connection lease time histogram • maximum pool size • overflow pool size • retries attempts • total connection acquisition time • Java EE • Bitronix / Atomikos • Apache DBCP / DBCP2 • C3P0 • BoneCP • HikariCP • Tomcat CP • Vibur DBCP https://github.com/vladmihalcea/flexy-pool
  • 17. FlexyPool – Concurrent connection requests 1 28 55 82 109 136 163 190 217 244 271 298 325 352 379 406 433 460 487 514 541 568 595 622 649 676 703 730 757 784 811 838 865 892 919 946 973 1000 1027 0 2 4 6 8 10 12 Sample time (Index × 15s) Connectionrequests max mean p50 p95 p99
  • 18. FlexyPool – Pool size growth 1 28 55 82 109 136 163 190 217 244 271 298 325 352 379 406 433 460 487 514 541 568 595 622 649 676 703 730 757 784 811 838 865 892 919 946 973 1000 1027 0 1 2 3 4 5 6 Sample time (Index × 15s) Maxpoolsize max mean p50 p95 p99
  • 19. FlexyPool – Connection acquisition time 1 28 55 82 109 136 163 190 217 244 271 298 325 352 379 406 433 460 487 514 541 568 595 622 649 676 703 730 757 784 811 838 865 892 919 946 973 1000 1027 0 500 1000 1500 2000 2500 3000 3500 Sample time (Index × 15s) Connectionacquisitiontime(ms) max mean p50 p95 p99
  • 20. FlexyPool – Connection lease time 1 29 57 85 113 141 169 197 225 253 281 309 337 365 393 421 449 477 505 533 561 589 617 645 673 701 729 757 785 813 841 869 897 925 953 981 1009 1037 0 5000 10000 15000 20000 25000 30000 35000 40000 Sample time (Index × 15s) Connectionleasetime(ms) max mean p50 p95 p99
  • 21. Agenda • Performance and Scaling • Connection providers • Identifier generators • Relationships • Batching • Fetching • Caching
  • 22. JPA Identifier Generators 𝑇 = 𝑡 𝑎𝑐𝑞 + 𝑡 𝑟𝑒𝑞 + 𝑡 𝑒𝑥𝑒𝑐 + 𝑡 𝑟𝑒𝑠 + 𝑡𝑖𝑑𝑙𝑒 • IDENTITY • SEQUENCE • TABLE • AUTO
  • 23. IDENTITY • In Hibernate, IDENTITY generator disables JDBC batch inserts • MySQL 5.7 does not offer support for database SEQUENCE
  • 24. SEQUENCE • Oracle, PostgreSQL, and even SQL Server 2012 • May use roundtrip optimizers: hi/lo, pooled, pooled-lo • By default, Hibernate 5 uses the enhanced sequence generators <property name="hibernate.id.new_generator_mappings" value="true"/>
  • 25. SEQUENCE - Pooled optimizer (50 rows) 1 5 10 50 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 Sequence increment size Time(ms)
  • 26. TABLE • Uses row-level locks and a separate transaction/connection • May use roundtrip optimizers: hi/lo, pooled, pooled-lo • By default, Hibernate 5 uses the enhanced sequence generators <property name="hibernate.id.new_generator_mappings" value="true"/>
  • 27. TABLE - Pooled optimizer (50 rows) 1 5 10 50 0 0.5 1 1.5 2 2.5 3 Table increment size Time(ms)
  • 28. IDENTITY vs TABLE (100 rows) • IDENTITY makes no use of batch inserts • TABLE generator using a pooled optimizer with an increment size of 100
  • 29. IDENTITY vs TABLE (100 rows) 1 2 4 8 16 0 500 1000 1500 2000 2500 Thread count Time(ms) Identity Table
  • 30. AUTO: IDENTITY vs TABLE? • Prior to Hibernate 5, AUTO would resolve to IDENTITY if the database supports such a feature • Hibernate 5 uses TABLE generator if the database does not support sequences
  • 31. SEQUENCE vs TABLE (100 rows) • Both benefiting from JDBC batch inserts • Both using a pooled optimizer with an increment size of 100
  • 32. SEQUENCE vs TABLE (100 rows) 1 2 4 8 16 0 200 400 600 800 1000 1200 Thread count Time(ms) Sequence Table
  • 33. Agenda • Performance and Scaling • Connection providers • Identifier generators • Relationships • Batching • Fetching • Caching
  • 34. Relationships 𝑇 = 𝑡 𝑎𝑐𝑞 + 𝑡 𝑟𝑒𝑞 + 𝑡 𝑒𝑥𝑒𝑐 + 𝑡 𝑟𝑒𝑠 + 𝑡𝑖𝑑𝑙𝑒
  • 35. Agenda • Performance and Scaling • Connection providers • Identifier generators • Relationships • Batching • Fetching • Caching
  • 36. Batching 𝑇 = 𝑡 𝑎𝑐𝑞 + 𝑡 𝑟𝑒𝑞 + 𝑡 𝑒𝑥𝑒𝑐 + 𝑡 𝑟𝑒𝑠 + 𝑡𝑖𝑑𝑙𝑒 • SessionFactory setting • Session-level configuration since Hibernate 5.2
  • 38. Batching - Session doInJPA( this::entityManagerFactory, entityManager -> { entityManager.unwrap( Session.class ) .setJdbcBatchSize( 10 ); for ( long i = 0; i < entityCount; ++i ) { Person = new Person( i, String.format( "Person %d", i ) ); entityManager.persist( person ); if ( i % batchSize == 0 ) { entityManager.flush(); entityManager.clear(); } } } );
  • 39. Batching DEBUG [main]: n.t.d.l.SLF4JQueryLoggingListener – Name:DATA_SOURCE_PROXY, Time:1, Success:True, Type:Prepared, Batch:True, QuerySize:1, BatchSize:10, Query: ["insert into Person (name, id) values (?, ?)"], Params:[ (Person 1, 1), (Person 2, 2), (Person 3, 3), (Person 4, 4), (Person 5, 5), (Person 6, 6), (Person 7, 7), (Person 8, 8), (Person 9, 9), (Person 10, 10) ]
  • 40. Insert PreparedStatement batching (5k rows) 1 10 20 30 40 50 60 70 80 90 100 1000 0 200 400 600 800 1000 1200 1400 1600 Batch size Time(ms) DB_A DB_B DB_C DB_D
  • 41. Update PreparedStatement batching (5k rows) 1 10 20 30 40 50 60 70 80 90 100 1000 0 100 200 300 400 500 600 700 Batch size Time(ms) DB_A DB_B DB_C DB_D
  • 42. Delete PreparedStatement batching (5k rows) 1 10 20 30 40 50 60 70 80 90 100 1000 0 200 400 600 800 1000 1200 Batch size Time(ms) DB_A DB_B DB_C DB_D
  • 44. Batching – @Version <property name="hibernate.jdbc.batch_versioned_data" value="true"/> • Enabled by default in Hibernate 5 • Disabled in Hibernate 3.x, 4.x, and for Oracle 8i, 9i, and 10g dialects
  • 45. Agenda • Performance and Scaling • Connection providers • Identifier generators • Relationships • Batching • Fetching • Caching
  • 46. Fetching 𝑇 = 𝑡 𝑎𝑐𝑞 + 𝑡 𝑟𝑒𝑞 + 𝑡 𝑒𝑥𝑒𝑐 + 𝑡 𝑟𝑒𝑠 + 𝑡𝑖𝑑𝑙𝑒 • JDBC fetch size • JDBC ResultSet size • DTO vs Entity queries • Fetching relationships
  • 47. Fetching – JDBC Fetch Size • Oracle – Default fetch size is 10 • SQL Server – Adaptive buffering • PostgreSQL, MySQL – Fetch the whole ResultSet at once • SessionFactory setting: <property name="hibernate.jdbc.fetch_size" value="100"/>
  • 48. Fetching - JDBC fetch size • Query-level hint: List<PostCommentSummary> summaries = entityManager.createQuery( "select new PostCommentSummary( " + " p.id, p.title, c.review ) " + "from PostComment c " + "join c.post p") .setHint(QueryHints.HINT_FETCH_SIZE, fetchSize) .getResultList();
  • 49. Fetching – JDBC Fetch Size (10k rows) 1 10 100 1000 10000 0 100 200 300 400 500 600 Fetch size Time(ms) DB_A DB_B DB_C DB_D
  • 50. Fetching – Pagination • JPA / Hibernate API works for both entity and native queries List<PostCommentSummary> summaries = entityManager.createQuery( "select new PostCommentSummary( " + " p.id, p.title, c.review ) " + "from PostComment c " + "join c.post p") .setFirstResult(pageStart) .setMaxResults(pageSize) .getResultList();
  • 51. Fetching – 100k vs 100 rows Fetch all Fetch limit 0 500 1000 1500 2000 2500 3000 3500 4000 4500 5000 Time(ms) DB_A DB_B DB_C DB_D
  • 52. Fetching – Pagination • Hibernate uses OFFSET pagination • Keyset pagination scales better when navigating large result sets • http://use-the-index-luke.com/no-offset
  • 53. Fetching – Entity vs Projection • Selecting all columns vs a custom projection SELECT * FROM post_comment pc INNER JOIN post p ON p.id = pc.post_id INNER JOIN post_details pd ON p.id = pd.id SELECT pc.version FROM post_comment pc INNER JOIN post p ON p.id = pc.post_id INNER JOIN post_details pd ON p.id = pd.id
  • 54. Fetching – Entity vs Projection All columns Custom projection 0 5 10 15 20 25 30 Time(ms) DB_A DB_B DB_C DB_D
  • 55. Fetching – DTO Projections • Read-only views • Tree structures (Recursive CTE) • Paginated Tables • Analytics (Window functions)
  • 56. Fetching – Entity Queries • Writing data • Web flows / Multi-request logical transactions • Application-level repeatable reads • Detached entities / PersistenceContextType.EXTENDED • Optimistic concurrency control (e.g. version, dirty properties)
  • 57. Fetching – Relationships Association FetchType @ManyToOne EAGER @OneToOne EAGER @OneToMany LAZY @ManyToMany LAZY • LAZY associations can be fetched eagerly • EAGER associations cannot be fetched lazily
  • 58. Fetching – Best Practices • Default to FetchType.LAZY • Fetch directive in JPQL/Criteria API queries • Entity graphs / @FetchProfile • LazyInitializationException
  • 59. Fetching – Open Session in View Anti-Pattern
  • 60. Fetching – Temporary Session Anti-Pattern • “Band aid” for LazyInitializationException • One temporary Session/Connection for every lazily fetched association <property name="hibernate.enable_lazy_load_no_trans" value="true"/>
  • 61. Agenda • Performance and Scaling • Connection providers • Identifier generators • Relationships • Batching • Fetching • Caching
  • 62. Caching 𝑇 = 𝑡 𝑎𝑐𝑞 + 𝑡 𝑟𝑒𝑞 + 𝑡 𝑒𝑥𝑒𝑐 + 𝑡 𝑟𝑒𝑠 + 𝑡𝑖𝑑𝑙𝑒
  • 63. Caching – Why 2nd - Level Caching
  • 64. Caching – Why 2nd - Level Caching “There are only two hard things in Computer Science: cache invalidation and naming things.” Phil Karlton
  • 65. Caching – Strategies Strategy Cache type Particularity READ_ONLY READ-THROUGH Immutable NONSTRICT_READ_WRITE READ-THROUGH Invalidation/ Inconsistency risk READ_WRITE WRITE-THROUGH Soft Locks TRANSACTIONAL WRITE-THROUGH JTA
  • 66. Caching – Collection Cache • It complement entity caching • It stores only entity identifiers • Read-Through • Invalidation-based (Consistency over Performance)
  • 67. Caching – Read - Write Aggregates
  • 68. Questions and Answers https://leanpub.com/high-performance-java-persistence • Performance and Scaling • Connection providers • Identifier generators • Relationships • Batching • Fetching • Caching