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 as 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 atom to manage concurrency.
Atoms are simpler to manage that locks because they are lock-free. As a consequence, the usual complexity of locks that is require to avoid deadlocks don’t apply to them.
In this chapter, we cover:
- The implementation details of atoms
- How to use atoms to manage a thread-safe counter and a thread-safe in-memory cache
- How to manage the whole system state in a thread-safe way with atoms
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 like if JavaScript were multi-threaded.