Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Thread-Safety in Go - An Overlooked Concern (Uber Meetups, Sofia)

Thread-Safety in Go - An Overlooked Concern (Uber Meetups, Sofia)

A talk on learning the fundamentals of thread-safety in Go as presented at Uber Meetups, Sofia (https://www.meetup.com/Uber-Engineering-Events-Sofia/)

Materials - http://bit.ly/thread-safety-uber

Af78adc6caf494f74c1e4fb10a15c74a?s=128

Preslav Mihaylov

May 13, 2021
Tweet

Transcript

  1. Thread-Safety in Go An Overlooked Concern P. Mihaylov Software Engineer,

    Tech. Trainer, Speaker preslav@pmihaylov.com Uber Ungineering Sofia, 2021
  2. 👋 🚗 Software Engineer @ Uber 🎤 Tech. Trainer &

    Speaker ✍ Blogging at pmihaylov.com 󰞵 Open-sourcing at github.com/preslavmihaylov
  3. Why not a talk on concurrency?

  4. Most courses/books are incomplete…

  5. Not enough ❤ for thread-safety

  6. Concurrency without thread-safety -> 200 mp/h 🏎 without safety measures

  7. Goals → 1) Learn the fundamentals 2) Get guidance on

    next steps
  8. What’s covered 1. What does “thread-safety” mean? 2. Should you

    care about it at all? 3. Thread-safety problems & solutions a. When is there a potential thread-safety issue? b. Atomic access to shared state c. Delegating thread-safety d. Atomic compound actions e. Visibility & stale data 4. Where to go from here?
  9. What does “thread-safety” mean?

  10. Safe classes → behave correctly

  11. Thread-safe classes → behave correctly while bombarded by a gazillion

    goroutines
  12. Should you care about it at all?

  13. “I don’t need thread-safe classes as I’m typically writing web

    apps without spawning any threads…” the average web dev
  14. You’re already writing multi-threaded applications...

  15. ...and no. Channels can’t save you every time

  16. None
  17. Thread-safety problems & solutions

  18. When is there a potential thread-safety issue?

  19. None
  20. Is it thread-safe? Join at slido.com #thread-safety-uber

  21. Shared mutable state

  22. Non-shared state Stack #1 Thread #1 Non-shared state #1 Non-shared

    state #2 Shared memory aka heap Shared state Non-shared state Stack #2 Thread #2
  23. Atomic access to shared state

  24. None
  25. None
  26. What can go wrong?

  27. None
  28. Using Mutexes is Go’s standard way to add critical sections/locks

  29. Delegating thread-safety

  30. None
  31. None
  32. Delegated thread-safety → relying on the thread-safetiness of objects you

    use
  33. But also (third-party) packages...

  34. Atomic compound actions

  35. None
  36. None
  37. Actions from the same invariant need to be synchronized together

  38. Visibility & stale data

  39. None
  40. None
  41. Is it thread-safe? Join at slido.com #thread-safety-uber

  42. Thread-safety is not only about concurrent writes...

  43. None
  44. Remember 3+1 things

  45. 1

  46. Stateless methods → thread-safe methods

  47. None
  48. 2

  49. Thread-safety is about both reads and writes...

  50. None
  51. 3

  52. Prefer delegation over explicit synchronization

  53. None
  54. 4

  55. Be careful with compound actions...

  56. None
  57. None
  58. Is that all I need to know?

  59. What’s not covered 1. Initialisation safety & Immutable objects 2.

    Thread-confinement 3. Concurrency tooling - atomic variables/collections, synchronizers 4. Goroutine Management 5. Channels 6. Managing goroutine lifecycle via cancellation/interrupts 7. Dealing with performance/liveness issues (i.e. deadlock, livelock, etc.) 8. The Go Memory Model
  60. None
  61. Get the mats → http://bit.ly/thread-safety-uber

  62. Thread-Safety in Go An Overlooked Concern P. Mihaylov Software Engineer,

    Tech. Trainer, Speaker preslav@pmihaylov.com Uber Ungineering Sofia, 2021