SlideShare a Scribd company logo
1 of 15
Download to read offline
Clojure’s
                            Software
                            Transactional
                            Memory



                                @fronx at @cljugb 12/2011

Thursday, December 15, 11
what’s it good for?




Thursday, December 15, 11
it’s pretty much useless

                                    unless you have:

                                    concurrency
                                    shared state
                                  changes to state



Thursday, December 15, 11
the pitch
                                    “It allows you to do
                                    coordinated change
                                    without the complexity
                                    of locking.”
                                    — Rich Hickey
                                    http://www.infoq.com/interviews/hickey-clojure




Thursday, December 15, 11
kind of like
                            database transactions.

                                but in memory.


Thursday, December 15, 11
example
              shared state      1   9       5   7       3   10       4
                                                                                     2   6   8


          changing state                                                     4

                                                                         4

                      threads           a
                                                    b            c               d
                                                                                     e



                                                                 solution: do it atomically!

Thursday, December 15, 11
user=> (def my-atom (atom 0))
                            #'user/my-atom
                                                                    atoms
                                                                    (No STM here.)
                            user=> @my-atom
                            0

                            user=> (swap! my-atom inc)
                            1

                            user=> @my-atom
                            1

                            user=> (swap! my-atom (fn [n] (* (+ n n) 2)))
                            4
Thursday, December 15, 11
broken example
                              (using atoms)




Thursday, December 15, 11
changing state

                            atomic   (acts as one point in time)

                            consistent    (from valid state to valid state)

                            isolated   (changes are local until committed)




Thursday, December 15, 11
sharing state
                            mutate in
                            STM only!
                                              coordinated   independent

                                synchronous      ref          atom

                               asynchronous       –          agent        receive message
                                                                             and return




Thursday, December 15, 11
STM functions
                               ref    reference to a collection
                             dosync   transaction
                            set-ref   set new value
                             alter    set new value via a function
                            commute   set new value and don’t block
                             ensure   block writes by others
                               io!    IllegalStateException


Thursday, December 15, 11
working and slightly
                              different example
                                            (using STM)




Thursday, December 15, 11
STM functions
                               ref    reference to a collection
                             dosync   transaction
                            set-ref   set new value
                             alter    set new value via a function
                            commute   set new value and don’t block
                             ensure   block writes by others
                               io!    IllegalStateException


Thursday, December 15, 11
what to think about
                            is it okay if others make changes in parallel?
                                  commute

                            multi-ref constraints?
                                  ensure




Thursday, December 15, 11
how does it work?
                            MVCC, snapshot isolation
                            persistent data structures
                            on-commit evaluation
                            http://java.ociweb.com/mark/stm/article.html




Thursday, December 15, 11

More Related Content

Similar to Introduction to Clojure's STM

AlphaBox Technology Overview
AlphaBox Technology OverviewAlphaBox Technology Overview
AlphaBox Technology Overviewmonica_singh
 
Day 9 combining like terms
Day 9 combining like termsDay 9 combining like terms
Day 9 combining like termsErik Tjersland
 
Web design annual plan annual plan
Web design annual plan annual planWeb design annual plan annual plan
Web design annual plan annual plancal1968
 
From java to rails
From java to railsFrom java to rails
From java to railsjokry
 
iOS Architecture and MVC
iOS Architecture and MVCiOS Architecture and MVC
iOS Architecture and MVCMarian Ignev
 
Geometry Section 3-2 1112
Geometry Section 3-2 1112Geometry Section 3-2 1112
Geometry Section 3-2 1112Jimbo Lamb
 
Implementing Metadata Standards for a Digital Audiovisual Preservation Reposi...
Implementing Metadata Standards for a Digital Audiovisual Preservation Reposi...Implementing Metadata Standards for a Digital Audiovisual Preservation Reposi...
Implementing Metadata Standards for a Digital Audiovisual Preservation Reposi...Kara Van Malssen
 
RIPE Atlas
RIPE AtlasRIPE Atlas
RIPE AtlasRIPE NCC
 
Webinar: Top 5 Mistakes Your Don't Want to Make When Moving to the Cloud
Webinar: Top 5 Mistakes Your Don't Want to Make When Moving to the CloudWebinar: Top 5 Mistakes Your Don't Want to Make When Moving to the Cloud
Webinar: Top 5 Mistakes Your Don't Want to Make When Moving to the CloudInternap
 
IPv6 Adoption in the RIPE NCC Service Region
IPv6 Adoption in the RIPE NCC Service RegionIPv6 Adoption in the RIPE NCC Service Region
IPv6 Adoption in the RIPE NCC Service RegionRIPE NCC
 
Oracle Crystal Ball Screens
Oracle Crystal Ball ScreensOracle Crystal Ball Screens
Oracle Crystal Ball ScreensDave Maskell
 
interpolation
interpolationinterpolation
interpolation8laddu8
 
The Application Development Landscape - 2011
The Application Development Landscape -  2011The Application Development Landscape -  2011
The Application Development Landscape - 2011David Skok
 
Analyzing Statistical Results
Analyzing Statistical ResultsAnalyzing Statistical Results
Analyzing Statistical Resultsoehokie82
 
Magazine advert research
Magazine advert researchMagazine advert research
Magazine advert researchElliot Fursdon
 
15 dec 2011 jlhs sec 2
15 dec 2011 jlhs sec 215 dec 2011 jlhs sec 2
15 dec 2011 jlhs sec 2outdoorjohn
 
Restinpeaceosidays2011 111121093818-phpapp02
Restinpeaceosidays2011 111121093818-phpapp02Restinpeaceosidays2011 111121093818-phpapp02
Restinpeaceosidays2011 111121093818-phpapp02OpenSourceIndia
 
Motion graphs summary
Motion graphs summaryMotion graphs summary
Motion graphs summaryPatrick Cole
 

Similar to Introduction to Clojure's STM (20)

AlphaBox Technology Overview
AlphaBox Technology OverviewAlphaBox Technology Overview
AlphaBox Technology Overview
 
Day 9 combining like terms
Day 9 combining like termsDay 9 combining like terms
Day 9 combining like terms
 
Web design annual plan annual plan
Web design annual plan annual planWeb design annual plan annual plan
Web design annual plan annual plan
 
From java to rails
From java to railsFrom java to rails
From java to rails
 
FYP Presentation
FYP PresentationFYP Presentation
FYP Presentation
 
iOS Architecture and MVC
iOS Architecture and MVCiOS Architecture and MVC
iOS Architecture and MVC
 
Compost Modern, 2009
Compost Modern, 2009Compost Modern, 2009
Compost Modern, 2009
 
Geometry Section 3-2 1112
Geometry Section 3-2 1112Geometry Section 3-2 1112
Geometry Section 3-2 1112
 
Implementing Metadata Standards for a Digital Audiovisual Preservation Reposi...
Implementing Metadata Standards for a Digital Audiovisual Preservation Reposi...Implementing Metadata Standards for a Digital Audiovisual Preservation Reposi...
Implementing Metadata Standards for a Digital Audiovisual Preservation Reposi...
 
RIPE Atlas
RIPE AtlasRIPE Atlas
RIPE Atlas
 
Webinar: Top 5 Mistakes Your Don't Want to Make When Moving to the Cloud
Webinar: Top 5 Mistakes Your Don't Want to Make When Moving to the CloudWebinar: Top 5 Mistakes Your Don't Want to Make When Moving to the Cloud
Webinar: Top 5 Mistakes Your Don't Want to Make When Moving to the Cloud
 
IPv6 Adoption in the RIPE NCC Service Region
IPv6 Adoption in the RIPE NCC Service RegionIPv6 Adoption in the RIPE NCC Service Region
IPv6 Adoption in the RIPE NCC Service Region
 
Oracle Crystal Ball Screens
Oracle Crystal Ball ScreensOracle Crystal Ball Screens
Oracle Crystal Ball Screens
 
interpolation
interpolationinterpolation
interpolation
 
The Application Development Landscape - 2011
The Application Development Landscape -  2011The Application Development Landscape -  2011
The Application Development Landscape - 2011
 
Analyzing Statistical Results
Analyzing Statistical ResultsAnalyzing Statistical Results
Analyzing Statistical Results
 
Magazine advert research
Magazine advert researchMagazine advert research
Magazine advert research
 
15 dec 2011 jlhs sec 2
15 dec 2011 jlhs sec 215 dec 2011 jlhs sec 2
15 dec 2011 jlhs sec 2
 
Restinpeaceosidays2011 111121093818-phpapp02
Restinpeaceosidays2011 111121093818-phpapp02Restinpeaceosidays2011 111121093818-phpapp02
Restinpeaceosidays2011 111121093818-phpapp02
 
Motion graphs summary
Motion graphs summaryMotion graphs summary
Motion graphs summary
 

Recently uploaded

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsAndrey Dotsenko
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
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
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
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
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
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
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 

Recently uploaded (20)

Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).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?
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
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
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.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
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 

Introduction to Clojure's STM

  • 1. Clojure’s Software Transactional Memory @fronx at @cljugb 12/2011 Thursday, December 15, 11
  • 2. what’s it good for? Thursday, December 15, 11
  • 3. it’s pretty much useless unless you have: concurrency shared state changes to state Thursday, December 15, 11
  • 4. the pitch “It allows you to do coordinated change without the complexity of locking.” — Rich Hickey http://www.infoq.com/interviews/hickey-clojure Thursday, December 15, 11
  • 5. kind of like database transactions. but in memory. Thursday, December 15, 11
  • 6. example shared state 1 9 5 7 3 10 4 2 6 8 changing state 4 4 threads a b c d e solution: do it atomically! Thursday, December 15, 11
  • 7. user=> (def my-atom (atom 0)) #'user/my-atom atoms (No STM here.) user=> @my-atom 0 user=> (swap! my-atom inc) 1 user=> @my-atom 1 user=> (swap! my-atom (fn [n] (* (+ n n) 2))) 4 Thursday, December 15, 11
  • 8. broken example (using atoms) Thursday, December 15, 11
  • 9. changing state atomic (acts as one point in time) consistent (from valid state to valid state) isolated (changes are local until committed) Thursday, December 15, 11
  • 10. sharing state mutate in STM only! coordinated independent synchronous ref atom asynchronous – agent receive message and return Thursday, December 15, 11
  • 11. STM functions ref reference to a collection dosync transaction set-ref set new value alter set new value via a function commute set new value and don’t block ensure block writes by others io! IllegalStateException Thursday, December 15, 11
  • 12. working and slightly different example (using STM) Thursday, December 15, 11
  • 13. STM functions ref reference to a collection dosync transaction set-ref set new value alter set new value via a function commute set new value and don’t block ensure block writes by others io! IllegalStateException Thursday, December 15, 11
  • 14. what to think about is it okay if others make changes in parallel? commute multi-ref constraints? ensure Thursday, December 15, 11
  • 15. how does it work? MVCC, snapshot isolation persistent data structures on-commit evaluation http://java.ociweb.com/mark/stm/article.html Thursday, December 15, 11