Coinlocally
A live crypto exchange whose real-time path ran on a third-party socket library and locking code that three screens each wrote for themselves. I replaced it with one layer we owned, while the app kept trading. Crash-free sessions went from 85% to 98%.
- Role
- iOS Engineer (plus some Go and Node.js backend work)
- When
- Nov 2023 to Dec 2024
- Stack
- Swift · Objective-C · URLSessionWebSocketTask · Swift actors
- Result
- 85% → 98% crash-free sessions
Borrowed parts on the riskiest path
Coinlocally is a cryptocurrency exchange with a live iOS trading app. When I joined in November 2023, the real-time part of the app was built from borrowed pieces: a third-party WebSocket library inside a thin manager class, a price chart running as a JavaScript widget in a web view, and order-book state that the futures, spot and home screens each kept and locked on their own.
Crash-free sessions were stuck at 85%. I worked on the iOS team until December 2024, with the real-time layer as my main area, and I also built some backend features in Go and Node.js along the way.
The same bug, three times
It was not one bad line. Each trading screen had its own queues and locks for what was really the same data, and there was no shared rule about which queue was safe to write from. Fixing a race on the futures screen told you nothing about the spot screen.
On an exchange, a crash is something that happens to a person in the middle of a trade. And the exchange could not close while we fixed it.
Constraints
- Real money. Instability means someone loses their screen or their connection mid-trade.
- No rewrite and no feature freeze. Reliability work had to ship inside the normal release cadence.
- Old and new code together. Objective-C and Swift, UIKit and SwiftUI, all working together through the whole migration.
- A fast feed. The order book and prices can update many times a second on the exact screens people trade from.
One writer, however fast the feed
An in-house WebSocket client replaced the third-party wrapper and writes into one market-data store. The order book, the chart, old screens and new screens all read from that one place.
A price tick comes in over the exchange's socket. Our client decodes it and writes it into the store. At first the store was protected by a concurrent queue with barrier writes, so reads could run in parallel and never race a write. Later, once Swift actors were practical for us, I moved the store into an actor and the queue-and-barrier code went away.
The order book and chart were rebuilt as native views on top of the store. They render the same way whether the screen around them is still UIKit or already SwiftUI.
What we chose, and what we paid for it
Replace the WebSocket library instead of patching it
Connection state and decoding finally lived behind one boundary that we owned, with no vendor dependency on the most latency-sensitive part of the app.
Everything the library used to handle for us (reconnection storms, bad frames, backpressure) became our problem.
One store instead of three copies of the same fix
A concurrency bug in market data now has exactly one place it can live.
Every screen that owned its own state had to be moved onto the shared store, on a live app, with no maintenance window.
A write barrier first, an actor second
The barrier closed the worst races right away. The actor later replaced hand-written synchronization with a guarantee from the compiler.
Changing the concurrency model twice meant auditing every caller twice.
Objective-C to Swift, screen by screen
The exchange never stopped trading, and reliability improved while features kept shipping.
For months we lived with two idioms side by side. That is slower and messier than a rewrite looks on a slide, but it is what Martin Fowler's Refactoring actually asks for: small steps, behavior held constant.
Where the old pattern was hiding
Protection that only looked like protection
Some of the duplicated queues were set up inconsistently. A .barrier flag on a queue that is already serial does nothing, so a screen could look protected and not be. I had to audit each screen's synchronization one by one instead of assuming a fix in one place meant safety in another. I wrote about this in detail in Race conditions on a live trading app.
The first actor was on the wrong thing
Before I touched the order book, the codebase already had one actor. It protected a small buffer used for paginating open orders, nowhere near the screens that were crashing. Adopting a language feature is not the same as using it on your problem.
Background work
I also moved app data refresh onto BGTaskScheduler, so updates happened in the background on the system's schedule instead of all at once on launch.
The number that mattered
What a live exchange taught me
- Duplicated fixes are not fixes. Three screens protecting the "same" state means three chances to get it slightly wrong, and one of them did.
- Consolidate before you modernize. Moving three copies into one store mattered more than which primitive protected it.
- Reliability work does not get a maintenance window. On a live exchange it ships with the features or it does not ship.