dRuby explained: Ruby objects that talk across processes
An unofficial reference for dRuby (DRb), Ruby's distributed object library. Short, working examples for servers and clients, passing objects by reference, Rinda tuple spaces, and running it safely.
Guide
What dRuby is, how to install it, and how servers, clients and remote objects behave.
7 guide pages
- Installing dRuby: the drb gem and Ruby 3.4
drb was a default library and became a bundled gem in Ruby 3.4. When you need gem "drb" in your Gemfile and how to check it loads.
- A first dRuby server and client
Two short scripts: a server that publishes an object with DRb.start_service and a client that calls it through DRbObject.new_with_uri.
- Pass by value, pass by reference and DRbUndumped
dRuby copies objects it can marshal and sends a reference for the rest. How to tell which you get, and how DRb::DRbUndumped forces a reference.
- Callbacks and blocks: why the client needs DRb.start_service
A block or callback object passed to a dRuby server stays in the client and is called back over the network, so the client must run a service too.
- dRuby URIs and protocols: druby://, drbunix: and drbssl://
The three protocols that ship with dRuby, what each URI looks like, and which file to require for UNIX sockets and SSL.
- Threads, DRb.thread.join and keeping a server alive
A dRuby server runs in a background thread, so the script must wait on it with DRb.thread.join. Also: why front objects must be thread-safe.
Rinda
Tuple spaces for coordinating processes, and the ring server for finding services.
3 Rinda pages
- What are Rinda and tuple spaces?
Rinda is Ruby's implementation of the Linda tuple space model: a shared board where processes leave and collect tuples instead of calling each other.
- 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.
- RingServer and RingFinger: finding services with Rinda
Rinda's ring lets processes find a shared tuple space by UDP broadcast instead of a hard-coded URI, then register and look up services in it.
Safety
Why a dRuby port must never face an untrusted network, and how to restrict who can connect.
3 safety pages
- Why dRuby must not face untrusted networks
Anyone who can connect to a dRuby port can make the server unmarshal data and call methods, which can lead to running arbitrary code. What that means in practice.
- Access control lists with drb/acl
How to restrict which IP addresses can connect to a dRuby server using ACL.new and DRb.install_acl, with a working example and its limits.
- Binding to localhost, UNIX sockets and SSL
Three ways to narrow who can reach a dRuby server: bind to the loopback interface, use a UNIX domain socket with file permissions, or use drbssl with certificates.