WorkCase study
Coursework, group of seven · In development · 2026
Distributed Smart Parking
A distributed parking system for my distributed systems class, built on plain Python sockets. Group of seven.

- Role
- The protocols, servers, crash recovery, the overlay, leader election and locking, the tests and the browser demo.
- Team
- Group of seven, CSULB CECS 327
- Stack
- Python standard library, TCP sockets, Vanilla JS, Web Workers
- Status
- In development, 2026
Problem
This is the term project for CECS 327, distributed systems, at Cal State Long Beach. It's a set of parking lots. Sensors report which spaces are taken, each lot server keeps the state for one lot, and a driver's client finds and reserves a space.
Each of those is a separate process that only talks to the others through messages, and any of them can fail. The interesting part is what the system should do right after something breaks:
- A reply gets lost. Did the reservation go through?
- A lot server gets killed. What does it know when it comes back?
- A sensor goes quiet. Is the space free, or is the sensor dead?
- The process handing out a shared lock dies. Who grants the next one?
We could only use the Python standard library. No message queue, no RPC library, no framework.
What I built
It's a group project with seven people, and all of our names are on the demo's overview page. These are the parts I wrote. 55 of the repo's 57 commits are from my account.
- The wire format. JSON messages over TCP with a four byte length prefix. Each message has a type, an id, the sender, a timestamp, a reply reference and a Lamport clock value.
- Three kinds of communication. Request and reply with retries, publish and subscribe through a broker, and group messaging through a registry.
- Crash recovery. Each lot server writes every message to a log and replays it when it restarts.
- A peer to peer overlay. Lot summaries spread by gossip, lookups flood with a hop limit, and a ping round cleans up dead links.
- Coordination. Lamport clocks, Bully leader election, and a lock on a shared overflow garage with the leader as coordinator.
- The tests and the browser demo.
How it works
Framing. TCP is a byte stream, so one read can return half a message or two stuck together. Every message is sent as a four byte length followed by a JSON body, and the receiver keeps reading until it has that many bytes. A length over 1 MiB or a body that isn't JSON drops that connection and logs it, without crashing the process.
Retries. A client that hears nothing can't tell whether the request or the reply was lost. So it retries with the same message id, and the server keeps the reply it sent for each id. A repeat gets the stored reply back, marked as a duplicate, and the handler doesn't run again.
One writer per lot. Only a lot's own server changes that lot's state. Other lots keep a copy that's marked stale after twelve seconds without an update. If a sensor has been silent for six seconds, its space shows as unknown instead of free.
Recovery from the log. A lot server keeps its table in memory and its history on disk. After a crash it replays its own log. A thirty second reservation is rebuilt from the timestamp in the log entry, so it survives the crash and still expires on time.
The leader runs the lock. The lots elect a leader with the Bully algorithm, and the leader grants the lock on the shared garage. When the leader dies, the new one rebuilds the lock state from what the lots report before it grants anything.
- Gossip
- Lot summaries spread between neighbors with a forwarding probability of 0.7.
- Repair
- Every five seconds a peer pings its neighbors and drops any that don't answer.
- Replicas
- A copy of another lot's summary is marked stale after twelve seconds.
- Leases
- A reservation holds a space for thirty seconds. Reservation requests are idempotent.
The retry path
Every incoming request goes through this function on the server. The highlighted part is the reply cache:
prototype/comm/request_reply.pydef _reply_for(self, request): with self.lock: cached = self.reply_cache.get(request["msg_id"]) if cached is not None: reply = dict(cached) reply["payload"] = dict(cached["payload"], duplicate=True) return envelope.stamp(reply) op = request["payload"].get("op") args = request["payload"].get("args", {}) handler = self.ops.get(op) ... reply = envelope.make("REPLY", self.name, payload, reply_to=request["msg_id"]) with self.lock: self.reply_cache[request["msg_id"]] = reply return reply
And here's what it looks like in the server log. The test drops the first reply on purpose. The server sees the same message id twice and answers twice, but only books the space once:
| lamport | dir | type | msg_id | payload |
|---|---|---|---|---|
| 126 | recv | REQUEST | ca77c033 | reserve A-01 for carol |
| 127 | send | REPLY | 092e12e0 | reply_to ca77c033, granted, lease 30.0 |
| 128 | recv | REQUEST | ca77c033 | reserve A-01 for carol (same id, second attempt) |
| 129 | send | REPLY | 092e12e0 | reply_to ca77c033, granted, duplicate: true |
lot_server_A.log, trimmed to the relevant fields.

Testing
There are 35 unit tests for the framing, communication, clocks, overlay, election and lock. The more useful ones are the ten end to end scenarios. Each one starts real processes, runs the client against them, kills things with SIGKILL, and checks the replies and the log files.
| No. | Scenario | What it checks |
|---|---|---|
| 2 | Lost reply | A retry with the same id gets the stored reply, and there's only one reservation. |
| 4 | Lot server crash | After a restart the log is replayed and the reservation is still there. |
| 5 | Peer crash in the overlay | The dead link gets removed and the peer's summary goes stale. |
| 6 | Broker crash and restart | Everyone reconnects and resubscribes. Request and reply keeps working in the meantime. |
| 9 | Leader crash | The next highest lot takes over within a second, and the old leader takes it back when it restarts. |
| 10 | Coordinator killed mid race | Without the lock, six requests double book. With it none do, even when the coordinator dies part way through. |
One of them is flaky. Scenario 8 failed once in five runs for me. A lot that joined the overlay with two neighbors lost both of them, one left and the other crashed, and nothing reattached it. Which neighbors a lot picks is random, so the test passes whenever the lot happens to still have a live link. That's a real bug in the overlay.
Limits
- A peer that loses all its neighbors doesn't rejoin the overlay (the bug above).
- The lock on the shared garage is centralized. Between a coordinator dying and the next election finishing, nobody can get it.
- It's coursework. It runs on one machine over loopback and has never handled real traffic.
- The repo doesn't have CI, so the tests are run by hand.
What's next
Fix the overlay bug: when a peer's neighbor list is empty, it should rejoin through the registry the same way it joined. Then seed the neighbor choice in scenario 8 so that case gets tested every run. And add CI.