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

Day 9 combining like terms
Day 9 combining like termsDay 9 combining like terms
Day 9 combining like terms
Erik Tjersland
 
Web design annual plan annual plan
Web design annual plan annual planWeb design annual plan annual plan
Web design annual plan annual plan
cal1968
 
From java to rails
From java to railsFrom java to rails
From java to rails
jokry
 
Geometry Section 3-2 1112
Geometry Section 3-2 1112Geometry Section 3-2 1112
Geometry Section 3-2 1112
Jimbo Lamb
 
Pre preliminary task
Pre preliminary taskPre preliminary task
Pre preliminary task
ceriseoliviaa
 
Rdf explained by Suess and me
Rdf explained by Suess and meRdf explained by Suess and me
Rdf explained by Suess and me
3 Round Stones
 
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
RIPE NCC
 
interpolation
interpolationinterpolation
interpolation
8laddu8
 
Analyzing Statistical Results
Analyzing Statistical ResultsAnalyzing Statistical Results
Analyzing Statistical Results
oehokie82
 
Magazine advert research
Magazine advert researchMagazine advert research
Magazine advert research
Elliot Fursdon
 

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
 
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
 
Pre preliminary task
Pre preliminary taskPre preliminary task
Pre preliminary task
 
Do your test
Do your testDo your test
Do your test
 
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
 
Rdf explained by Suess and me
Rdf explained by Suess and meRdf explained by Suess and me
Rdf explained by Suess and me
 
ISA11 - Martin Verzilli: Interaction Design for Emergencies and Disasters
ISA11 - Martin Verzilli: Interaction Design for Emergencies and DisastersISA11 - Martin Verzilli: Interaction Design for Emergencies and Disasters
ISA11 - Martin Verzilli: Interaction Design for Emergencies and Disasters
 
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
 
Academic Library Collection Development: Current Landscape, Future Trends
Academic Library Collection Development: Current Landscape, Future TrendsAcademic Library Collection Development: Current Landscape, Future Trends
Academic Library Collection Development: Current Landscape, Future Trends
 
Analyzing Statistical Results
Analyzing Statistical ResultsAnalyzing Statistical Results
Analyzing Statistical Results
 
Magazine advert research
Magazine advert researchMagazine advert research
Magazine advert research
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+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...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Recently uploaded (20)

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
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
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
 
+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...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 

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