how to know what is NOT thread-safe in ruby?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
There is no short official list of "everything that is not thread-safe in Ruby." The practical rule is more conservative: if an object is mutable and shared across threads, assume it is not safe unless the documentation explicitly says otherwise. Ruby's GVL in MRI reduces some kinds of parallel execution, but it does not turn shared mutable state into a safe design.
Start With the Risk Model
Thread-safety problems usually come from three ingredients:
- shared state
- mutation
- operations that take more than one step
If your code has all three, it deserves suspicion.
For example, this increment is not a single conceptual unit at the Ruby level:
Even if MRI serializes bytecode execution in many moments, the read-modify-write pattern is still logically vulnerable when threads interleave.
Mutable Core Objects Need Care
Arrays, hashes, and strings are common sources of trouble when several threads write to the same instance:
This kind of code may appear to work in small tests, which is exactly why it is dangerous. Thread-safety bugs often hide until timing changes under load.
The conservative approach is:
- do not share mutable objects unless necessary
- protect writes with a
Mutex - prefer immutable data or per-thread state when possible
Memoization Is a Frequent Trap
This pattern is convenient but not reliably thread-safe:
Two threads can observe @config as unset and both initialize it. If initialization has side effects or must happen exactly once, add synchronization:
This is a good example of why "works most of the time" is not the same as thread-safe.
Documentation Beats Assumption
To know whether something is safe, check in this order:
- library documentation
- implementation notes
- issue trackers or known concurrency caveats
- source code, if needed
If the docs do not promise thread-safety, do not invent that guarantee yourself.
This matters especially for gems. Some libraries are safe for concurrent reads but not concurrent writes. Others require one object per thread. "Thread-safe" is rarely universal.
Use Explicit Synchronization
Ruby gives you tools to make the contract obvious:
This is slower than unsynchronized mutation, but it is correct. Correctness is the first requirement.
For richer concurrency primitives, libraries such as concurrent-ruby can provide safer data structures and coordination tools.
Common Pitfalls
- Assuming MRI's GVL makes all object access thread-safe.
- Sharing mutable hashes, arrays, or instance variables across threads without a lock.
- Treating memoization with
||=as automatically safe in concurrent code. - Testing only under low contention and concluding the code is safe.
- Forgetting that third-party gems need explicit thread-safety guarantees too.
Summary
- In Ruby, shared mutable state should be assumed unsafe unless documented otherwise.
- MRI's GVL does not remove the need for synchronization.
- Compound operations such as incrementing counters or lazy initialization are common trouble spots.
- Use
Mutex, thread-local data, or thread-safe abstractions when state must be shared. - When in doubt, trust documentation and source code over intuition.
Related reading
- How to know when a recursive, asynchronous task finishes
- How to let the UI refresh during a long running UI operation
- How to limit concurrent message consuming based on a criteria
- How to limit the amount of concurrent async I/O operations?
- How to limit the amount of simultaneously running jobs of a certain type?
- How to limit the maximum number of parallel tasks in C
- How to limit the maximum number of parallel tasks in C
- How to load an image asynchronously?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.