Wednesday, January 04, 2012

Mobile Application Architecture, Part 3

[Note: So it's taken me a long time to get this third part out. I changed jobs in the middle of writing all of this, and there's a lot to learn at the new gig. Also in case you are wondering, this is heavily based on my experiences working on eBay Mobile for Android and Bump for Android. Both apps use an event driven architecture in places, or at least they did while I was working on them. What follows is not the secret sauce from either application, but some good things that were common to both along with some ideas for improving on the patterns.]

This is the third post in a series on the architecture of mobile applications. In the first post we looked at the classical architecture of mobile apps that evolved from web apps. In the second post we looked at event driven architecture and when it should be used. Now we're going to dive a little deeper into event driven architectures and in particular talk about some of the significant design decisions that must be made and the pitfalls that need to be avoided.

So you say you want an event driven architecture? I'll assume you've done your homework and aren't just doing this to because it seems like the cool thing to do. I'll assume that it really fits your application. One of the first things that you will realize as you get started is that event driven architecture does not exempt you from many important aspects. Security is a great example. You must figure out how you will authenticate with your server and how to secure any sensitive information that you send over the wire. Just because you are no longer in the world of HTTP and HTTPS does not mean that you get to send over people's credit card numbers in plain text. It's not any harder to sniff your bits and chances are that you are just as vulnerable to things like man-in-the-middle attacks. So go read up on things like TCP headers and TLS.

Once the basics are in place, you can think more about the architecture of your application. Your servers are going to be sending events to your application. So what is going to receive them? Do you have each UI (controller, Activity, etc.) receive them? How about something that is more long-lived. The most important thing to remember is that whatever receives events generally has to be able to deal with any kind of event. You can never assume that just because your controller only cares about some small set of events that those will be the only events that it ever receives. Thus you probably need to have an event receiver whose lifecyle extends beyond that of any given screen in your application and can handle any and all events. Now keep in mind that it will often delegate events to other components in your application, so no need to worry about this global receiver being stuffed with knowledge of your app. On Android you could use a Service for this or even a subclass of Application. You could also create some other singleton object that is tied to your application's lifecycle.

Now you might ask does this really need to be a singleton? Not necessarily. However I think that in many (most?) circumstances that a singleton will simplify things for you. What happens to those events that are not needed by the UI? Such events probably mutate some internal state of your application. If that internal state is transient, then it probably makes sense for there to be only one copy of it. If it is persistent... 

Inevitably there will be some events coming in from your server that you do care about at the UI level. These things will probably be "second-hand", i.e. the events will have been received by the longer-lived object we talked about above. Now you need to deal with how to get these events to the UI. The most accepted way to do this is to use an Observer pattern. You define an interface for an object that cares about a certain event (or certain set of related events potentially.) Then the object registers itself with the central receiver. When a new event comes in from the server, the central receiver examines it and decides on who to delegate it to.

I must add a few words of caution here. For each event you can easily wind up with a class for the event's data structure, an interface for the observer, and an implementation of the observer. The last of these things is often handled by having the Activity/Fragment/controller implement the interface, or by using an anonymous inner class to implement it. In any case, you wind up with multiple types being defined for each event. So if you have many types of events that your server will push to your application then you can end up with an explosion of code. Android developers should consider using Handlers and Messages as a leaner, more de-coupled solution. The big downside is that you may windup doing manual type checking and casting. This can be avoided by late binding, i.e. keep the data from the server raw as long as possible and do the parsing after the dispatch to the observer.

Finally I will close this long series of posts with a brief word about server side technologies. This post in particular has focussed on how to write your mobile/client applications with an event driven architecture. There is a flip-side to this and that is how to implement an event driven architecture on your server. Just like in our classical architectures, most servers are dominated by HTTP. That is not only the default interface into them, but it is at the heart of the server's architecture. A connection is a short-lived, usually stateless object. There is generally a one-to-one mapping of HTTP requests to either threads or processes. This is a poor fit for event driven architectures where a connection is extremely long lived. It only dies when the client app dies or explicitly closes it for some reason.

Fortunately there are numerous server side technologies that do not use the request-per-thread/process model. Certainly the most hyped one of these is Node.js. The Internet is chock full of benchmarks showing how Node's non-blocking out-scales Apache, Nginx, etc. However there are numerous other event driven app servers in Python, Java, Ruby, etc. Personally I have a bias towards my ex-colleague Jamie Turner's Diesel framework. The point is that there are many choices on the server and I expect that you will see many more moving forward.

17 comments:

  1. This is one of the progressive post.I like your blog quality.Great work.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Anonymous9:20 PM

    CUSTOMERS ARE MOVING TO MOBILE. YOU FEELING LEFT BEHIND?
    You must have magento mobile apps, right away! Because this is the best way to interact and convert sales from mobile users (PROVED)
    Go Magento mobile apps to convert sales way better!

    SimiCart is a solution that helps Magento Merchants build mobile shopping apps.

    Magento merchants: Our solution is specially designed for Magento platform so you can get all the best features of Magento to apps. If you use a solution for every platform you're missing many good things.

    Mobile shopping apps: You will have apps that are coded separately and specifically for iPhone and android.
    Your customers can search and download your shopping apps from Google Play and Apple stores. And again, such tailored apps will bring the best user experience!

    ReplyDelete
  4. Good work…unique site and interesting too… keep it up…looking forward for more updates.Good luck to all of you and thanks so much for your hard-work.
    asi legal bangalore, asi legal,asi legal cochin,Shahul hameed

    ReplyDelete
  5. Amazing Post, I really like your post. If any YouTube users need help regarding youtube.com/activate then they can contact us.

    ReplyDelete
  6. Thank you for sharing nice article.Astroswamig is the top most astrology prediction portal here you can get live astrology prediction you can ask question about your past present and future or get remedies of your problems.

    Talk To Astrologer
    Aaj ka Rashifal
    Online Astrologer
    Free kundli Prediction

    ReplyDelete

  7. The best Bitcoin private key hack online tool
    https://smartprivatekeyhack.com
    Bitcoin private key hack online

    ReplyDelete

  8. We cover a broad-band of buyers’ purchase ability as undetectable fake banknotes supply is
    concerned. Bulk and retail unbeatable prices on quality Euro, Great British Pounds,
    USA dollars, Canadian and Australian dollars, and more other major currencies.
    buy counterfeit dollars online and
    best quality counterfeit pounds for sale
    first class fake banknotes for sale
    Bitcoin private key generator software

    ReplyDelete
  9. Top-notch counterfeit offers a vast experience in the counterfeit money industry,
    Bulk and retail unbeatable prices on quality Euro, Great British Pounds,
    USA dollars, Canadian and Australian dollars, and more other major currencies.
    Grade A counterfeit banknotes for sale and
    Top-Notch counterfeit banknotes
    Buy SSD Chemical solution

    ReplyDelete


  10. Welcome to Caluanie Muelear Metal Crushing, a leading, United Sates based manufacturer and supplier of Caluanie Muelear Oxidize. Committed to bringing you the highest quality products at the most competitive prices, we are here to simplify your business and life.We are the leading suppliers of Caluanie Muelear Oxidize in the USA. Best quality offer Caluanie Muelear Oxidize at a cheaper price. Also, provide reliable & safe delivery of Caluanie Muelear Oxidize in USA, UK & worldwide. Caluanie Muelear Oxidize is one of the most useful Caluanie derivatives. Caluanie Muelear Oxidize can be used in Caluanie Foundation, Caluanie Oil & Caluanie Crude as a base impurity. Caluanie Muelear Oxidize is used as a base impurity in making other chemical products like refinery processes and it’s also used for medicinal purposes. Caluanie Muelear Oxidize is a chemical substance with the formula (C15H23O2)3, occurring as an impurity in many types of natural oil. It has been isolated from fossil fuels including coal and petroleum. It is used as a reagent to prepare other derivatives of Caluanie esters, such as methyl 3-methyloxepanone carboxylate.

    https://caluaniemuelearmetalcrushing.com/

    https://caluaniemuelearmetalcrushing.com/blogs/

    https://caluaniemuelearmetalcrushing.com/product/buy-caluanie-muelear-oxidize-online/

    https://caluaniemuelearmetalcrushing.com/caluanie-muelear-manufacturer/

    https://caluaniemuelearmetalcrushing.com/buy-caluanie-muelear-oxidize-for-metal-crushing/

    https://caluaniemuelearmetalcrushing.com/caluanie-muelear-oxidize-for-sale-usa/

    ReplyDelete
  11. If you have lost your digital wallet, the first
    thing to do is to make sure that you’re
    on a computer that is connected to the internet.
    bitcoin private key finder software online
    Blockchain private key generator
    Generate private key from bitcoin address online
    website: www.smartprivatekeyhack.com
    Email: info@smartprivatekeyhack.com
    WhatsApp: +1 (646) 926-0214
    Telegram: @Tonybright77

    ReplyDelete
  12. Anonymous8:32 AM


    Best Bitcoin private key generator online 2022 at your disposal
    The person who holds the private key fully controls the coins in that wallet.
    For this reason, you should keep it secret. And if you really want to generate
    Best Bitcoin private key generator online 2022 at your disposal
    https://smartprivatekeyhack.com

    ReplyDelete
  13. We are the leading suppliers of Caluanie Muelear Oxidize in the USA.
    Best quality offer Caluanie Muelear Oxidize at a cheaper price.
    Also, provide reliable & safe delivery of Caluanie Muelear Oxidize in
    USA, UK & worldwide. Caluanie Muelear Oxidize is one of the most useful Caluanie derivatives.
    Caluanie Muelear Oxidize can be used in Caluanie Foundation,
    Caluanie Oil & Caluanie Crude as a base impurity.
    https://popularbanknotes.com
    https://caluaniemuelearmetalcrushing.com
    https://smartprivatekeyhack.com
    https://topnotchcounterfeit.com
    https://www.flymedishop.com

    ReplyDelete
  14. https://chemglobalexport.com/buy-ammonium-chloride-online/
    Buy Ammonium Chloride Online


    AMMONIUM CHLORIDE .Dry cell. used as electrolyte in zinc-carbon batteries.
    2.Metalwork.as a flux in preparing metals to be tin coated, galvanized or soldered.
    3.Other applications. Used to work on oil wells with clay swelling problems. Other uses include in hair shampoo, in the glue that bonds plywood, and in cleaning products. In hair shampoo, it is used as a thickening agent in ammonium-based surfactant systems, such as ammonium lauryl sulfate. Ammonium chlorides used in the textile and leather industry in dyeing , tanning, textile printing and to luster cotton.

     

    ReplyDelete
  15. https://www.counterfeitdeals.com/2019/06/28/grade-a-counterfeit-money-for-sale/

    ReplyDelete
  16. In the modern, fast-paced world we live in, technology has completely transformed the way we consume media. The days when we had to rely on traditional television sets to watch our favorite shows and movies are long gone. With the advent of smart TVs and mobile technology, we now have the convenience of accessing TV listings and guides right at our fingertips. This has not only made our lives easier but also significantly enhanced our viewing experience. In this article, we will delve deeper into the integration of XMLTV on smart TVs and mobile phones, and how it has revolutionized the way we watch TV.

    ReplyDelete