Rolando Andrade
Back to projects

Built in 2021

Rest Object Manager

Distributed Systems Project

Rest Object Manager preview

Rest object manager created as distribute systems project. It makes book's replications in different servers and restores the missing information from them. It uses REST between client and server, and sockets between server and replication servers.

Servers fail. Hard drives crash. Network connections drop. If you store data on one machine, you accept the risk of losing all of it. Distributed systems spread the data across many servers. Keeping those servers in agreement is harder than it sounds.

For a university distributed systems course, I built a book management system that copies its data to several servers. I wanted to see how real systems keep data consistent when things go wrong. One corrupted server should not mean lost data.

Books on many servers

The system manages a collection of books stored as XML files. Users work in a Vue.js frontend to create, read, update and delete books. Behind the screen, the data lives on several servers, not one.

When you save a book, the system copies it to every connected server. When you restore data, the system asks every server for its copy and picks the most common version. This majority vote handles the case where one server has corrupted or outdated data.

The architecture has three parts:

  1. The application server handles REST requests from the client.
  2. A coordinator runs the replication protocol.
  3. Several replicator servers store copies of the data.

Saving with two phase commit

Replication uses a two phase commit protocol. The application server sends a request to the coordinator. The coordinator then sends a vote request to every replicator over WebSockets, a connection that stays open in both directions.

Each replicator votes commit or abort, and the coordinator counts the votes. If every replicator votes commit, the coordinator sends a global commit. Otherwise it sends a global abort. Either every server gets the new data, or none of them does.

After the global commit, each replicator says it is ready. When all of them are ready, the coordinator sends the file data, encoded in base64. Each replicator writes it to disk.

To save a book, the coordinator collects every vote before any server writes the file.
To save a book, the coordinator collects every vote before any server writes the file.

Restoring with a majority vote

Restoring solves a different problem. What if the application server’s data is wrong? The coordinator asks every replicator to send back its copy. When all the answers arrive, it compares them.

If every copy matches, the system writes that version to the application server. If the copies differ, the system takes a vote. It picks the version that appears most often. That way, a few corrupted servers cannot win.

REST outside, WebSockets inside

The system uses different protocols for different jobs. The Vue.js client talks to the NestJS backend over REST. Standard HTTP verbs handle the create, read, update and delete operations on books.

Between the backend servers, WebSockets handle the live coordination. The coordinator keeps an open socket to every replicator. It can send vote requests and get answers without opening a new connection each time.

The split makes sense. Clients make a request now and then. Servers need constant, fast communication during replication and restoration.

Plain XML files on disk

Each server stores its data as an XML file on disk, not in a database. I chose that on purpose. XML files are easy to inspect, and easy to corrupt for testing. The whole state is one file, which also kept the replication logic simple.

The tradeoff is clear. File storage does not scale. For showing how distributed systems work, it was enough.

Where it got tricky

Counting votes across many servers needed careful state. The coordinator tracks how many votes are left, how many were commits and how many were aborts. If a replicator disconnects during a vote, the system has to handle it.

Testing was hard too. I needed several servers running at once to check the protocols. I corrupted XML files by hand on chosen servers to test the restore logic.

Race conditions were everywhere. What if a new replication request arrives while another is still running? The coordinator rejects the second request until the first one finishes. It is simple, and it works.

Why consensus is hard

This system showed me why distributed consensus is hard. Even a simple two phase commit has edge cases. What if the coordinator crashes after sending vote requests but before it collects every vote? Production systems need recovery for that, and this project did not have it.

The majority vote restore was my favorite part to build. I corrupted one server on purpose and watched the system bring back the right data. It felt like magic. But it only works while most servers hold the correct data.

I also learned to pick the protocol that fits the job. REST between the client and the server. WebSockets between servers.

Interested in working together? Check out my services and pricing to see how we can collaborate on your next project. Let's Build