How a text message actually finds your phone across the world.
When you send an SMS, your phone doesn't talk directly to your friend's phone. It talks to a Cell Tower, which talks to an SMSC (Short Message Service Center), which uses a global routing protocol (SS7 or SMPP) to figure out which carrier your friend belongs to, and finally which physical cell tower they are currently standing near.
SMS uses a Store and Forward architecture. If your friend's phone is off, the SMSC holds onto the message and retries later. It relies on the HLR (Home Location Register) to find where the user is.
# The logical routing flow
def route_sms(sender, recipient_number, text):
# 1. Store the message safely on disk
msg_id = smsc.store(sender, recipient_number, text)
# 2. Ask the global registry: "Who owns this number?"
carrier_id = hlr.lookup_network(recipient_number)
# 3. Ask that carrier: "Where is this user right now?"
current_tower = carrier_id.get_current_location(recipient_number)
if current_tower.is_online():
# 4. Forward the message to that specific cell tower
current_tower.deliver(msg_id, text)
smsc.mark_delivered(msg_id)
else:
# User's phone is off or in a tunnel.
# Leave it in the DB, a cron job will retry later!
smsc.queue_for_retry(msg_id)
SMS routing has high latency and zero guarantees of ordering. Because it uses Store and Forward, Message 2 might be delivered before Message 1 if different SMSCs route them differently.