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

From Generator to Fiber the Road to Coroutine in PHP
From Generator to Fiber the Road to Coroutine in PHPFrom Generator to Fiber the Road to Coroutine in PHP
From Generator to Fiber the Road to Coroutine in PHPAlbert Chen
 
Exactly-once Stream Processing with Kafka Streams
Exactly-once Stream Processing with Kafka StreamsExactly-once Stream Processing with Kafka Streams
Exactly-once Stream Processing with Kafka StreamsGuozhang Wang
 
XXE: How to become a Jedi
XXE: How to become a JediXXE: How to become a Jedi
XXE: How to become a JediYaroslav Babin
 
Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!Jakub Kubrynski
 
Postgresql on NFS - J.Battiato, pgday2016
Postgresql on NFS - J.Battiato, pgday2016Postgresql on NFS - J.Battiato, pgday2016
Postgresql on NFS - J.Battiato, pgday2016Jonathan Battiato
 
[오픈소스컨설팅]Zabbix Installation and Configuration Guide
[오픈소스컨설팅]Zabbix Installation and Configuration Guide[오픈소스컨설팅]Zabbix Installation and Configuration Guide
[오픈소스컨설팅]Zabbix Installation and Configuration GuideJi-Woong Choi
 
How to Avoid Common Mistakes When Using Reactor Netty
How to Avoid Common Mistakes When Using Reactor NettyHow to Avoid Common Mistakes When Using Reactor Netty
How to Avoid Common Mistakes When Using Reactor NettyVMware Tanzu
 
Spring batch for large enterprises operations
Spring batch for large enterprises operations Spring batch for large enterprises operations
Spring batch for large enterprises operations Ignasi González
 
Git Tutorial
Git TutorialGit Tutorial
Git TutorialMDLicht
 
Scouter와 influx db – grafana 연동 가이드
Scouter와 influx db – grafana 연동 가이드Scouter와 influx db – grafana 연동 가이드
Scouter와 influx db – grafana 연동 가이드Ji-Woong Choi
 
Spring boot
Spring bootSpring boot
Spring bootsdeeg
 
Git - An Introduction
Git - An IntroductionGit - An Introduction
Git - An IntroductionBehzad Altaf
 
Spring Security 5.5 From Taxi to Takeoff
Spring Security 5.5 From Taxi to TakeoffSpring Security 5.5 From Taxi to Takeoff
Spring Security 5.5 From Taxi to TakeoffVMware Tanzu
 
Formation JPA Avancé / Hibernate gratuite par Ippon 2014
Formation JPA Avancé / Hibernate gratuite par Ippon 2014Formation JPA Avancé / Hibernate gratuite par Ippon 2014
Formation JPA Avancé / Hibernate gratuite par Ippon 2014Ippon
 
An Introduction to Gradle for Java Developers
An Introduction to Gradle for Java DevelopersAn Introduction to Gradle for Java Developers
An Introduction to Gradle for Java DevelopersKostas Saidis
 

What's hot (20)

From Generator to Fiber the Road to Coroutine in PHP
From Generator to Fiber the Road to Coroutine in PHPFrom Generator to Fiber the Road to Coroutine in PHP
From Generator to Fiber the Road to Coroutine in PHP
 
Exactly-once Stream Processing with Kafka Streams
Exactly-once Stream Processing with Kafka StreamsExactly-once Stream Processing with Kafka Streams
Exactly-once Stream Processing with Kafka Streams
 
Git basics
Git basicsGit basics
Git basics
 
XXE: How to become a Jedi
XXE: How to become a JediXXE: How to become a Jedi
XXE: How to become a Jedi
 
Basic Git Intro
Basic Git IntroBasic Git Intro
Basic Git Intro
 
Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!
 
Postgresql on NFS - J.Battiato, pgday2016
Postgresql on NFS - J.Battiato, pgday2016Postgresql on NFS - J.Battiato, pgday2016
Postgresql on NFS - J.Battiato, pgday2016
 
[오픈소스컨설팅]Zabbix Installation and Configuration Guide
[오픈소스컨설팅]Zabbix Installation and Configuration Guide[오픈소스컨설팅]Zabbix Installation and Configuration Guide
[오픈소스컨설팅]Zabbix Installation and Configuration Guide
 
How to Avoid Common Mistakes When Using Reactor Netty
How to Avoid Common Mistakes When Using Reactor NettyHow to Avoid Common Mistakes When Using Reactor Netty
How to Avoid Common Mistakes When Using Reactor Netty
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
 
Git
GitGit
Git
 
Spring batch for large enterprises operations
Spring batch for large enterprises operations Spring batch for large enterprises operations
Spring batch for large enterprises operations
 
Git Tutorial
Git TutorialGit Tutorial
Git Tutorial
 
Scouter와 influx db – grafana 연동 가이드
Scouter와 influx db – grafana 연동 가이드Scouter와 influx db – grafana 연동 가이드
Scouter와 influx db – grafana 연동 가이드
 
Spring boot
Spring bootSpring boot
Spring boot
 
Git - An Introduction
Git - An IntroductionGit - An Introduction
Git - An Introduction
 
Configuration management with Chef
Configuration management with ChefConfiguration management with Chef
Configuration management with Chef
 
Spring Security 5.5 From Taxi to Takeoff
Spring Security 5.5 From Taxi to TakeoffSpring Security 5.5 From Taxi to Takeoff
Spring Security 5.5 From Taxi to Takeoff
 
Formation JPA Avancé / Hibernate gratuite par Ippon 2014
Formation JPA Avancé / Hibernate gratuite par Ippon 2014Formation JPA Avancé / Hibernate gratuite par Ippon 2014
Formation JPA Avancé / Hibernate gratuite par Ippon 2014
 
An Introduction to Gradle for Java Developers
An Introduction to Gradle for Java DevelopersAn Introduction to Gradle for Java Developers
An Introduction to Gradle for Java Developers
 

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

The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 

Recently uploaded (20)

The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 

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