Live data from Hacker News

If You're Programming a Cell Phone Like a Server, You're Doing it Wrong

highscalability.com

51–60 of 77 posts

Re: If You're Programming a Cell Phone Like a Server, You're Doing it Wrong

#51

Earlier quoted context omitted.

Respecting known limitations and working within best practices is absolutely not premature optimization. If you know that using the mobile radio in a certain way is a source of excessive battery usage, it's silly to just disregard this information. It's not premature optimization if you know where the performance issues are from the outset, and these performance issues are so incredibly common that the Android team p…

It's premature optimization if you're caring about this in your prototype. It's not at all premature optimization to look at battery life before you ship.

There's no such thing as a prototype.

The design decisions you make early on will stay with you. How you treat "occasionally connected" users is likely to have some architectural impact. Keep their requirements in mind as you sketch out your application.

Re: If You're Programming a Cell Phone Like a Server, You're Doing it Wrong

#52

Earlier quoted context omitted.

It's premature optimization if you're caring about this in your prototype. It's not at all premature optimization to look at battery life before you ship.

There's no such thing as a prototype. The design decisions you make early on will stay with you. How you treat "occasionally connected" users is likely to have some architectural impact. Keep their requirements in mind as you sketch out your application.

I can understand what dllthomas is talking about. I'd never write a full-blown prototype, but whenever I'm writing a non-trivial app for myself it can be quite productive to write 10% of the app, throw that away, and start again. This strategy always gives me a better understanding of the problem, the app's architecture and so on, as well as a ton of new ideas.

Re: If You're Programming a Cell Phone Like a Server, You're Doing it Wrong

#53
> Every decision you make should be based on minimizing the number of times the radio powers up.

It doesn't have to be "every decision" if you architect the app to not do CRUD-over-mobile-data and do sync instead.

This not only improves battery life, it has a strong positive effect on perceived performance and interactivity.

Fortunately, there will soon be a book all about that (ISBN 1118183495).

Re: If You're Programming a Cell Phone Like a Server, You're Doing it Wrong

#54

Earlier quoted context omitted.

There's no such thing as a prototype. The design decisions you make early on will stay with you. How you treat "occasionally connected" users is likely to have some architectural impact. Keep their requirements in mind as you sketch out your application.

I can understand what dllthomas is talking about. I'd never write a full-blown prototype, but whenever I'm writing a non-trivial app for myself it can be quite productive to write 10% of the app, throw that away, and start again. This strategy always gives me a better understanding of the problem, the app's architecture and so on, as well as a ton of new ideas.

Right, that's pretty much what I was talking about. Not so much something "officially blessed as a prototype" but "does this make sense at all?" experimentation shy of an MVP. I've heard "write it, throw it away, rewrite it" attributed to Knuth, and while that's overstating it a little you definitely learn things in your first attempt.

Re: If You're Programming a Cell Phone Like a Server, You're Doing it Wrong

#55
post #50

Earlier quoted context omitted.

The difference between DCH (the full-on high power state) and PCH (the lowest power, waiting-for-paging state) is about 2-orders of magnitude. Last time I measured, it was about ~100mA vs ~1mA (at ~3.7V) used by the radio. So, it's not couple of minutes of battery life, but rather _many_ hours of standby life instead. People usually aren't very happy when a fully charged phone dies overnight. I've seen apps do some c…

Anecdata: I turn my phone's wifi off when not required because it has a significant effect on the battery life - particularly when I'm not in range of a wifi signal. I've had a full charge die on an 8-hour country drive because wifi was on. On the return trip, wifi was turned off and it behaved as expected. Go for a long walk in the park? All that time your phone's wifi is straining to find a wifi signal. It's not as…

Wifi scanning is also pretty terrible for power consumption as you found out.

If you are on Android, my guess is that your phone was set to be connected to Wifi during sleep. You can control it by going to Wifi networks section -> menu -> Advanced -> Keep Wi-Fi on during sleep, then setting it to "Only when plugged in" or "Never".

If you're driving around with the phone set to leave the Wifi on during sleep, it'll be constantly going in and out of range of various Wifi connections. If the Wifi part is set for passive scan, then the processor needs to be kept up as the Wifi chip attempts to collect the BSSID of all the network it see and processor tries to figure out whether these networks have been seen or not. If it's set for active scan, then the Wifi needs to power up the radios to transmit beacons. Pretty bad news in either case, unfortunately.

Re: If You're Programming a Cell Phone Like a Server, You're Doing it Wrong

#56
post #50

Earlier quoted context omitted.

Anecdata: I turn my phone's wifi off when not required because it has a significant effect on the battery life - particularly when I'm not in range of a wifi signal. I've had a full charge die on an 8-hour country drive because wifi was on. On the return trip, wifi was turned off and it behaved as expected. Go for a long walk in the park? All that time your phone's wifi is straining to find a wifi signal. It's not as…

Wifi scanning is also pretty terrible for power consumption as you found out. If you are on Android, my guess is that your phone was set to be connected to Wifi during sleep. You can control it by going to Wifi networks section -> menu -> Advanced -> Keep Wi-Fi on during sleep, then setting it to "Only when plugged in" or "Never". If you're driving around with the phone set to leave the Wifi on during sleep, it'll be…

I personally setup Llama to turn the wifi when I'm not connected to a tower that I previously defined as "wifi available". It's nifty because I never have to explicitly turn wifi off then back on during my travels. To add a new wifi zone, I simply add the cell tower to the previous list and let the app do the rest.

Llama by itself uses less battery than leaving the wifi chip on, even in passive scanning mode, as the phone is always looking and checking cell towers.

Re: If You're Programming a Cell Phone Like a Server, You're Doing it Wrong

#57

> Every decision you make should be based on minimizing the number of times the radio powers up. This is lunacy. Ok, lunacy is a bit strong. But I disagree with this and am throwing a "premature optimization" flag. Modern phone batteries last plenty long, and the radio being on is nothing compared to the big bright screen. /edit Given the opportunity to chose, I know I would gladly sacrifice a few minutes per charge…

The difference between DCH (the full-on high power state) and PCH (the lowest power, waiting-for-paging state) is about 2-orders of magnitude. Last time I measured, it was about ~100mA vs ~1mA (at ~3.7V) used by the radio. So, it's not couple of minutes of battery life, but rather _many_ hours of standby life instead. People usually aren't very happy when a fully charged phone dies overnight. I've seen apps do some c…

That kind of frequent-on behavior is why the iPhone 5S has the new M7 chip.

Re: If You're Programming a Cell Phone Like a Server, You're Doing it Wrong

#58

> Every decision you make should be based on minimizing the number of times the radio powers up. This is lunacy. Ok, lunacy is a bit strong. But I disagree with this and am throwing a "premature optimization" flag. Modern phone batteries last plenty long, and the radio being on is nothing compared to the big bright screen. /edit Given the opportunity to chose, I know I would gladly sacrifice a few minutes per charge…

i would like to place a bet that these measurements are made in an environment where the only app running is your own.

that said, this is hardly new knowledge. or is it? i thought there had been articles about it a couple of years back, and even though i would consider this advanced common sense there is a high chance that i would take this optimization into account right away.

Re: If You're Programming a Cell Phone Like a Server, You're Doing it Wrong

#59

> Every decision you make should be based on minimizing the number of times the radio powers up. This is lunacy. Ok, lunacy is a bit strong. But I disagree with this and am throwing a "premature optimization" flag. Modern phone batteries last plenty long, and the radio being on is nothing compared to the big bright screen. /edit Given the opportunity to chose, I know I would gladly sacrifice a few minutes per charge…

Respecting known limitations and working within best practices is absolutely not premature optimization. If you know that using the mobile radio in a certain way is a source of excessive battery usage, it's silly to just disregard this information. It's not premature optimization if you know where the performance issues are from the outset, and these performance issues are so incredibly common that the Android team p…

I just want to add in from a network carrier perspective.

I work for a mobile operator, and have had to work on several network issues related to poorly designed apps. I agree with kintamanimatt on this, it's not premature optimization, you need to know the cost of what you're doing. There is an incredible resource cost, to all the naive developers making apps these days, that treat the wireless interface as an always on connection.

1. Server polling every minute We actually sent one customer an invoice for over $100,000 because they had a particularly bad application that polled a server every minute. This was only an internal app used by a few hundred people, but they used it at their office, and it caused constant blocking on the cell site covering their office. This actually broke cellular coverage near their office, and caused issues for all the other customers in the same area. Ultimately, we said, we can help you fix you're app, you can pay the invoice to upgrade cell coverage to you're office, or get off our network. In this instance, we helped them fix their app, so it only interacted with the server when something needed to be changed (the server pushed changes), which was actually quite easy to do. This simply isn't the same thing, as writing an inner loop in assembly, to shave off a few microseconds in runtime, which would be premature optimization, and actually making robust software that works well wherever it is used.

2. Synchronized network access This one has been the bane of us on a few occasions. If you write a network access to be like a cron entry, that run's not only every 15 minutes, but does so at exactly the same time between all devices. We get to hunt you down, and scream, as our wireless network crumbles. When every device, wakes up at the same time, and asks for a higher power state, at exactly the same time, the network will only process so many. The funny thing is, you'll test this yourself and not see anything, but then when you release your app, you might find 30% of those checks are failing for some reason, and struggle to figure out why. For any one who is thinking, well the operator should add more capacity, the answer is we do, but ultimately the customer pays for the network build.

3. High usage apps What happens is, your device vendor, is in the interest of protecting it's customers as well. We once had an app on our network, that would treat all reject codes, as a cause to retry. This caused it, to every time it was run, begin uploading data, over and over again, leading to thousands of dollars in bills to the customers for excessive data usage. The device vendor, pulled the app from their stores.

Now, how do you feel, about having to plead the case why you should be allowed to continue to sell your product, not only to the device vendor, but the carrier who found the problem, prove that you fixed it, have our bureaucracy test that you actually fixed it, and maybe if we feel like it, let you sell again. It's not as simple as fix the bugs, you might actually lose you're ability to make money of you're software, for months while this get's sorted out.

What do you think you're store rating will be, when all your customers get sent a $5000 bill for using your app?

Now the question is, should there be a better way, and I'd like to see more from mobile vendors, in making it stupid easy to be smart about this. In the phone API's, if I need to do a background update, I should be able to register with the OS, and say, I have an update to do, within the next 15 minutes, let me know when you have an active radio connection. You're phone goes into a high powered radio connection, and bam, all you're background updates now go out together. Need to send notifications, here is our push API, only interact with the mobile when an update needs to be done. If the API's you're using, are designed for mobile, and take alot of this network stuff that you shouldn't need to know into account, it's a lot easier for the naive developers to do better by blind luck.

-- * my views in this post are my own, and do not reflect those of my employer.

Re: If You're Programming a Cell Phone Like a Server, You're Doing it Wrong

#60

Earlier quoted context omitted.

I can't speak for websockets, but holding a TCP connection open indefinitely does not require that the radio stay in its active state. On iOS, setting the kCFStreamNetworkServiceTypeVoIP property on the connection allows the radio to return to the idle state and reawake when it receives data on the socket. Android may have similar APIs.

Wouldn't the radio have to wake on a timer to scan? How else will you notice there is data waiting on the other side of the connection?

The radio is in a low power idle state, the same state used to wait for incoming phone calls and text messages.
Post reply on HN