SlideShare a Scribd company logo
1 of 19
@jamie_allen
Mutability Matrix of Pain 2
Agenda
• The Java Memory Model
• What do concurrent and parallel mean?
• How does the heap work?
• How do we protect shared state on the heap?
Mutability Matrix of Pain 3
Scaling in the Multicore Era
• We have more more cores than ever (Intel Xeon E5-2699-v3 has 18
cores!)
• Shared mutable state, and the contention between threads of
execution vying to update them simultaneously, is a performance
killer
• Blocking
• Locking
Mutability Matrix of Pain 4
The JVM is awesome!
• The power to leverage all cores on a box through a single, unified
process
• The first formal memory model, with well-defined semantics for
visibility
Mutability Matrix of Pain 5
Diagram by Juan Pablo Francisconi
http://www.researchbeta.com/?p=334
The Java Memory Model
• Two primary concepts:
• Happens-Before – if A happens before B, the A's result should be
visible to B
• Synchronize-With – action results in write-through to RAM before next
action
Mutability Matrix of Pain 6
The Java Memory Model
• There are no visibility guarantees between threads in a JVM without
locks or memory barriers (volatile)
• Something may end up being visible to another thread by pure
happenstance
• Contended locks are arbitrated at the kernel level, leading to
expensive context switches
Mutability Matrix of Pain 7
Concurrent and Parallel
Mutability Matrix of Pain 8
Name Versus Value
Mutability Matrix of Pain 9
• The name is the handle you (or the compiler) bind to a reference
• The value is the actual state stored in the heap
• Therefore, the name is a reference to a location on the heap
Name Value
val me = new Person("Jamie", "Allen");
Name Versus Value
Mutability Matrix of Pain 10
• For instance variables, a heap location for the class instance holds
the reference via the name field
• The value itself is located elsewhere (possibly contiguous, no
guarantee)class Customer {
val me = new Person("Jamie", "Allen")
...
}
Person("Jamie", "Allen")
me
Collections
Mutability Matrix of Pain 11
• Collections are an aggregation of references to values
• Java collections are all mutable by default, developers have to use
Guava to get immutable collections
val me = new Person("Jamie", "Allen")
val other = new Person("Yeon", "Kim")
val us = List(me, other)
Person("Jamie", "Allen")
Person("Yeon", "Kim")
us
me
other
Method-Scoped Name Versus Value
Mutability Matrix of Pain 12
• For variables defined in a method, the reference to the name is on
the stack frame
• The value itself is still located elsewhere (possibly contiguous, no
guarantee)
def myMethod() = {
val me = new Person("Jamie", "Allen")
...
}
Person("Jamie", "Allen")
me
Final
Mutability Matrix of Pain 13
• If we make a reference final in Java, it cannot be reassigned to a
new value
• Easy to forget to use the keyword
• A mutable value remains mutable despite the usage of the final
keyword
final Person me = new Person("Jamie", "Allen");
val me: Person = Person("Jamie", "Allen")
Mutability Matrix of Pain 14
The Mutability Matrix of Pain
val
var
ImmutableMutable
NAME
VALUE
@volatile
Implications of Immutability
Mutability Matrix of Pain 15
• Uses more Eden/NewGen heap space
• Resizing Eden/NewGen larger means longer stop the world pauses,
may cause Survivor spillover directly into Tenured/OldGen
• Measure the effects of attempts to tune GC at varying loads
• Only optimize where you must, over 99% of my customers don't
even try and are still wildly successful
Volatile is Your Friend
Mutability Matrix of Pain 16
• Use snapshots when mutability must be attained, and publish
updates via volatile with the last write
• Readers should first read the one volatile field of multiple writes
(reverse your order of access)
Caveat
• Just because you're using volatile doesn't mean that publishing
cannot happen before you expect
• For greater consistency guarantees, volatile may not be
appropriate
Mutability Matrix of Pain 17
Use volatile!
• Don't be mutable, but if you have
to, stay in the snapshot world with
volatile and avoid locks
• Understand the visibility
guarantees you need for your
application, and consider the
impact when you leave visibility to
chance
• You can be more specific with read
and write barriers if you go with
sun.misc.Unsafe
Mutability Matrix of Pain 18
©Typesafe 2015 – All Rights Reserved

More Related Content

More from shinolajla

20180416 reactive is_a_product_rs
20180416 reactive is_a_product_rs20180416 reactive is_a_product_rs
20180416 reactive is_a_product_rsshinolajla
 
20180416 reactive is_a_product
20180416 reactive is_a_product20180416 reactive is_a_product
20180416 reactive is_a_productshinolajla
 
20161027 scala io_keynote
20161027 scala io_keynote20161027 scala io_keynote
20161027 scala io_keynoteshinolajla
 
20160609 nike techtalks reactive applications tools of the trade
20160609 nike techtalks reactive applications   tools of the trade20160609 nike techtalks reactive applications   tools of the trade
20160609 nike techtalks reactive applications tools of the tradeshinolajla
 
20160524 ibm fast data meetup
20160524 ibm fast data meetup20160524 ibm fast data meetup
20160524 ibm fast data meetupshinolajla
 
20160520 The Future of Services
20160520 The Future of Services20160520 The Future of Services
20160520 The Future of Servicesshinolajla
 
20160520 what youneedtoknowaboutlambdas
20160520 what youneedtoknowaboutlambdas20160520 what youneedtoknowaboutlambdas
20160520 what youneedtoknowaboutlambdasshinolajla
 
20160317 lagom sf scala
20160317 lagom sf scala20160317 lagom sf scala
20160317 lagom sf scalashinolajla
 
Effective Akka v2
Effective Akka v2Effective Akka v2
Effective Akka v2shinolajla
 
Reactive applications tools of the trade huff po
Reactive applications   tools of the trade huff poReactive applications   tools of the trade huff po
Reactive applications tools of the trade huff poshinolajla
 
20140228 fp and_performance
20140228 fp and_performance20140228 fp and_performance
20140228 fp and_performanceshinolajla
 
Effective akka scalaio
Effective akka scalaioEffective akka scalaio
Effective akka scalaioshinolajla
 
Real world akka recepies v3
Real world akka recepies v3Real world akka recepies v3
Real world akka recepies v3shinolajla
 
Effective actors japanesesub
Effective actors japanesesubEffective actors japanesesub
Effective actors japanesesubshinolajla
 
Effective Actors
Effective ActorsEffective Actors
Effective Actorsshinolajla
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scalashinolajla
 

More from shinolajla (18)

20180416 reactive is_a_product_rs
20180416 reactive is_a_product_rs20180416 reactive is_a_product_rs
20180416 reactive is_a_product_rs
 
20180416 reactive is_a_product
20180416 reactive is_a_product20180416 reactive is_a_product
20180416 reactive is_a_product
 
20161027 scala io_keynote
20161027 scala io_keynote20161027 scala io_keynote
20161027 scala io_keynote
 
20160609 nike techtalks reactive applications tools of the trade
20160609 nike techtalks reactive applications   tools of the trade20160609 nike techtalks reactive applications   tools of the trade
20160609 nike techtalks reactive applications tools of the trade
 
20160524 ibm fast data meetup
20160524 ibm fast data meetup20160524 ibm fast data meetup
20160524 ibm fast data meetup
 
20160520 The Future of Services
20160520 The Future of Services20160520 The Future of Services
20160520 The Future of Services
 
20160520 what youneedtoknowaboutlambdas
20160520 what youneedtoknowaboutlambdas20160520 what youneedtoknowaboutlambdas
20160520 what youneedtoknowaboutlambdas
 
20160317 lagom sf scala
20160317 lagom sf scala20160317 lagom sf scala
20160317 lagom sf scala
 
Effective Akka v2
Effective Akka v2Effective Akka v2
Effective Akka v2
 
Reactive applications tools of the trade huff po
Reactive applications   tools of the trade huff poReactive applications   tools of the trade huff po
Reactive applications tools of the trade huff po
 
20140228 fp and_performance
20140228 fp and_performance20140228 fp and_performance
20140228 fp and_performance
 
Effective akka scalaio
Effective akka scalaioEffective akka scalaio
Effective akka scalaio
 
Cpu Caches
Cpu CachesCpu Caches
Cpu Caches
 
Real world akka recepies v3
Real world akka recepies v3Real world akka recepies v3
Real world akka recepies v3
 
Effective actors japanesesub
Effective actors japanesesubEffective actors japanesesub
Effective actors japanesesub
 
Effective Actors
Effective ActorsEffective Actors
Effective Actors
 
Taxonomy of Scala
Taxonomy of ScalaTaxonomy of Scala
Taxonomy of Scala
 
CPU Caches
CPU CachesCPU Caches
CPU Caches
 

Recently uploaded

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
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
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
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
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
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
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
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
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 

Recently uploaded (20)

How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
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
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
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)
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
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
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
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...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

20150411 mutability matrix of pain scala

  • 1.
  • 3. Agenda • The Java Memory Model • What do concurrent and parallel mean? • How does the heap work? • How do we protect shared state on the heap? Mutability Matrix of Pain 3
  • 4. Scaling in the Multicore Era • We have more more cores than ever (Intel Xeon E5-2699-v3 has 18 cores!) • Shared mutable state, and the contention between threads of execution vying to update them simultaneously, is a performance killer • Blocking • Locking Mutability Matrix of Pain 4
  • 5. The JVM is awesome! • The power to leverage all cores on a box through a single, unified process • The first formal memory model, with well-defined semantics for visibility Mutability Matrix of Pain 5 Diagram by Juan Pablo Francisconi http://www.researchbeta.com/?p=334
  • 6. The Java Memory Model • Two primary concepts: • Happens-Before – if A happens before B, the A's result should be visible to B • Synchronize-With – action results in write-through to RAM before next action Mutability Matrix of Pain 6
  • 7. The Java Memory Model • There are no visibility guarantees between threads in a JVM without locks or memory barriers (volatile) • Something may end up being visible to another thread by pure happenstance • Contended locks are arbitrated at the kernel level, leading to expensive context switches Mutability Matrix of Pain 7
  • 9. Name Versus Value Mutability Matrix of Pain 9 • The name is the handle you (or the compiler) bind to a reference • The value is the actual state stored in the heap • Therefore, the name is a reference to a location on the heap Name Value val me = new Person("Jamie", "Allen");
  • 10. Name Versus Value Mutability Matrix of Pain 10 • For instance variables, a heap location for the class instance holds the reference via the name field • The value itself is located elsewhere (possibly contiguous, no guarantee)class Customer { val me = new Person("Jamie", "Allen") ... } Person("Jamie", "Allen") me
  • 11. Collections Mutability Matrix of Pain 11 • Collections are an aggregation of references to values • Java collections are all mutable by default, developers have to use Guava to get immutable collections val me = new Person("Jamie", "Allen") val other = new Person("Yeon", "Kim") val us = List(me, other) Person("Jamie", "Allen") Person("Yeon", "Kim") us me other
  • 12. Method-Scoped Name Versus Value Mutability Matrix of Pain 12 • For variables defined in a method, the reference to the name is on the stack frame • The value itself is still located elsewhere (possibly contiguous, no guarantee) def myMethod() = { val me = new Person("Jamie", "Allen") ... } Person("Jamie", "Allen") me
  • 13. Final Mutability Matrix of Pain 13 • If we make a reference final in Java, it cannot be reassigned to a new value • Easy to forget to use the keyword • A mutable value remains mutable despite the usage of the final keyword final Person me = new Person("Jamie", "Allen"); val me: Person = Person("Jamie", "Allen")
  • 14. Mutability Matrix of Pain 14 The Mutability Matrix of Pain val var ImmutableMutable NAME VALUE @volatile
  • 15. Implications of Immutability Mutability Matrix of Pain 15 • Uses more Eden/NewGen heap space • Resizing Eden/NewGen larger means longer stop the world pauses, may cause Survivor spillover directly into Tenured/OldGen • Measure the effects of attempts to tune GC at varying loads • Only optimize where you must, over 99% of my customers don't even try and are still wildly successful
  • 16. Volatile is Your Friend Mutability Matrix of Pain 16 • Use snapshots when mutability must be attained, and publish updates via volatile with the last write • Readers should first read the one volatile field of multiple writes (reverse your order of access)
  • 17. Caveat • Just because you're using volatile doesn't mean that publishing cannot happen before you expect • For greater consistency guarantees, volatile may not be appropriate Mutability Matrix of Pain 17
  • 18. Use volatile! • Don't be mutable, but if you have to, stay in the snapshot world with volatile and avoid locks • Understand the visibility guarantees you need for your application, and consider the impact when you leave visibility to chance • You can be more specific with read and write barriers if you go with sun.misc.Unsafe Mutability Matrix of Pain 18
  • 19. ©Typesafe 2015 – All Rights Reserved