8 Advanced concurrency control

 

No more deadlocks!

This chapter covers

  • Atoms as an alternative to locks
  • Managing a thread-safe counter and a thread-safe in-memory cache with atoms
  • Managing the whole system state in a thread-safe way with atoms

The traditional way to manage concurrency in a multi-threaded environment involves lock mechanisms like mutexes. Lock mechanisms tend to increase the complexity of the system because it’s not trivial to make sure the system is free of deadlocks. In DOP, we leverage the fact that data is immutable, and we use a lock-free mechanism, called an atom, to manage concurrency. Atoms are simpler to manage than locks because they are lock-free. As a consequence, the usual complexity of locks that are required to avoid deadlocks don’t apply to atoms.

►Note

This chapter is mostly relevant to multi-threaded environments like Java, C#, Python, and Ruby. It is less relevant to single-threaded environments like JavaScript. The JavaScript code snippets in this chapter are written as though JavaScript were multi-threaded.

8.1 The complexity of locks

This Sunday afternoon, while riding his bike across the Golden Gate Bridge, Theo thinks about the Klafim project with concern, not yet sure that betting on DOP was a good choice. Suddenly, Theo realizes that he hasn’t yet scheduled the next session with Joe. He gets off his bike to call Joe. Bad luck, the line is busy.

8.2 Thread-safe counter with atoms

8.3 Thread-safe cache with atoms

8.4 State management with atoms

Summary