I'm having a debate with some engineers and trying to decide on the right choice here. I'd like to get your thoughts on this topic. Totally subjective.
We've built an API for an iPhone app. It connects to our pre-existing business layer. The API is stateless but requires authentication for most calls/actions. But because there is authentication required, the API was originally designed to make a DB call first to get the User object, and then to extract our ID from that and use it to perform the requested action. So all in all - this design means a minimum of 2 DB calls for every action performed.
The alternative was to change the design so that each action didn't require 2 DB calls, but that the call would include the ID, username, pwd encrypted into one hash tag, which could then be decrypted on the server side API to remove the need to make another DB call just to get the ID for the requested action.
The argument is that with the extra code to encrypt/decrypt that it would be a costly choice to include this additional code and wasn't worth the extra time to change it just to reduce the DB calls by one.
This seems like a no brainer to me, but please enlighten me with your thoughts. My thought is that I'd rather take the hit internally in code maintenance costs, and not put that extra wait time on the users.
Again, totally subjective. Please forgive me for my ignorance. I'm a single engineer maintaining a lot of code on a high-volume site. Both speed and code maintenance costs are important to me.
Additional Info
Great responses everyone! Here are a few more details.It's correct, it's not an extra round trip to from the iPhone to the server, but from the web server to the db server. 2 WebSvr-to-DB trips for any action performed on the iPhone vs 1 when using the encryption technique mentioned.
I have not measured or benchmarked anything, as part of the argument is about limited resources. Benchmarking it is certainly how I'd prefer to decide, but this will ultimately take twice or three times as long as just making the change. Maybe it's best to bite the bullet on it? Data driven decisions provide the most confidence.
Speed on the website, DB and iPhone are already an issue. So doing what we can to speed it up is something we are looking to accomplish. This greatly contributes to the decision and should have mentioned it initially.
About network latency + cost of the DB lookup. I can only speed up the DB call and not the latency unless we decide to do some type of caching. My thought is is that decrypting one string will always be faster than the latency of a round trip to the DB. I will do a quick test on this.
I'm still leaning toward my push to reduce the DB calls because it's seems vague to consider the cost of code maintenance in this case since the difference in code we're talking about it simply the code it takes to encrypt/decrypt a string. If performance is one of our concerns, choosing to add more load to the DB just makes optimization more of an urgent matter because we didn't make a conscious decision to reduce DB resources when we had a chance.
Besides my need to decide on this soon and move forward, I find this question extremely interesting and find myself in these kinds of debates more and more lately.