---
title: "Libraries of the world"
year: "2020"
motto: "Distributed Library System"
description: "Distribute system of a library that queries books in many servers using RMI."
stack: ["java"]
website: "https://github.com/RolandoAndrade/libraries-of-the-world"
url: "https://rolandoandrade.me/projects/libraries-of-the-world/"
author: "Rolando Andrade"
---

# Libraries of the world

> Distributed Library System

Distribute system of a library that queries books in many servers using RMI.

Year: 2020. Stack: java. Website: https://github.com/RolandoAndrade/libraries-of-the-world

Repositories:

- [github](https://github.com/RolandoAndrade/libraries-of-the-world)
- [gitlab](https://gitlab.com/RolandoAndrade/libraries-of-the-world)

Library A asks for a book with "Pedir Libro". Library B asks with "Buscar Título". Both mean the same thing, but neither server understands the other. Every library system names its operations, builds its queries and formats its answers in its own way.

In 2020, I was studying distributed systems. Our professor gave us a challenge. Build a network where independent library servers can talk to each other, even though each one uses a different internal protocol. There was a catch. Each server had to keep its own words and still understand requests from any client in the network.

I did not want to force every library to speak the same language. Instead, I built a middle layer, called middleware, that translates between them.

## Search every library at once

Libraries of the World is a distributed system where several library servers share their book catalogs. A user at one library can search the books of every connected library in real time.

The system does three main jobs:

- Search by title, asking every server for one book
- Search by author, finding all books by one author across the network
- Translate between each library's own words and a common format

Each library server runs on its own, with its own book database stored in XML files. Clients connect through Java RMI and can query any server in the network.

## Servers, clients and a protocol

The system keeps three parts separate: servers, clients and a shared protocol layer.

### A common library language

Without a common language, the libraries cannot talk. I solved this with two layers of translation.

The first layer is a standard protocol inspired by Z39.50. Z39.50 is the real standard that libraries use to talk to each other. Commands like "Get Title" and "Get Author" make up this shared vocabulary.

The second layer does the translating. Each library has its own command set that maps its local words to the standard protocol and back. When Library A sends "Pedir Libro", the middleware turns it into "Get Title" before sending the request on.

![Each library keeps its own words. The middleware turns them into the shared Z39 command and back.](https://storage.rolandoandrade.me/blog/libraries-of-the-world-translation.webp)

### Inside a server

Each server offers a remote interface that accepts standard commands. When a request arrives, the server:

1. Receives the command in Z39 format
2. Translates it into the local library's words
3. Queries the XML database
4. Translates the answer back into Z39 format
5. Returns the result to the client

Requests are handled one at a time. When several clients query the same server at once, a mutex makes them wait their turn. A mutex is a lock that only one request can hold. It prevents race conditions, which happen when two requests read the shared book database at the same moment and get in each other's way.

### The client mirrors it

The client follows the same translation pattern. When a user searches for a book, the client:

1. Takes the search term
2. Translates the request into Z39 format
3. Connects to the remote server through RMI
4. Receives the answer in Z39 format
5. Translates it back into the local library's words

Because translation works in both directions, every library keeps its own words and still joins the network.

### Remote calls with RMI

Java RMI (Remote Method Invocation) handles the network. It lets a program call a method on another computer as if it were local. Each server registers itself in an RMI registry with a URL like `rmi://192.168.1.100:3000/books`. Clients find servers by their network address and call their methods.

A configuration file lists every known library with its name and network address. The client knows where to send queries, and no server location is written into the code.

## What would not scale

Every new library type needs a full command mapping. Libraries A, B and C each have their own translations. Adding Library D means writing another full mapping layer. Configuration files would be more flexible than code for this.

The mutex on each server is a bottleneck. Under heavy load, requests line up and wait. A production system would scale better with a connection pool or async processing.

Libraries must be listed ahead of time in a JSON file. New servers cannot join the network on their own. A real system would need service discovery or a central registry.

## Why standards exist

This project showed me how protocol translation works in practice. Z39.50 exists because libraries faced this exact problem decades ago. Building a simple version of it showed me why standards like that matter.

I also learned Java RMI by using it. Newer tools like gRPC have replaced RMI in most places. The basics of calling a method on a remote machine still apply to any distributed system.

Keeping the domain logic apart from the infrastructure made parts easy to swap. The same translation layer could work over HTTP, sockets or any other transport.

[Book a call with Rolando](https://rolandoandrade.me/pricing/)
