Section650

Writing

TopicArchitecture
Reading11 min

What Open Source VoIP Taught Me About Distributed Systems Before We Called Them Distributed Systems

Partial failure, routing, retries and records that had to reconcile were everyday VoIP problems. Working with Asterisk, OpenSIPS, Python AGI and billing taught me the questions I still ask before choosing any architecture.

By Ka Lun Chan · VoIP · Architecture

Distributed systems, before I used the term

Distributed systems were a well-established field long before I worked on VoIP. The title describes my own path: I was dealing with the problems every day before the phrase was part of my working vocabulary.

As a co-founder, I built a communications platform on open-source VoIP. The pieces I worked with most were Asterisk, OpenSIPS, Python scripts running through the Asterisk Gateway Interface (AGI), a billing system, and routing informed by where callers were. Years later, when I started hearing microservices debates, a lot of them sounded familiar.

Many of the problems engineers now discuss around microservices were everyday concerns in VoIP: coordinating separate components, routing requests, handling partial failures, tracking state across steps and keeping business records accurate. The components themselves weren’t microservices, but the questions were the same.

A phone call crossed several system boundaries

OpenSIPS handles SIP signaling: the messages that set up, change and end a call. It decides where a call request should go and passes it along. The audio doesn’t flow through it. The media path, carried over RTP, is a separate stream that can take a different route, which is why a call can be set up perfectly and still have one-way audio.

Users behind home and office NAT routers added another piece. MediaProxy relayed their audio, so for many calls the media went through a relay we operated. That made the relay a capacity concern, because it carried the full audio of every relayed call, and another place a call could fail while signaling looked fine.

Asterisk handles the call itself. Its dialplan decides what happens step by step, and it can terminate and process media for features like prompts and recordings. When the dialplan needs a business decision, it can run an AGI script. With plain AGI, Asterisk starts the script as a local process and talks to it over standard input and output. That script is where Python could check an account, look up a rate or choose a route from backend data.

Billing sits after all of that. It turns usage into money: call records, rating, balances and reconciliation. Ours also had to work for local businesses that resold the service to their own communities on our hosted platform, so a single call could matter to a customer’s balance, a reseller’s account and a carrier’s invoice at once.

A hypothetical call, and where it goes wrong

The flow below is illustrative, and real deployments differ a lot. I didn’t take it from any system I ran.

Fig. 666-1Hypothetical flow: one prepaid call across four components Where the billing record is written
  • SIP phone to OpenSIPS
  • OpenSIPS to Asterisk
  • Asterisk to Python AGI script
  • Asterisk to Call detail record
  • Call detail record to Rating & balance

A prepaid customer places a call. OpenSIPS receives the SIP request and forwards it to an Asterisk server. The dialplan runs a Python AGI script, which checks the customer’s balance and picks a carrier route. The call connects and lasts twelve minutes. At hangup, Asterisk writes a call detail record, and a billing job rates the call and deducts it from the balance.

Now suppose the billing job deducts the charge, and the database connection drops before the job hears that the write succeeded. The job assumes it failed and tries again. Without protection, the customer pays twice for one call.

A second problem sits earlier in the flow. If the AGI script can’t reach the balance database, should the call go through? Allowing it risks usage nobody can bill. Blocking it turns away a paying customer because of an internal timeout. That’s a policy decision, and someone on the business side should make it before the outage, not during it.

Why “the system is up” was never the whole answer

In that example, every component could report itself healthy. OpenSIPS routed the call, Asterisk handled it and the customer had a clear conversation. The failure was in a record.

That’s what partial failure looks like day to day. One component can be fine while another times out. A call can connect while the billing update fails. A completed call and a correctly billed call are separate outcomes, and a platform has to get both right. A status page that only asked whether each server was up would have missed the problem customers cared about most.

Carrier selection set the margin

Choosing where a call goes involves several inputs that pull against each other: carrier availability, cost per minute, call quality, regulatory policy and the caller’s location.

In a business that sells minutes, what you pay carriers is most of what each call costs, so the routing table effectively sets the gross margin. Least-cost routing picks the cheapest carrier for each destination that can still deliver acceptable quality. The cheapest carrier on the rate sheet was not always the cheapest in practice. A route that drops calls or delivers poor audio costs you in repeat attempts, support tickets and customers who stop calling. We ranked routes on call statistics, including answer rates, call durations, completion, cost and media quality, which I describe in the data-to-decision loop behind those routes.

Carriers also fail, sometimes cleanly and sometimes by answering and then delivering bad audio. Routing has to account for that with a next choice for each destination, and the statistics are how you notice a route has degraded before the support queue tells you. In today’s terms that is fault tolerance: a fallback for every dependency you don’t control, and monitoring that measures the outcome customers care about, not just whether the dependency answered.

Location needs care. A telephone number’s prefix tells you where the number was issued, not where the caller is standing; mobile and VoIP numbers travel with their owners. IP geolocation is an estimate, and sometimes a badly wrong one. And physical distance doesn’t guarantee a better network path. A nearby data center can sit behind a congested link while a farther one performs better. Location was a useful signal for routing, weighed with the others, and routing needs a sensible fallback for when it’s wrong.

Distance was a boundary too

Our users were in North America, South America, Asia and Europe, and the platform started in one data center. Audio that crosses an ocean twice arrives late, and no amount of server capacity fixes latency. We used the caller’s source IP address to estimate their location and steered the media through a closer path, which improved call quality for distant users and cut what we paid to carry the audio. I covered the capacity and cost side of that in capacity planning from carrier networks to the cloud.

When we moved from the data center into cloud regions on three continents, we ran the same application in each region and routed users to the nearest one, rather than breaking the application apart. Distance was the problem, and splitting the code wouldn’t have fixed it. That is a distinction I still use when a team proposes microservices to solve a latency or scaling problem: first find out which boundary is actually causing it. The three-continent case study covers how the move happened one region at a time.

Retries needed context

“Just retry it” means very different things depending on what you retry.

  • Retrying a rate lookup is usually harmless. Reading the same data twice costs a little time.
  • Trying another carrier before the call is answered can rescue it. Starting a second call after the first one connected rings someone twice.
  • Replaying a billing event can charge a customer twice for one call.

Idempotency means an operation can run more than once with the same result as running it once. In billing, that usually means keying each charge to a unique identifier for the call, so a replayed event finds the charge already recorded and does nothing.

SIP gives every dialog a Call-ID, and Asterisk assigns its own unique IDs to channels. Using an identifier like that as the key turns a dangerous retry into a safe one.

State and records needed reconciliation

A call passes through several stages: setup, an active session, a finished call detail record, and finally a charge on an account. Each stage lives in a different place and can fail on its own.

A call detail record (CDR) records that a call happened and how long it lasted. A billing system also needs the rate that applied, the balance before and after, adjustments and refunds, and a way to match its records against what carriers bill you. Reconciliation is the regular job of comparing those sources and explaining every difference. It only works if the same call can be followed through every stage by a shared identifier.

Debugging meant following the whole path

When a customer reported a problem, the answer was rarely in one log. You matched the SIP messages from signaling to the channel in Asterisk, the AGI script’s log lines, and the rows in billing, and the identifiers had to line up for any of that to work.

Distributed tracing today does something similar with far better tools: one trace ID carried across service calls, with timing for each step. The two aren’t identical, but the habit transfers directly. I wrote about the same lesson from the network side in what different systems taught me about software architecture.

Every boundary came with work

Separating signaling, call handling, business logic and billing made each piece easier to reason about. It also added configuration to keep consistent, monitoring for each component, version compatibility between them, and failure handling at every handoff.

That trade is the same one teams face when they count services today. Each boundary should solve a real problem, and the team has to be able to carry it, which is the argument in your team size should influence your architecture.

Engineering decisions were business decisions

On that platform almost every technical choice showed up somewhere in the business. Carrier selection set the gross margin. Routing decided whether a call sounded clear or broken, which is the whole product for a communications company. Infrastructure was a fixed cost that arrived every month whether or not the minutes did, and the move into cloud regions was as much about our cost structure as about latency. Reliability decided retention, because a customer who can’t complete a call rarely files a bug report. They are more likely to try another service.

Complexity was a cost too. Every component we added needed someone who understood it, monitoring that worked, and a plan for when it failed. The architecture had to fit the business requirements and the people we had at the time.

I still evaluate modern architecture that way. Before a team splits a system into microservices, I want to know which boundary earns its operating cost. Before Kafka, I want the fan-out or write volume that justifies an event stream. Before an infrastructure investment, I want the measured demand behind it and an idea of what it will cost every month after it ships.

The lessons in today’s terms

The vocabulary changed more than the problems did. The table maps what the VoIP platform had to handle to the names modern engineers use, and to the question I ask now. The last column is general practice, not a description of how we implemented it.

Fig. 666-2 VoIP problems, their modern names, and the question I ask before choosing an architecture
Today’s termOn the VoIP platformThe question I ask now
Partial failureA call connects and the audio goes nowhereWhich outcome does the customer care about, and do we measure that or only whether each component is up?
Retries and idempotencyA replayed billing event could charge a call twiceWhich operations are safe to retry, and what is the idempotency key for the ones that aren’t?
Routing and latencyAudio crossing an ocean twice, and the cost of carrying itWhere are the users, and which path does each request really take?
Observability and tracingMatching SIP messages, Asterisk channels, AGI logs and billing rowsCan we follow one request across every component by a shared identifier?
Fault toleranceA carrier that stops answering or degradesWhat is the fallback for each dependency we don’t control, and how do we notice it has degraded?
Consistency and reconciliationCall records, balances and carrier invoices that must agreeWho owns each piece of state, and how do we prove the records are right after a failure?
Capacity planningSignaling, media and relays filling at different ratesWhich resource fills first, and what does the next step up cost?

The tools improved; the questions stayed

Containers, orchestration and observability tools make this work far easier than it was. They help a team deploy consistently, restart what crashes and see across components. I’ve written about that change in how DevOps changed during my career.

They don’t answer the design questions. Someone still has to decide who owns each piece of state, what happens when a step fails halfway, which operations are safe to retry, and how the system recovers and proves its records are right.

Short answers

What is partial failure in a distributed system?

Partial failure is when some components work while others fail or time out. In VoIP, a call can connect and sound fine while the billing update for it fails, so “the system is up” doesn’t mean every outcome the business needs happened.

What is idempotency in billing?

Idempotency means running an operation more than once has the same result as running it once. In billing, each charge is keyed to a unique identifier for the call, so a retried or replayed event finds the charge already recorded instead of charging the customer twice.

What is the difference between SIP signaling and media?

SIP signaling carries the messages that set up, change and end a call. The media, the audio itself, usually travels separately over RTP and can take a different path, which is why a call can be set up correctly and still have audio problems.

Boundaries and failure first, tools second

VoIP taught me to look at a system through what crosses its boundaries and what happens when one of them fails.

Talk through an architecture decision