explaingit

ruby-concurrency/concurrent-ruby

5,815RubyAudience · developerComplexity · 3/5Setup · easy

TLDR

A Ruby library that provides thread-safe data structures and background-task patterns, futures, promises, timers, so you can write concurrent code without data conflicts, with no external gem dependencies.

Mindmap

mindmap
  root((concurrent-ruby))
    Core Abstractions
      Promises and Futures
      Scheduled tasks
      Timer tasks
    Thread-Safe Data
      Concurrent Hash
      Concurrent Array
      ImmutableStruct
    Ruby Runtimes
      MRI CRuby
      JRuby
      TruffleRuby
    Design Goals
      No dependencies
      Natural Ruby API
      Multiple patterns
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Start a slow API call or database query in the background using a Promise and retrieve the result later without blocking the rest of your program.

USE CASE 2

Schedule recurring background jobs at fixed intervals using TimerTask without adding an external job queue or worker process.

USE CASE 3

Replace Ruby's standard Hash or Array with thread-safe versions to prevent data corruption in multi-threaded Rails or Sinatra apps.

USE CASE 4

Add async behavior to any Ruby class with the Async mixin so it handles concurrent work without requiring a full actor framework.

Tech stack

RubyJRubyTruffleRuby

Getting it running

Difficulty · easy Time to first run · 30min

In plain English

Concurrent Ruby is a library that gives Ruby developers a collection of tools for writing programs that do multiple things at the same time without running into conflicts. When several parts of a program try to read or change the same data at once, hard-to-reproduce bugs can appear. This library provides structures and patterns designed to prevent those kinds of problems, and it works consistently across the three main Ruby versions: MRI/CRuby, JRuby, and TruffleRuby. The library draws ideas from several other programming languages and communities. Erlang contributed the notion of isolated processes passing messages rather than sharing memory. Clojure, Scala, Haskell, and Java contributed patterns for futures, promises, and immutable data. The result is a Ruby gem that brings the safest and most practical of those ideas into the Ruby language without introducing any external gem dependencies. At the core are general-purpose building blocks for managing work that happens in the background. Promises let you start a computation and retrieve its result later without blocking the rest of your program. ScheduledTask runs a piece of work at a specific point in the future. TimerTask wakes up at regular intervals to perform background work. The Async mixin adds straightforward asynchronous behavior to any class with minimal setup. The library also provides thread-safe versions of Ruby's common data containers: arrays, hashes, sets, and a Map type designed to perform well under high concurrency. There are immutable value types too, such as ImmutableStruct, for situations where you want to guarantee that an object cannot be changed after it is created. The design philosophy is to offer a broad set of options without pushing a particular style. The library avoids debates about which concurrency pattern is superior, keeps its own codebase small, and aims to keep the API feeling like natural Ruby rather than a translation from another language.

Copy-paste prompts

Prompt 1
Using concurrent-ruby Promises, show me how to fire three parallel HTTP requests and wait for all results before continuing.
Prompt 2
I need a background polling job in my Ruby app that runs every 30 seconds. Show me how to set this up with concurrent-ruby's TimerTask.
Prompt 3
My Rails app shares a Hash between threads and occasionally loses data. How do I replace it with concurrent-ruby's Concurrent::Hash?
Prompt 4
Show me how to use the Async mixin from concurrent-ruby to make an existing Ruby class process method calls asynchronously.
Prompt 5
What is the difference between concurrent-ruby's Promise, ScheduledTask, and TimerTask, and when should I choose each one?
Open on GitHub → Explain another repo

← ruby-concurrency on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.