SlideShare a Scribd company logo
1 of 77
Parallel processing with
      Spring Batch
     Lessons learned
Morten Andersen-Gott
•   Manager at Accenture Norway
•   30 years old
•   Been using Spring Batch since 1.0M2 (2007)
•   Member of JavaZone program committee
    – http://tinyurl.com/javaever
    – http://tinyurl.com/thestreaming
    – http://tinyurl.com/ladyjava




     mortenag
     www.github.com/magott
     www.andersen-gott.com
A BoF focusing on the problems

•   Functional background for the batch
•   Short introduction to Spring Batch
•   Even shorter on Hibernate
•   The problems
•   The problems
•   The problems
Norwegian Public Service
                            Pension Fund (SPK)
• Norway’s main provider of public occupational
  pensions
• Also provide housing loans and insurance schemes
• Membership of the Norwegian Public Service Pension
  Fund is obligatory for government employees
• Stats
   –   950,000 members across 1600 organisations
   –   Approx 138,000 receive a retirement pension
   –   Approx 58,000 receive a disability pension
   –   950,000 members have total accrued pension entitlements
       in the Norwegian Public Service Pension Fund of 339
       billion kroner.
Background
• Every year the parliament sets the basic amount
  of the national insurance
• This amount is a constant used in calculation of all
  benefits
• When the basic amount is changed, all benefits
  must be recalculated
• It’s more complex than a constant in an algorithm
   – Rules are result of political games the last 50 years
   – Complex rules
   – Lots of exemptions
Execution time requirements

• SPK’s recalculation batch must run
  – After the basic amount is set
  – After the Labour and Welfare Administration
    has done it’s calculation
  – Before the pensions are due next month
• Window of 1 week
  – Will ideally only run during weekends
  – Can not run while case workers are doing
    their job
Spring Batch

• Framework for developing batch
  applications
• Implements batch semantics
  – Steps, Chunks, Stop, Restart, Skips, Retries
  – Partitioning, Multithreading, Parallel steps
A step…
The components
• ItemReader
  – read() returns one row at the time
  – Step is completed once read()returns null
• ItemProcessor
  – process(item) item is return value from read()
  – Business logic goes here
  – Items can be filtered out by returning null
• ItemWriter
  – Write(list) list of items returned from
    process()
The code
<job id=”foo”>
   <step id="fileImport">
     <tasklet>
          <chunk commit-interval="10"
            reader=“reader"
            processor=“processor"
            writer=“writer“/>
     </tasklet>
   </step>
</job>
<bean id=“reader" class="...">
<bean id=“processor" class="...">
<bean id=“writer" class="...">
Chunk
• A chunk is a unit of work
  – Executes within a transaction
  – Size is defined by number of items read
• A step is divided into chunks by the
  framework
• When x is the chunk size
  – read() is called x times
  – process() is called x times
  – write is called once with a list where list.size()==x
    (minus filtered items)
Scaling
Multi-threaded step
Id   Name

1    Paul

2    John

3    Lisa




                                     Execution
               Reader




                                       Step
4    Simon
              Processor      T1..*
5    Rick       Writer

6    Julia

7    Olivia

8    Scott

9    Martin
Partitioned Step
Id   Name




                                       Execution
               Reader




                                         Step
1    Paul                       T1
              Processor
2    John       Writer

3    Lisa




                                       Execution
               Reader




                                         Step
4    Simon
              Processor         T2
5    Rick       Writer

6    Julia




                                       Execution
7    Olivia    Reader




                                         Step
              Processor         T3
8    Scott      Writer
9    Martin
Hibernate 101

• Proxy
• Session cache
• Flushing
  – Queries
  – Commit
The pension recalculation batch
- Stored procedure
Step 1
                  Populate staging table


                  - Read from staging
                  - Fetch additional data
Step 2   Rules    - Call out to rules engine
         engine   - Store result as pending
                  pensions



                  - Loop through pending
Step 3
                  pensions and activate


                  - Create manual tasks
Step 4
                  for failed items
Staging table

• A batch processing pattern
• A simple table with a identity column,
  functional key and processing status
• Restart is easy
  – Select soc_sec from staging where
    processed=false
• An easy way to get parallel processing
  capabilities
Our staging table(s)
Reader

           Updates
         family status


         Processor
Step 2                   ILog

                                Sybase


           Writer
Hibernate in the reader

• Using a separate statefull session in
  reader
  – Not bound to active transaction
  – Is never flushed
  – clear() is called before commit
• Entities are written to database using a
  (different) transaction bound session
  – Used by processor and writer
Problems?
Problems?
What happened?

org.hibernate.HibernateException: Illegal
  attempt to associate a collection with two
  open sessions
Why

• Family.errors and Family.persons are
  attached to reader’s session
• Attempting to attach them to transaction
  bound session
• Hibernate will have non of that!
Problems?
The solution?
Stateless sessions
Hibernate in the reader
                                (Second attempt)


• Let’s try stateless session
• Default behavior in Spring Batch’s
  Hibernate ItemReaders
  – useSateless=”true”
• LEFT JOIN FETCH for eager loading
  collections
  – Avoiding LazyLoadingExceptions
Hibernate in the reader
            (Second attempt)
Problems?
Problems?
What happened?

• org.hibernate.HibernateException: cannot
  simultaneously fetch multiple bags
Why

• Hibernate is unable to resolve the
  Cartesian product
  – Throws exception to avoid duplicates
You are using it wrong!
Curing the
                    Problems?
you are using it wrong
syndrome
Hibernate in the reader
                               (The third attempt)

• Examine the object graph
• Replace List with Set
  – Only one eagerly loaded collection may be of
    type list List
• This works…
  – ..for a while
  – We’ll revisit Hibernate later…
Exception resilience

• New demands sneak in
  – The batch should not abort
     • Not under any circumstance
• The batch should deal with
  – Missing or functionally corrupt data
  – Programming errors
  – ...
Pseudo code
try{
  //do data and business operations
}catch(Exception e){
   //Add error to staging & continue
   family.addError(createError(e));
}
Problems?
Problems?
Overzealous exception handling

an assertion failure occurred (this may
  indicate a bug in Hibernate, but is more
  likely due to unsafe use of the session)
Overzealous exception handeling
                               The solution

• Some exception MUST result in a rollback
  – Ex. StaleStateException
• Configure the framework to do retry/skip
  for these
• Only catch exceptions you know you can
  handle in a meaningful way
  – Nothing new here
  – Do not succumb to crazy requirements
Time to turn on parallelization

• We chose partitioned over mutli-threaded
  step
  – No need for a thread safe reader
    • Step scope
    • Each partition get a new instance of reader
  – Page lock contentions are less likely
    • Row 1 in partition 1 not adjacent to row 1 in
      partition 2
Deadlocks

• Legacy database using page locking
• Normalized database
• Relevant data for one person is spread
  across a number of tables
• Different threads will access same data
  pages
• Deadlocks will occur
Page locking
ID   NAME
1    Paul       T1
2    John       T2 waiting
3    Simon
4    Scott     T1 waiting

5    Lisa      T2
6    Jack
7    Nina
8    Linda      T3
DeadlockLoserDataAccessException
Retry to the rescue
<step id=”step2">
 <tasklet>
   <chunk reader="reader" processor="processor" writer="writer"
      commit-interval="10" retry-limit="10">
         <retryable-exception-classes>
            <include class=”…DeadlockLoserDataAccessException"/>
         </retryable-exception-classes>
   </chunk>
  </tasklet>
</step>
The mystery exception
Problems?
Two weeks later..
#42
Problems?
Retry
Id=42




StaleState
Exception
What do we do?
What we should have done weeks ago
We ditch Hibernate
…well, almost anyway
Removing Hibernate from
                                   reader

• ItemReader is re-configured to use JDBC
• Fetches primary key from family staging
  table
• ItemProcesser fetches staging object
  graph
  – Uses primary key to fetch graph with
    hibernate
• Primary keys are immutable and stateless
End result

•   Performance requirement was 48 hrs
•   Completed in 16 hrs
•   Used 12 threads
•   C-version used 1 thread and ran for 1 week
    – Stopped each morning, started each evening
• A batch that scales with the infrastructure
    – Number of threads is configurable in .properties
What we would have done
                                differently

• Switched from partioned to multi-threaded
  step
  – All work is shared among threads
  – All threads will run until batch completes
  – Avoid idle threads towards the end
  – With partitioning some partitions finished well
    before others
Recommendations
• Do not use Hibernate in the ItemReader
• Test parallelization early
• Monitor your SQLs
  – Frequently called
  – Long running
• Become friends with your DBA
• There is no reason to let Java be the bottle
  neck
  – Increase thread count until DB becomes the
    bottle neck
Thanks!



@mortenag
www.github.com/magott
www.andersen-gott.com
Pro tip: @BatchSize
• If one lazily loaded entity is fetched, they
  are all fetched – in one query

More Related Content

What's hot

Java Performance Analysis on Linux with Flame Graphs
Java Performance Analysis on Linux with Flame GraphsJava Performance Analysis on Linux with Flame Graphs
Java Performance Analysis on Linux with Flame GraphsBrendan Gregg
 
From zero to hero Backing up alfresco
From zero to hero Backing up alfrescoFrom zero to hero Backing up alfresco
From zero to hero Backing up alfrescoToni de la Fuente
 
Spring Batch Introduction (and Bitbucket Project)
Spring Batch Introduction (and Bitbucket Project)Spring Batch Introduction (and Bitbucket Project)
Spring Batch Introduction (and Bitbucket Project)Guillermo Daniel Salazar
 
Presto best practices for Cluster admins, data engineers and analysts
Presto best practices for Cluster admins, data engineers and analystsPresto best practices for Cluster admins, data engineers and analysts
Presto best practices for Cluster admins, data engineers and analystsShubham Tagra
 
Play with FILE Structure - Yet Another Binary Exploit Technique
Play with FILE Structure - Yet Another Binary Exploit TechniquePlay with FILE Structure - Yet Another Binary Exploit Technique
Play with FILE Structure - Yet Another Binary Exploit TechniqueAngel Boy
 
MySQL Performance Schema in 20 Minutes
 MySQL Performance Schema in 20 Minutes MySQL Performance Schema in 20 Minutes
MySQL Performance Schema in 20 MinutesSveta Smirnova
 
Hudi architecture, fundamentals and capabilities
Hudi architecture, fundamentals and capabilitiesHudi architecture, fundamentals and capabilities
Hudi architecture, fundamentals and capabilitiesNishith Agarwal
 
Apache Hudi: The Path Forward
Apache Hudi: The Path ForwardApache Hudi: The Path Forward
Apache Hudi: The Path ForwardAlluxio, Inc.
 
RocksDB Performance and Reliability Practices
RocksDB Performance and Reliability PracticesRocksDB Performance and Reliability Practices
RocksDB Performance and Reliability PracticesYoshinori Matsunobu
 
Ndb cluster 80_ycsb_mem
Ndb cluster 80_ycsb_memNdb cluster 80_ycsb_mem
Ndb cluster 80_ycsb_memmikaelronstrom
 
Microservices with Kafka Ecosystem
Microservices with Kafka EcosystemMicroservices with Kafka Ecosystem
Microservices with Kafka EcosystemGuido Schmutz
 
Gitlab ci e kubernetes, build test and deploy your projects like a pro
Gitlab ci e kubernetes, build test and deploy your projects like a proGitlab ci e kubernetes, build test and deploy your projects like a pro
Gitlab ci e kubernetes, build test and deploy your projects like a prosparkfabrik
 
Ceph Block Devices: A Deep Dive
Ceph Block Devices:  A Deep DiveCeph Block Devices:  A Deep Dive
Ceph Block Devices: A Deep DiveRed_Hat_Storage
 
Zabbix Performance Tuning
Zabbix Performance TuningZabbix Performance Tuning
Zabbix Performance TuningRicardo Santos
 
Monitoring using Prometheus and Grafana
Monitoring using Prometheus and GrafanaMonitoring using Prometheus and Grafana
Monitoring using Prometheus and GrafanaArvind Kumar G.S
 
Introducing GitLab (June 2018)
Introducing GitLab (June 2018)Introducing GitLab (June 2018)
Introducing GitLab (June 2018)Noa Harel
 
Binary exploitation - AIS3
Binary exploitation - AIS3Binary exploitation - AIS3
Binary exploitation - AIS3Angel Boy
 
Performance tuning in BlueStore & RocksDB - Li Xiaoyan
Performance tuning in BlueStore & RocksDB - Li XiaoyanPerformance tuning in BlueStore & RocksDB - Li Xiaoyan
Performance tuning in BlueStore & RocksDB - Li XiaoyanCeph Community
 

What's hot (20)

Java Performance Analysis on Linux with Flame Graphs
Java Performance Analysis on Linux with Flame GraphsJava Performance Analysis on Linux with Flame Graphs
Java Performance Analysis on Linux with Flame Graphs
 
From zero to hero Backing up alfresco
From zero to hero Backing up alfrescoFrom zero to hero Backing up alfresco
From zero to hero Backing up alfresco
 
Spring Batch Introduction (and Bitbucket Project)
Spring Batch Introduction (and Bitbucket Project)Spring Batch Introduction (and Bitbucket Project)
Spring Batch Introduction (and Bitbucket Project)
 
Presto best practices for Cluster admins, data engineers and analysts
Presto best practices for Cluster admins, data engineers and analystsPresto best practices for Cluster admins, data engineers and analysts
Presto best practices for Cluster admins, data engineers and analysts
 
Play with FILE Structure - Yet Another Binary Exploit Technique
Play with FILE Structure - Yet Another Binary Exploit TechniquePlay with FILE Structure - Yet Another Binary Exploit Technique
Play with FILE Structure - Yet Another Binary Exploit Technique
 
MySQL Performance Schema in 20 Minutes
 MySQL Performance Schema in 20 Minutes MySQL Performance Schema in 20 Minutes
MySQL Performance Schema in 20 Minutes
 
Hudi architecture, fundamentals and capabilities
Hudi architecture, fundamentals and capabilitiesHudi architecture, fundamentals and capabilities
Hudi architecture, fundamentals and capabilities
 
Apache Hudi: The Path Forward
Apache Hudi: The Path ForwardApache Hudi: The Path Forward
Apache Hudi: The Path Forward
 
RocksDB Performance and Reliability Practices
RocksDB Performance and Reliability PracticesRocksDB Performance and Reliability Practices
RocksDB Performance and Reliability Practices
 
Ndb cluster 80_ycsb_mem
Ndb cluster 80_ycsb_memNdb cluster 80_ycsb_mem
Ndb cluster 80_ycsb_mem
 
Microservices with Kafka Ecosystem
Microservices with Kafka EcosystemMicroservices with Kafka Ecosystem
Microservices with Kafka Ecosystem
 
Gitlab ci e kubernetes, build test and deploy your projects like a pro
Gitlab ci e kubernetes, build test and deploy your projects like a proGitlab ci e kubernetes, build test and deploy your projects like a pro
Gitlab ci e kubernetes, build test and deploy your projects like a pro
 
Ceph Block Devices: A Deep Dive
Ceph Block Devices:  A Deep DiveCeph Block Devices:  A Deep Dive
Ceph Block Devices: A Deep Dive
 
Zabbix Performance Tuning
Zabbix Performance TuningZabbix Performance Tuning
Zabbix Performance Tuning
 
Monitoring using Prometheus and Grafana
Monitoring using Prometheus and GrafanaMonitoring using Prometheus and Grafana
Monitoring using Prometheus and Grafana
 
Git 101
Git 101Git 101
Git 101
 
Introducing GitLab (June 2018)
Introducing GitLab (June 2018)Introducing GitLab (June 2018)
Introducing GitLab (June 2018)
 
Binary exploitation - AIS3
Binary exploitation - AIS3Binary exploitation - AIS3
Binary exploitation - AIS3
 
Git and Github Session
Git and Github SessionGit and Github Session
Git and Github Session
 
Performance tuning in BlueStore & RocksDB - Li Xiaoyan
Performance tuning in BlueStore & RocksDB - Li XiaoyanPerformance tuning in BlueStore & RocksDB - Li Xiaoyan
Performance tuning in BlueStore & RocksDB - Li Xiaoyan
 

Similar to Parallel batch processing with spring batch slideshare

Batching and Java EE (jdk.io)
Batching and Java EE (jdk.io)Batching and Java EE (jdk.io)
Batching and Java EE (jdk.io)Ryan Cuprak
 
2013 06-03 berlin buzzwords
2013 06-03 berlin buzzwords2013 06-03 berlin buzzwords
2013 06-03 berlin buzzwordsNitay Joffe
 
2013.09.10 Giraph at London Hadoop Users Group
2013.09.10 Giraph at London Hadoop Users Group2013.09.10 Giraph at London Hadoop Users Group
2013.09.10 Giraph at London Hadoop Users GroupNitay Joffe
 
Asynchronous Awesome
Asynchronous AwesomeAsynchronous Awesome
Asynchronous AwesomeFlip Sasser
 
Scala in the Wild
Scala in the WildScala in the Wild
Scala in the WildTomer Gabel
 
Parallel programming
Parallel programmingParallel programming
Parallel programmingSwain Loda
 
Optimizing thread performance for a genomics variant caller
Optimizing thread performance for a genomics variant callerOptimizing thread performance for a genomics variant caller
Optimizing thread performance for a genomics variant callerAllineaSoftware
 
Cassandra Day Atlanta 2015: Diagnosing Problems in Production
Cassandra Day Atlanta 2015: Diagnosing Problems in ProductionCassandra Day Atlanta 2015: Diagnosing Problems in Production
Cassandra Day Atlanta 2015: Diagnosing Problems in ProductionDataStax Academy
 
Cassandra Day Chicago 2015: Diagnosing Problems in Production
Cassandra Day Chicago 2015: Diagnosing Problems in ProductionCassandra Day Chicago 2015: Diagnosing Problems in Production
Cassandra Day Chicago 2015: Diagnosing Problems in ProductionDataStax Academy
 
Cassandra Day London 2015: Diagnosing Problems in Production
Cassandra Day London 2015: Diagnosing Problems in ProductionCassandra Day London 2015: Diagnosing Problems in Production
Cassandra Day London 2015: Diagnosing Problems in ProductionDataStax Academy
 
Eureka Moment UKLUG
Eureka Moment UKLUGEureka Moment UKLUG
Eureka Moment UKLUGPaul Withers
 
Silicon Valley Code Camp 2015 - Advanced MongoDB - The Sequel
Silicon Valley Code Camp 2015 - Advanced MongoDB - The SequelSilicon Valley Code Camp 2015 - Advanced MongoDB - The Sequel
Silicon Valley Code Camp 2015 - Advanced MongoDB - The SequelDaniel Coupal
 
Diagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - CassandraDiagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - CassandraJon Haddad
 
Treasure Data Summer Internship 2016
Treasure Data Summer Internship 2016Treasure Data Summer Internship 2016
Treasure Data Summer Internship 2016Yuta Iwama
 
Performance Scenario: Diagnosing and resolving sudden slow down on two node RAC
Performance Scenario: Diagnosing and resolving sudden slow down on two node RACPerformance Scenario: Diagnosing and resolving sudden slow down on two node RAC
Performance Scenario: Diagnosing and resolving sudden slow down on two node RACKristofferson A
 
Groovy concurrency
Groovy concurrencyGroovy concurrency
Groovy concurrencyAlex Miller
 
Infinispan from POC to Production
Infinispan from POC to ProductionInfinispan from POC to Production
Infinispan from POC to ProductionJBUG London
 
Infinispan from POC to Production
Infinispan from POC to ProductionInfinispan from POC to Production
Infinispan from POC to ProductionC2B2 Consulting
 
Case Study of the Unexplained
Case Study of the UnexplainedCase Study of the Unexplained
Case Study of the Unexplainedshannomc
 
Distributed system coordination by zookeeper and introduction to kazoo python...
Distributed system coordination by zookeeper and introduction to kazoo python...Distributed system coordination by zookeeper and introduction to kazoo python...
Distributed system coordination by zookeeper and introduction to kazoo python...Jimmy Lai
 

Similar to Parallel batch processing with spring batch slideshare (20)

Batching and Java EE (jdk.io)
Batching and Java EE (jdk.io)Batching and Java EE (jdk.io)
Batching and Java EE (jdk.io)
 
2013 06-03 berlin buzzwords
2013 06-03 berlin buzzwords2013 06-03 berlin buzzwords
2013 06-03 berlin buzzwords
 
2013.09.10 Giraph at London Hadoop Users Group
2013.09.10 Giraph at London Hadoop Users Group2013.09.10 Giraph at London Hadoop Users Group
2013.09.10 Giraph at London Hadoop Users Group
 
Asynchronous Awesome
Asynchronous AwesomeAsynchronous Awesome
Asynchronous Awesome
 
Scala in the Wild
Scala in the WildScala in the Wild
Scala in the Wild
 
Parallel programming
Parallel programmingParallel programming
Parallel programming
 
Optimizing thread performance for a genomics variant caller
Optimizing thread performance for a genomics variant callerOptimizing thread performance for a genomics variant caller
Optimizing thread performance for a genomics variant caller
 
Cassandra Day Atlanta 2015: Diagnosing Problems in Production
Cassandra Day Atlanta 2015: Diagnosing Problems in ProductionCassandra Day Atlanta 2015: Diagnosing Problems in Production
Cassandra Day Atlanta 2015: Diagnosing Problems in Production
 
Cassandra Day Chicago 2015: Diagnosing Problems in Production
Cassandra Day Chicago 2015: Diagnosing Problems in ProductionCassandra Day Chicago 2015: Diagnosing Problems in Production
Cassandra Day Chicago 2015: Diagnosing Problems in Production
 
Cassandra Day London 2015: Diagnosing Problems in Production
Cassandra Day London 2015: Diagnosing Problems in ProductionCassandra Day London 2015: Diagnosing Problems in Production
Cassandra Day London 2015: Diagnosing Problems in Production
 
Eureka Moment UKLUG
Eureka Moment UKLUGEureka Moment UKLUG
Eureka Moment UKLUG
 
Silicon Valley Code Camp 2015 - Advanced MongoDB - The Sequel
Silicon Valley Code Camp 2015 - Advanced MongoDB - The SequelSilicon Valley Code Camp 2015 - Advanced MongoDB - The Sequel
Silicon Valley Code Camp 2015 - Advanced MongoDB - The Sequel
 
Diagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - CassandraDiagnosing Problems in Production - Cassandra
Diagnosing Problems in Production - Cassandra
 
Treasure Data Summer Internship 2016
Treasure Data Summer Internship 2016Treasure Data Summer Internship 2016
Treasure Data Summer Internship 2016
 
Performance Scenario: Diagnosing and resolving sudden slow down on two node RAC
Performance Scenario: Diagnosing and resolving sudden slow down on two node RACPerformance Scenario: Diagnosing and resolving sudden slow down on two node RAC
Performance Scenario: Diagnosing and resolving sudden slow down on two node RAC
 
Groovy concurrency
Groovy concurrencyGroovy concurrency
Groovy concurrency
 
Infinispan from POC to Production
Infinispan from POC to ProductionInfinispan from POC to Production
Infinispan from POC to Production
 
Infinispan from POC to Production
Infinispan from POC to ProductionInfinispan from POC to Production
Infinispan from POC to Production
 
Case Study of the Unexplained
Case Study of the UnexplainedCase Study of the Unexplained
Case Study of the Unexplained
 
Distributed system coordination by zookeeper and introduction to kazoo python...
Distributed system coordination by zookeeper and introduction to kazoo python...Distributed system coordination by zookeeper and introduction to kazoo python...
Distributed system coordination by zookeeper and introduction to kazoo python...
 

Recently uploaded

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 

Recently uploaded (20)

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 

Parallel batch processing with spring batch slideshare

  • 1. Parallel processing with Spring Batch Lessons learned
  • 2. Morten Andersen-Gott • Manager at Accenture Norway • 30 years old • Been using Spring Batch since 1.0M2 (2007) • Member of JavaZone program committee – http://tinyurl.com/javaever – http://tinyurl.com/thestreaming – http://tinyurl.com/ladyjava mortenag www.github.com/magott www.andersen-gott.com
  • 3. A BoF focusing on the problems • Functional background for the batch • Short introduction to Spring Batch • Even shorter on Hibernate • The problems • The problems • The problems
  • 4. Norwegian Public Service Pension Fund (SPK) • Norway’s main provider of public occupational pensions • Also provide housing loans and insurance schemes • Membership of the Norwegian Public Service Pension Fund is obligatory for government employees • Stats – 950,000 members across 1600 organisations – Approx 138,000 receive a retirement pension – Approx 58,000 receive a disability pension – 950,000 members have total accrued pension entitlements in the Norwegian Public Service Pension Fund of 339 billion kroner.
  • 5. Background • Every year the parliament sets the basic amount of the national insurance • This amount is a constant used in calculation of all benefits • When the basic amount is changed, all benefits must be recalculated • It’s more complex than a constant in an algorithm – Rules are result of political games the last 50 years – Complex rules – Lots of exemptions
  • 6. Execution time requirements • SPK’s recalculation batch must run – After the basic amount is set – After the Labour and Welfare Administration has done it’s calculation – Before the pensions are due next month • Window of 1 week – Will ideally only run during weekends – Can not run while case workers are doing their job
  • 7. Spring Batch • Framework for developing batch applications • Implements batch semantics – Steps, Chunks, Stop, Restart, Skips, Retries – Partitioning, Multithreading, Parallel steps
  • 9. The components • ItemReader – read() returns one row at the time – Step is completed once read()returns null • ItemProcessor – process(item) item is return value from read() – Business logic goes here – Items can be filtered out by returning null • ItemWriter – Write(list) list of items returned from process()
  • 10. The code <job id=”foo”> <step id="fileImport"> <tasklet> <chunk commit-interval="10" reader=“reader" processor=“processor" writer=“writer“/> </tasklet> </step> </job> <bean id=“reader" class="..."> <bean id=“processor" class="..."> <bean id=“writer" class="...">
  • 11. Chunk • A chunk is a unit of work – Executes within a transaction – Size is defined by number of items read • A step is divided into chunks by the framework • When x is the chunk size – read() is called x times – process() is called x times – write is called once with a list where list.size()==x (minus filtered items)
  • 13. Multi-threaded step Id Name 1 Paul 2 John 3 Lisa Execution Reader Step 4 Simon Processor T1..* 5 Rick Writer 6 Julia 7 Olivia 8 Scott 9 Martin
  • 14. Partitioned Step Id Name Execution Reader Step 1 Paul T1 Processor 2 John Writer 3 Lisa Execution Reader Step 4 Simon Processor T2 5 Rick Writer 6 Julia Execution 7 Olivia Reader Step Processor T3 8 Scott Writer 9 Martin
  • 15. Hibernate 101 • Proxy • Session cache • Flushing – Queries – Commit
  • 17. - Stored procedure Step 1 Populate staging table - Read from staging - Fetch additional data Step 2 Rules - Call out to rules engine engine - Store result as pending pensions - Loop through pending Step 3 pensions and activate - Create manual tasks Step 4 for failed items
  • 18. Staging table • A batch processing pattern • A simple table with a identity column, functional key and processing status • Restart is easy – Select soc_sec from staging where processed=false • An easy way to get parallel processing capabilities
  • 20. Reader Updates family status Processor Step 2 ILog Sybase Writer
  • 21. Hibernate in the reader • Using a separate statefull session in reader – Not bound to active transaction – Is never flushed – clear() is called before commit • Entities are written to database using a (different) transaction bound session – Used by processor and writer
  • 22.
  • 24. What happened? org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
  • 25. Why • Family.errors and Family.persons are attached to reader’s session • Attempting to attach them to transaction bound session • Hibernate will have non of that!
  • 28. Hibernate in the reader (Second attempt) • Let’s try stateless session • Default behavior in Spring Batch’s Hibernate ItemReaders – useSateless=”true” • LEFT JOIN FETCH for eager loading collections – Avoiding LazyLoadingExceptions
  • 29. Hibernate in the reader (Second attempt)
  • 31. What happened? • org.hibernate.HibernateException: cannot simultaneously fetch multiple bags
  • 32. Why • Hibernate is unable to resolve the Cartesian product – Throws exception to avoid duplicates
  • 33. You are using it wrong!
  • 34. Curing the Problems? you are using it wrong syndrome
  • 35. Hibernate in the reader (The third attempt) • Examine the object graph • Replace List with Set – Only one eagerly loaded collection may be of type list List • This works… – ..for a while – We’ll revisit Hibernate later…
  • 36. Exception resilience • New demands sneak in – The batch should not abort • Not under any circumstance • The batch should deal with – Missing or functionally corrupt data – Programming errors – ...
  • 37. Pseudo code try{ //do data and business operations }catch(Exception e){ //Add error to staging & continue family.addError(createError(e)); }
  • 39. Overzealous exception handling an assertion failure occurred (this may indicate a bug in Hibernate, but is more likely due to unsafe use of the session)
  • 40. Overzealous exception handeling The solution • Some exception MUST result in a rollback – Ex. StaleStateException • Configure the framework to do retry/skip for these • Only catch exceptions you know you can handle in a meaningful way – Nothing new here – Do not succumb to crazy requirements
  • 41. Time to turn on parallelization • We chose partitioned over mutli-threaded step – No need for a thread safe reader • Step scope • Each partition get a new instance of reader – Page lock contentions are less likely • Row 1 in partition 1 not adjacent to row 1 in partition 2
  • 42. Deadlocks • Legacy database using page locking • Normalized database • Relevant data for one person is spread across a number of tables • Different threads will access same data pages • Deadlocks will occur
  • 43. Page locking ID NAME 1 Paul T1 2 John T2 waiting 3 Simon 4 Scott T1 waiting 5 Lisa T2 6 Jack 7 Nina 8 Linda T3
  • 45. Retry to the rescue <step id=”step2"> <tasklet> <chunk reader="reader" processor="processor" writer="writer" commit-interval="10" retry-limit="10"> <retryable-exception-classes> <include class=”…DeadlockLoserDataAccessException"/> </retryable-exception-classes> </chunk> </tasklet> </step>
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57. #42
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 67. What do we do?
  • 68. What we should have done weeks ago
  • 70. Removing Hibernate from reader • ItemReader is re-configured to use JDBC • Fetches primary key from family staging table • ItemProcesser fetches staging object graph – Uses primary key to fetch graph with hibernate • Primary keys are immutable and stateless
  • 71.
  • 72.
  • 73. End result • Performance requirement was 48 hrs • Completed in 16 hrs • Used 12 threads • C-version used 1 thread and ran for 1 week – Stopped each morning, started each evening • A batch that scales with the infrastructure – Number of threads is configurable in .properties
  • 74. What we would have done differently • Switched from partioned to multi-threaded step – All work is shared among threads – All threads will run until batch completes – Avoid idle threads towards the end – With partitioning some partitions finished well before others
  • 75. Recommendations • Do not use Hibernate in the ItemReader • Test parallelization early • Monitor your SQLs – Frequently called – Long running • Become friends with your DBA • There is no reason to let Java be the bottle neck – Increase thread count until DB becomes the bottle neck
  • 77. Pro tip: @BatchSize • If one lazily loaded entity is fetched, they are all fetched – in one query

Editor's Notes

  1. trials and tribulationsfairy tale
  2. Offers a lot of different benefits for employees or former employees of the public sector in NorwayPlays a major part in the personal economy for a lot of Norwegian citizens
  3. To reflect changes in consumer price indexBenefits
  4. Labour and Wellfare Administration provides the minimum pensions for all Norwegian citizensWe rely on the calculations from the Wellfare Administration in our batch
  5. 2 loopsOuter: Loops while read returns dataInner: Loops over read+process according to commit-interval
  6. Data is partitioned before step executesThreads are processing isolated data setsExamples of partitioning strategiesOne file per partitionPartitioning a tablePartition 1: Row 1 – Row 1000Partition 2: Row 1001 – Row 2000Partition 3: Row 2001 – Row 3000
  7. Indicates that session contained stale dataThrown when a version number or timestamp check failsAlso occurs if we try delete or update a row that does not existStarted doing the numbers, how much can we do within 48 hours with a single thread?Weknewthattwothreadswould never accessthe same row. Same page, sure, but never same row.
  8. The ItemReader is forward-onlyWe can’t roll back a cursor next()Spring batch stores all read items in a retry cache until transaction is committedUses the cache for retries
  9. Hibernatethrows an exceptionwhenrealitydoes not match itsexpectations