Calling App Design
A calling app passes a signal from one phone to another to create a connection, carries the data, and ends on a signal for termination.
A call goes over the internet with voice over IP, or over PSTN. PSTN blocks a portion of the bandwidth for the caller, so it is charged.
A voice over IP call:
- C sends the call state manager a request with the sender and receiver user IDs.
- The call state manager validates it and asks the session service for each session.
- Each session maps to a connection on the switch: 113 on box A, 114 on box B.
- The switch bridges the two connections.
The switch publishes every state change to a message queue, and the call state manager logs each with a timestamp. A call that started at timestamp 53 and terminated at 100 is charged to the caller as one minute. An invoice ID keeps the same call from being charged multiple times.
For PSTN, a recommendation engine chooses the spectrum provider by call quality and fee.
Concept #1: Breaking calls into dialogs
A dialog is the connection between one participant of a call and the server, and it is treated as an independent component.
On the internet, the signal hops through the routers of internet service providers to the server. A phone call goes through the PSTN, a network of phone service providers. If A is on Verizon and B is on AT&T, Verizon either drops the call or forwards it to AT&T, which sends it to B.
The provider that terminates the call charges for it. Verizon's selling price is fixed, and its cost price depends on where the call terminates.
Calls go through the application server, because only registered users make calls. The call is broken into dialog A and dialog B. A third user C adds dialog C, which gives conference calling.
A dialog table maps a dialog ID to a call ID, with the provider, the cost price, the timestamp and the direction. Verizon at $1 per minute and AT&T at $3 per minute make the call's cost price the sum of its dialogs.
Concept #2: The state machine
Every dialog is a state machine: a set of actions that move it between states, from initiated to terminated.
- Initiated. The caller sends a call to the server, or the server makes a call to the callee.
- Ringing. A validated callee goes here, and the phone rings.
- Started. A validated caller goes here immediately.
- Terminated. Any state reaches it: validation fails, the balance is not enough, or a user cuts the call.
Charging starts when both users have hit the started state and runs until terminated.
The entire call needs a state machine too. Keeping that logic on the switch is a bad idea. A restart of a proxy, gateway or endpoint breaks every connection, and all the users reconnect to the box, which is the thundering herd problem. The state machine logic changes with the requirements, and a code change means a restart.
So the boxes are kept as dumb as possible. The call state manager (CSM) stores the conditional logic and turns it into a simple request response.
Concept #3: Charging Users
Users are charged after the calls on a postpaid plan, and during the call on a prepaid plan.
A prepaid user pays 500 rupees for 500 minutes. A user with one rupee left is allowed one minute, and without a constant check speaks for three hours.
- The switch polls the billing service every minute, and it answers yes or no.
- Or the billing service returns a max talk time, such as three minutes, and the switch cuts the call at the limit.
- That logic moves to the call state manager, which stores the max talk time per call ID. It costs two API calls and removes the logic from the switch.
- The max talk time is set at the start of the call. If 10 people join a conference call from different countries, the user exceeds the budget.
- A request response architecture is simpler. Every minute the switch asks to continue, and the call state manager asks the billing service whether the user has the three rupees for the next minute.
Consistent Hashing for caching call state
Consistent hashing maps call IDs to caches so that adding or losing a cache moves few entries.
Three caches A, B and C hold the state of eight call IDs. The usual mapping is the hash of the call ID mod the number of servers. Adding a cache D changes the remainder to mod 4, and entries migrate from box to box. The churn is the number of entries a server has to get rid of.
On a hash ring:
- The hash of each server ID mod a large prime number M places A, B and C on a ring from zero to M minus one.
- The same hash places each call ID on the ring.
- Each call ID is stored on the next clockwise server.
When D arrives, only 8, 5, 7 and 6 move to it. When A crashes, 1 and 3 move to C.
The ring suffers from skew: D takes four call IDs and B takes one. Mapping every server onto multiple points with different hash functions makes skew unlikely.