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

Preslav Mihaylov

May 13, 2021
Tweet

More Decks by Preslav Mihaylov

Other Decks in Programming

Transcript

  1. Thread-Safety in Go
    An Overlooked Concern
    P. Mihaylov
    Software Engineer, Tech. Trainer, Speaker
    [email protected]
    Uber Ungineering Sofia, 2021

    View full-size slide

  2. 👋
    🚗 Software Engineer @ Uber
    🎤 Tech. Trainer & Speaker
    ✍ Blogging at pmihaylov.com
    󰞵 Open-sourcing at
    github.com/preslavmihaylov

    View full-size slide

  3. Why not a talk on concurrency?

    View full-size slide

  4. Most courses/books are incomplete…

    View full-size slide

  5. Not enough ❤ for thread-safety

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  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?

    View full-size slide

  9. What does “thread-safety” mean?

    View full-size slide

  10. Safe classes →
    behave correctly

    View full-size slide

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

    View full-size slide

  12. Should you care about it at all?

    View full-size slide

  13. “I don’t need
    thread-safe classes
    as I’m typically
    writing web apps
    without spawning
    any threads…”
    the average web dev

    View full-size slide

  14. You’re already writing
    multi-threaded applications...

    View full-size slide

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

    View full-size slide

  16. Thread-safety problems & solutions

    View full-size slide

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

    View full-size slide

  18. Is it thread-safe?
    Join at slido.com
    #thread-safety-uber

    View full-size slide

  19. Shared mutable state

    View full-size slide

  20. 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

    View full-size slide

  21. Atomic access to shared state

    View full-size slide

  22. What can go wrong?

    View full-size slide

  23. Using Mutexes is Go’s standard way
    to add critical sections/locks

    View full-size slide

  24. Delegating thread-safety

    View full-size slide

  25. Delegated thread-safety →
    relying on the thread-safetiness of
    objects you use

    View full-size slide

  26. But also (third-party) packages...

    View full-size slide

  27. Atomic compound actions

    View full-size slide

  28. Actions from the
    same invariant need to be
    synchronized together

    View full-size slide

  29. Visibility & stale data

    View full-size slide

  30. Is it thread-safe?
    Join at slido.com
    #thread-safety-uber

    View full-size slide

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

    View full-size slide

  32. Remember 3+1 things

    View full-size slide

  33. Stateless methods →
    thread-safe methods

    View full-size slide

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

    View full-size slide

  35. Prefer delegation over explicit
    synchronization

    View full-size slide

  36. Be careful with compound actions...

    View full-size slide

  37. Is that all I need to know?

    View full-size slide

  38. 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

    View full-size slide

  39. Get the mats →
    http://bit.ly/thread-safety-uber

    View full-size slide

  40. Thread-Safety in Go
    An Overlooked Concern
    P. Mihaylov
    Software Engineer, Tech. Trainer, Speaker
    [email protected]
    Uber Ungineering Sofia, 2021

    View full-size slide