# Rinda::TupleSpace: write, take and read

> How to share a Rinda tuple space over dRuby, match tuples with patterns, and use timeouts. A complete server, producer and worker.

Source: https://druby.com/rinda/tuplespace-write-take-read/  
Section: Rinda  
Updated: 2026-12-05

`Rinda::TupleSpace` has three core operations: `write` adds a tuple, `take` removes and returns a tuple that matches a pattern, and `read` returns a matching tuple without removing it. To use one space from several processes, publish it as a dRuby front object.

## The tuple space server

```ruby
# space.rb
require 'drb/drb'
require 'rinda/tuplespace'

DRb.start_service('druby://localhost:8787', Rinda::TupleSpace.new)
DRb.thread.join
```

## A producer

```ruby
# producer.rb
require 'drb/drb'
require 'rinda/rinda'

DRb.start_service
ts = Rinda::TupleSpaceProxy.new(DRbObject.new_with_uri('druby://localhost:8787'))

3.times { |n| ts.write([:job, n, "image-#{n}.png"]) }
```

## A worker

```ruby
# worker.rb
require 'drb/drb'
require 'rinda/rinda'

DRb.start_service
ts = Rinda::TupleSpaceProxy.new(DRbObject.new_with_uri('druby://localhost:8787'))

loop do
  _, id, file = ts.take([:job, nil, nil])
  puts "Working on job #{id}: #{file}"
  ts.write([:done, id])
end
```

Start `space.rb`, then one or more copies of `worker.rb`, then `producer.rb`. Each job is handled by exactly one worker, because `take` removes the tuple it returns.

Both clients call `DRb.start_service`. A waiting `take` involves the space calling back to the client, so the client must be able to receive calls, as described on [callbacks and blocks](/guide/callbacks-and-blocks/).

## How patterns match

A pattern is an array of the same length as the tuples you want. Each element is compared with the tuple's element using `===`, and `nil` matches anything.

| Pattern | Matches |
| --- | --- |
| `[:job, nil, nil]` | Any three-element tuple starting with `:job` |
| `[:job, 7, nil]` | Job number 7 only |
| `[:job, Integer, String]` | A job whose id is an Integer and whose third element is a String |
| `[:job, 1..10, nil]` | Jobs with an id from 1 to 10 |
| `[:log, /error/, nil]` | A `:log` tuple whose second element is a string containing "error" |

A tuple with a different number of elements never matches.

## The operations

- `write(tuple)` adds a tuple. `write(tuple, seconds)` adds one that expires and disappears after that many seconds.
- `take(pattern)` removes and returns a matching tuple, waiting if there is none.
- `read(pattern)` returns a copy of a matching tuple and leaves it in the space, also waiting if there is none.
- `read_all(pattern)` returns an array of every matching tuple right now and never waits.

## Timeouts

`take` and `read` accept a number of seconds as a second argument. If nothing matches in time they raise `Rinda::RequestExpiredError`. A timeout of `0` means "do not wait at all".

```ruby
begin
  job = ts.take([:job, nil, nil], 5)
rescue Rinda::RequestExpiredError
  puts 'No work in the last 5 seconds'
end
```

## Why TupleSpaceProxy

A remote `take` has a weak point: the space removes the tuple, and if the taking process dies before the reply reaches it, the tuple is lost. `Rinda::TupleSpaceProxy` wraps the remote space and performs `take` in a way that guards against that case. Use it whenever the space is in another process. It is defined in `rinda/rinda`, which is why the clients above require that file.

## Good habits

- Start every tuple with a symbol that names its kind, such as `:job` or `:done`.
- Keep tuples small and made of plain values. They are marshaled on every write and take.
- Remember the space is in memory only. Anything that must survive a restart needs to be stored elsewhere.
