<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Andrew Chan on Medium]]></title>
        <description><![CDATA[Stories by Andrew Chan on Medium]]></description>
        <link>https://medium.com/@andrewchan_73514?source=rss-88dcb25323f6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*-xmBQt8beBGl6DbeUHR8zg.jpeg</url>
            <title>Stories by Andrew Chan on Medium</title>
            <link>https://medium.com/@andrewchan_73514?source=rss-88dcb25323f6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 03:56:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@andrewchan_73514/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Event Sourcing System in Finance]]></title>
            <link>https://medium.com/@andrewchan_73514/event-sourcing-system-in-finance-27bda4408ffb?source=rss-88dcb25323f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/27bda4408ffb</guid>
            <category><![CDATA[event-sourcing]]></category>
            <dc:creator><![CDATA[Andrew Chan]]></dc:creator>
            <pubDate>Sat, 21 Sep 2019 06:52:12 GMT</pubDate>
            <atom:updated>2019-09-21T06:52:12.303Z</atom:updated>
            <content:encoded><![CDATA[<p>(Trading, Betting and Payment)</p><p>Common practices of relying on ACID properties of database in resolving conflicts and recovering from failure would not scale in high-concurrency, low-latency use cases (e.g., trading, betting, payment).</p><p>This sharing offers a quick overview of event sourcing framework, and how one can build a fully working ledger using simply commodity, open sourced products (e.g., Apache Kafka).</p><p>This sharing further explores a more complicated use cases, where stochastic scenarios could be baked into traditional event sourcing to produce real time, big data type, operational intelligence.</p><p>You can find the code used in the sharing at my GitHub.</p><p><a href="https://github.com/andrewkkchan/client-ledger-service">andrewkkchan/client-ledger-service</a></p><h3>Why Event Sourcing?</h3><p>Ledger which relies on database ACID properties will handle up to 10–20 transaction per second. When pushed over that, there is a chance that the business rule (e.g., client ledger balance must not be overdrawn, a.k.a., &lt;0) will be broken by racing condition and thread visibility. This code base is a basic set up for mobile/web environment to install event sourcing ledger functionality. And even with such a primitive set up, the http endpoint can easily handle 1,000 transactions per second, with 100% guarantee of holding business rules.</p><h3>What about 1 million per second?</h3><p>In commercial products with trading, betting and payment enterprises, I have achieved 1 million transaction per second while holding all business rules intact. A step-by-step customisation and specialisation on software and hardware is required.</p><h3>Step 1: To 10,000 per second</h3><ul><li>Replace HTTP endpoint with TCP endpoint. For example, use Kafka/Solace as incoming message handlers — which would remove the limitation of HTTP protocol and HTTP server overhead.</li></ul><h3>Step 2: To 100,000 per second</h3><ul><li>Replace Messaging endpoint on TCP with those on UDP. You will however need to maintain reliable in sequence transfer of events.</li><li>Replace non-positional data encoding (e.g., JSON, XML) for positional data encoding (e.g., protobuf, SBE)</li><li>Install competing consumers, with at least one of which being implemented in-memory, to cope with the burst of events and support query during burst. Persisting consumer will eventually catch up.</li></ul><h3>Step 3: To 1,000,000 per second</h3><ul><li>Avoid garbage collection using pre-allocated ring buffer and other technique.</li></ul><h3>Further Case Study: Scenario Based Event Sourcing</h3><p>A further use case for event sourcing goes beyond simple ledger into stochastic scenario based data intelligence.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*db2d3PTdJnj_KMeIPLzdbw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*P1Xikj7wqJ8VSWbY6zObZg.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=27bda4408ffb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Curious Case of Memory in Mobile Architecture]]></title>
            <link>https://medium.com/@andrewchan_73514/the-curious-case-of-memory-in-mobile-architecture-683234ed1ea7?source=rss-88dcb25323f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/683234ed1ea7</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[memory-leak]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <dc:creator><![CDATA[Andrew Chan]]></dc:creator>
            <pubDate>Tue, 16 Jan 2018 05:46:52 GMT</pubDate>
            <atom:updated>2018-01-16T06:53:48.557Z</atom:updated>
            <content:encoded><![CDATA[<p>Enough has been written in this blog on the architecture of mobile, and now I would like to go into the implementation side — yet as I always strive to, at the same time — reveal the intrinsic connection between a sensible architecture and a sound implementation.</p><p>Supposed you now have settled on the Fully Scalable Stage, and implemented the supporting micro-services and distributed the workload and data sensibly. You have done 20% of work. 80% of work will be on mobile — implementing a user session, planting many independent, singleton controllers each delivering its own functionality, from user authentication, product discovery, to check-out, social blog, instant messaging, video editing, and so on. You go on with an annotation-based, JPA style, object database to persist the states which comes from your Bean-style finite state machine. Then each user interaction can be viewed as a transition between states, an edge between nodes in the graph metaphor. Valid edges are accounted for, invalid edges are handled as exception.</p><p>With controllers in place, you build your rich UI experience, with all sorts of newest, fancy native view components which iOS and Android so generously offer. Some views are custom-built, whereas some views recur — in iOS the most commonly used recurring view is UICollectionView, whereas in Android the Recycler View.</p><p>Think about, say a case which the user discovers products — so let’s say you have a million products — then you have a million views, simplistically. And let’s say your user then browse a magazine which has a thousand pages, so now you generates a thousand collections of views. Of course we have recycling, but we also have cases which recycling does not work all time. People sometimes forgot how much information, views and contents are drawn during normal usage of app, on such a constrained device — yet engineer overcomes this through stringent lifecycle.</p><p>Each of the memory-hungry item, especially views which draw a lot, respond to user frequently, and contain big bitmap and complicated states, comes with a stringent lifecycle. Generally speaking, the more memory the item use, the more stringent the lifecycle needs to be. And when lifecycle is breached, the app runs quickly out of memory and crash — which is still the second most commonly seen causes of crashes apart from null pointers.</p><p>There are many well-written literatures and blogs on using well-crafted tools to meter the memory usage, identify the leakage (like static class, dangling reference, etc), and fix leaks. You can consult articles like this:</p><p><a href="http://blog.nimbledroid.com/2016/05/23/memory-leaks.html">Eight Ways Your Android App Can Leak Memory</a></p><p>While I benefit a lot from reading the above, and such test-driven approach is always good to development, I want to cover something different — avoiding leaks from architecture.</p><p>I want to highlight how coherent, loosely-coupled, distributed micro-services on server, with a equally coherent, loosely-coupled, independent singleton controllers, go a long way to avoid leakage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HxjGg7i0s0nFSwHHWsj5jA.png" /><figcaption>Figure 1: Clean interface to cut off objects with different lifecycles and avoid memory leak</figcaption></figure><p>Let’s visualise our app as above — on one side, we have self-contained, object-persisted, singleton, thread-safe controllers implementing state transition, and exists as long as the app runs. On the other side, we have these memory heavy, rich UI view components which are transient in nature, meaning the views are created on the fly, at run time, and quickly destroyed once its purposed is fulfilled. And there is a clean interface in between (protocol in iOS term), with the sole purpose being separating the two worlds each with its own lifecycle.</p><p>More and richer functionality on the app is often fulfilled by heavy interaction between the singleton controllers — each being self-contained, they nonetheless can message each other to fulfil a function, which would otherwise need to be fulfilled on server side usually by a database join. The resulting diagram depicts this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NqZQ3h5qEoYkKc6qfgb8qg.png" /><figcaption>Figure 2: Singleton controllers interacting to provide functionality and avoid database joins</figcaption></figure><p>All these interactions are acceptable as long as they do not bypass the interface. However, breaching the interfaces shall cause memory leakage, as depicted below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MeJFb57WgKSfl4Ww2zMUvQ.png" /><figcaption>Figure 3: Red line showing the interface is breached, and memory leaks might follow</figcaption></figure><p>Here the interface is bypassed, and let’s say, the login pages somehow is directly referenced by the authentication controller. Memory leaks will occur, because the page is supposed to die, while the controller living long holds onto the page preventing the latter’s death.</p><p>So if we have clear architecture in mind, and code with clean interface, we not only simplify the code, but also avoid a lot of tuning/fixing time on memory leaks and errors.</p><p>Coding like above shall prove natural for someone coming from J2EE backgrounds, someone who codes embedded devices, or even someone who code desktop offline apps. However, it takes a wholesale change of paradigm for (more traditional) web developers who tend to embed states, logics and validation rules into the UI classes.</p><p>Binary nature of objects, long-live vs. transient, persistent vs. volatile, states machine vs. pure UI, aren’t that clear cut once you get into real development for real business problems. The key here is to always distinguish the binary nature of otherwise a single piece of work/component, and to break it down accordingly. For example, when implementing a swipe-to-buy button flashing with discount, you shall not think of it as a button. Within, there is a controller element of price (with transition from original to discount, based on timing and condition. There is also a view element, a timed animation loop of flashing, and a UI responder to swipe action. Swiping belongs to UI, while buying belongs to controller — and the two must be distinguished, and the only trigger is through interface from swipe to buy. Initially, this would require writing more lines of codes, and complicating the intuitive behaviours — yet the cost would be more than repaid by better testability, less leak, more readable code, more robust to changes in UI, and few issues with concurrency.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=683234ed1ea7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Many stages of mobile]]></title>
            <link>https://medium.com/@andrewchan_73514/many-stages-of-mobile-ba6bd9851315?source=rss-88dcb25323f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/ba6bd9851315</guid>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Andrew Chan]]></dc:creator>
            <pubDate>Tue, 16 Jan 2018 02:14:04 GMT</pubDate>
            <atom:updated>2018-01-16T04:18:12.009Z</atom:updated>
            <content:encoded><![CDATA[<p>So here comes my sixteenth piece of reflection, of my past decades working in mobile development — and I realise, after talking to my friends (acknowledgement goes to CK who has always been a wonderful listener and himself a talented developer and technologist) and getting valuable, constructive feedbacks, that I have too quickly focused on one specific stage of mobile application/scenario/system, and omitted the many stages of mobile generally happening in different enterprise spaces.</p><p>I therefore would like to add this chapter, which actually shall come in first — but with hindsight, I may articulate the topics in a better light — that there are many stage along the journey of developing your app, and my suggestions before this applies to but one stage only.</p><p>I would summarise the stages, for easier discussion, as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*kBmyF-0mKQhDiOC0mrxZmw.png" /><figcaption>Figure 1: Render Stage</figcaption></figure><p>Stage 1 is the <strong>Render Stage</strong> — which is usually the first stage where the enterprise sees the first glimpse of light into the era of mobility. This is where I usually enter a project — and where many people goes with wrapping the legacy system quickly with some mobilisation UI tools, e.g., HTML5, CSS framework. A lot of these are web-based, and there is few needs to go native. There is abundant vendors, each with their own promises and provision, competing fiercely — and where the big “WOW” and magic goes to — when an otherwise irrelevant out-dated legacy service is suddenly accessible to mobile users. Businesses with lower requirement goes with mobile web, whereas those with higher requirements often settle rightly on solutions such as Cordova/Mono/React Native — where native UI components can go into rendering. In a lot of cases, this is the sweetest part of the journey, the so-called honeymoon for developers — and every tools and framework I list in previous chapters seem like over-kill or unreasonably complex.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5S9ynDau6_DZNwUWLT_ruA.png" /><figcaption>Figure 2: Transaction Stage</figcaption></figure><p>Stage 2 is the <strong>Transaction Stage</strong> — when, for example, the enterprise wants now not only to show something on the phone, but also allows that something to go live for buying, checking-in, assigning — with all kinds of actions to be “moved” to the phone. In actual projects, this is where the boss/sponsor is very happy with the result in Render Stage, and would like to go for more — yet often resulting in frustration — because of the huge effort to go beyond Stage 1 in a legacy system. Here, the vendor’s promise often pales — for that planting native UI into a container is easy, planting logic into native code is much harder — and the participation of the system/backend team is critical for any success. Some vendors provide Enterprise Service Bus (ESB) type of solutions, and successfully wrap the legacy system which churns out full-blown HTML/ proprietary app protocol, into more commonly used JSON format API for mobile — and could prove to be extremely helpful in this stage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fdvVbMs5e6O84YA_35TxGA.png" /><figcaption>Figure 3: Frontend Scalability Stage</figcaption></figure><p>Stage 3 is the <strong>Frontend Scalability Stage </strong>— where the transaction no longer works out for more users, and for more time-critical, performance-conscious actions. Let’s suppose you pick the ESB solution for Stage 2, and you goes down the native path correctly, and have all your legacy services transformed into JSON API — so you think your enterprise mobile transformation can be considered done.</p><p>Not that early, and not that if your app goes viral within the enterprise and more users are using, demanding more functionality.</p><p>You will soon discover that, those legacy-service-transformed-into-JSON-by-ESB API is never designed for many, concurrent users. Despite the format, the actual performance of the API degrades significantly on more users, due mainly to the long session by the system, heavy locking on the database, and bulk loading on the schema transport. Atomicity and performance seems to always go against each other, while adding functionality could be a nightmare because a minor tweak would require a non-proportionally large change to the underlying monolithic system.</p><p>So you come up with the hype of micro-service and hear that it is a better alternative in building scalable and performant mobile app backend when compared with ESB. You want to go fully micro-service, but usually you will have to settle with some kind of hybrid solutions where micro-service would be on the tier close to client, while you retain ESB on some legacy part which you deem little used and too costly to transform.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fMcK7KZOiWXTSKccuUhWmg.png" /><figcaption>Figure 4: Backend Scalability Stage</figcaption></figure><p>Stage 4 is the <strong>Backend Scalability Stage </strong>— which could be a slow reckoning of bottleneck when high concurrency is required. So supposed you are lucky, and convince the server team and enterprise IT to help transform all legacy services into Micro-Service Architecture — and more, you also get these deployed to cloud and be easily assessable on mobile phones anywhere.</p><p>You shall then see your app now supports a lot more users, and can provide a lot more functionality at a much shorter time with less efforts — because new functionality means adding a decentralised service instead of battling with the legacy system.</p><p>And you are happy you go along the native path, for now you see a lot of business logic and user session management can only be done on the phone, given that all endpoints are stateless. Native code provides great performance gain, and users can often use your app smoothly even under poor network, like they never did before.</p><p>You think this is the end of the journey — until one day, a flash sale event, or a viral topic for photo-blogging, quickly brings down your system. You thought that you are running cloud, and the decentralised service can scale out easily, as you were told, so you are confused as to why this could still happen!</p><p>More in-depth review will show you that the database is your single point of failure. Whereas it is much easier to scale out the services layer, it is almost impossible to scale out the database. Database shredding, no matter by tables or records, are more or less a dark art. Operation like creating a post on the blogs can easily involves tens of joins across many tables — and suppose you do locking for concurrency, quickly results in deadlocks or degrading service level.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eSv-VDQ6FU3czPVG6bPMVQ.png" /><figcaption>Figure 5: Full scalability stage</figcaption></figure><p>Stage 5 is the<strong> Full Scalability Stage </strong>— which is the default stage I am talking about in all my previous chapters.</p><p>You realise that high concurrency always come with a trade-off, and you does not scale(distribute) the system if you cannot scale(distribute) the database.</p><p>So you start removing costly, non-robust joining operation across tables. Once done, you can move groups of tables into separate databases, host them in separate cloud and implement them in separate technologies (e.g., NoSQL).</p><p>Judgment calls when you separate data-repository layer and package them into your micro-service — what constitutes a perfect cut often depends on what your business goals are. For example, in my last project on building a mobile social commerce app, social features and shopping features are separable — each calls for different trade off on consistency vs concurrency. However, hashtag features may have to go with instant messaging into one component if heavy interaction is designed and anticipated.</p><p>Once you arrive at your cut, and separate everything into self-contained micro-services, you shall arrive at a full scalability. However you must realise that this comes with cost — cost which is especially heavy on mobile development. Now that the mobile team has to tackle two devils — the eventual consistency and the (apparently immediate) fulfilment of business logics.</p><p>Now that we arrive at full scalability, this also means that there is no longer a single source of truth at any time. Data consistency is maintained in each of the phone, and eventually synchronised, cross-checked, and exchanged onto the server space. Mobile app must therefore implement sophisticated enough caching and queuing mechanism, together with most of the business logics, based only on the now much more scalable, yet much less powerful micro-services. During the journey, you will find yourself more resemblance in building an offline app rather than a web page — and rest assured you will need the best grasp of a lot of fundamental computation, memory, and OS skills in getting things into the best shape.</p><p>This is the default stage — also the stage where mainstream consumer apps are delivering their services to millions of concurrent users every day across the globe. Enterprise users demands the same performance, service level, concurrency, and functionality today — and care must be exercised by developers in managing such expectation. For you can see, it is not an easy feat.</p><p>MyMM app which I last built is based on the technology of this stage.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*afdYZXeetI1Sb69LWwaztQ.png" /><figcaption>Figure 6: P2P (Blockchain) stage</figcaption></figure><p>Stage 6 is the <strong>P2P stage</strong> which is seldom seen — except with some cutting edge blockchain type of mobile payment solutions. Despite being experimental, I am convinced that this represents the gold standard when we call for extremely high level of concurrency, distributed computing, and user freedom/convenience.</p><p>There is unquestionably unjustified hypes on the cryptocurrency, in particular, the Bitcoins in today marketplaces. Blockchain, famous/notorious for enabling the Bitcoin, becomes a hot topics in most banks.</p><p>Yet Blockchain is nothing new, from a traditional programmer’s view. In fact, it is a natural next stage of the mobile development evolution — the tide of decentralisation, and the shift of efforts onto mobile clients, and the trends of giving away some consistency for higher concurrency.</p><p>Blockchain simply embeds the micro-services, which is hosted in the cloud in Stage 5, to the client nodes in Stage 6. In some cases, nodes designated as miners are often distinguished from nodes designated as consumers of the coins — so having a graph topology yet maintaining a duo-logy somewhat due to bandwidth and computation power. I believe in the future this line will blur, and we will have nodes which can both mine and consume the coins, probably on mobile.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*923rBoUBDQHksz_Kz48L1w.png" /><figcaption>‘Figure 7: Effort Distribution between mobile team and enterprise server team</figcaption></figure><p>The above figure charts the server vs mobile in terms of efforts and complexity — and summarises our discussion from render stage to P2P stage.</p><p>As we move along each of the stage, the efforts, demands and complexity also moves from the backend servers to the mobile clients. Given we have three billion installed-base of Android/iOS devices today, distributed around the geography of the world, connected by spotty 4G(and often degraded to 3G and worse still, EDGE) network, and millions of impatient users — it only makes sense to shift as much work to the client as possible!</p><p>Coin has its flip side — namely the efforts increased in mobile can often be ten times more than the efforts reduced in server. First of all, you need to code both iOS and Android (there is no way to escape native if you want to implement complex logic and achieve great performance). Then, distributed programming, with all sorts of eventual consistency, queuing and caching, are really at the forefront in our software engineering craft. It is often much more challenging to code a distributed system than a centralised system. Then of course there is the constant scarcity of good mobile resources in development and testing.</p><p>Yet, in most cases, eventually, you will more likely than not find yourself going along this path down your journey. The reason is that the advantage greatly outweighs the cost once your user grows, and your functionality builds up.</p><p>Throughout my career, I have never seen an app from an earlier stage winning an app from a later stage. This is a bold assertion, but in my mind, a truthful and useful insight. I have seen development teams, with extremely talented people, fails to impress the users with their hard work because the team adopts an earlier stage in mobile development. Evolution counts— and just like the old movie “Terminator”, it could be very hard to outsmart a T2000 with an earlier designed robot.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ba6bd9851315" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JPA metaphor in Mobile]]></title>
            <link>https://medium.com/@andrewchan_73514/jpa-metaphor-in-mobile-a8b0b4947a0d?source=rss-88dcb25323f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/a8b0b4947a0d</guid>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Andrew Chan]]></dc:creator>
            <pubDate>Mon, 15 Jan 2018 09:57:54 GMT</pubDate>
            <atom:updated>2018-01-15T09:57:54.709Z</atom:updated>
            <content:encoded><![CDATA[<p>Java Persistence API, known as JPA, has been widely adopted in the enterprise world. It is a good inspiration to persist our states following our last chapter in the bean-like implementation of singleton controller.</p><p>Since early days, both iOS and Android package a SQL-lite database with the source code — which is a simple database system complying most of the ANSI standard in SQL language. It quickly becomes a common practice to use raw-SQL in mobile development to persist objects and data — which is indeed substantially more performant and scalable than other available tools, like SharePreference in Android and NSDefault in iOS. Also it provides kind of interoperability between the iOS and Android platforms, as both share similar SQL code in handling data persistence, and queries (which often embeds the business logic inside).</p><p>However, in the enterprise world, meanwhile, most systems have abandoned raw-SQL in favour of Object Relationship Mapping (ORM) solution — for portability and design. Raw SQL in some cases are still justified due to its better performance(beware, this only applies to developers who are also experts in SQL — as modern ORM engines can often produce more performant SQL statements). However using ORM aligns the persistence layer with other components and follow the same object-oriented style and standards — for better encapsulation and decoupling. Moreover, the use of ORM allows changing of SQL engines more easily.</p><p>Not much has happened on mobile space, until very recently did we see ORM solutions arising on both iOS and Android.</p><p>Yet pretty quickly, people start to get unsatisfied with the performance and the translation needed from object layer to SQL layer — therefore a new form of database, namely object database comes into the mainstream role.</p><p>Object database is good for mobile, since there are usually one person’s data in a mobile use case — therefore concurrency can be kept minimal. Joining across table(now called collections) also are less required, as data selection can be done first, and subsequent calculation can be done in memory as the total volume of data would most likely be limited. Yet object model fits nicely with the other component in mobile, and the performance gain is huge.</p><p>Out of mobile object database, there are two branches — one dictates the inheritance of the database object by the entity you would like to persist, whereas the other adopts JPA style of annotation. Here it is interesting that, in my actual usage, the JPA style of annotation has been life-saver for projects which intend to adopt persistence half-way. It again shows that a lot can be borrowed from the J2EE world in the tackling of various problems in mobile space.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a8b0b4947a0d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Bean metaphor in Mobile]]></title>
            <link>https://medium.com/@andrewchan_73514/bean-metaphor-in-mobile-626463aa04f3?source=rss-88dcb25323f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/626463aa04f3</guid>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Andrew Chan]]></dc:creator>
            <pubDate>Mon, 15 Jan 2018 03:20:14 GMT</pubDate>
            <atom:updated>2018-01-15T03:20:14.414Z</atom:updated>
            <content:encoded><![CDATA[<p>Following last chapter, it is interesting to note that — as we are adopting a Mobile oriented development, with more features and power going to client side — there is a need to implement something previously always been implemented on server. One of those are Java Beans.</p><p>Bean is an interesting, decades old concept — where business logic are encapsulated and decoupled from other parts of the system. Whereas the other parts of the system in J2EE world could mean many things, from containers, web servlets, etc. — in mobile it mainly means UI elements. So getting the logic out into singleton controllers and completely separate those from UI elements are very much like what we have been doing years on J2EE.</p><p>With beans, one interesting thing we can do is to inverse the control and inject the dependency — what this means is the beans, being now decoupled, can have a different, separate, independent lifecycle as compared to the UI(in mobile) and requests(in Java servlet) — and thus be reused with or without state information and memory. A rightly performed dependency injection also greatly reduce the risk of memory leak — as long-lifecycle objects are separated from shorter-lifecycle objects, and managed separately, as long as the references between them are sorted out.</p><p>For example, in mobile, authentication (signup/login) functionality could often be decoupled into singleton controllers — as the user authentication state should not be changed when the signup page, for example, is dismissed and killed in its lifecycle. So, the authentication state, itself being implemented as a finite state machine, shall have a lifecycle much longer than the signup/login page — in fact, the state machine shall live forever unless user explicitly wants to erase by calling logout.</p><p>Better still, if we consult how beans are constructed and bake it into mobile, we could obtain further flexibility using dependency injection. For the same example, if we have three different kinds of authentication controller (mainly operates a finite state machine on user states) based on Mobile Number, WeChat and Facebook signup/login — we can inject the chosen one on run time to the app and in particular the UI element, so that the right logics and behaviours are immediately obtained — and successfully reused across the app.</p><p>What is different from beans, however, is that in mobile the controllers shall not implement serialisable API, instead, it shall remain a thread-safe singleton — which normally an inner static class and private constructor will do. The states, when persisted, also shall not take the full bean into disk — instead, a more refined storage of the states as an object into the object data-base would be more robust and performant. The difference here is that, in Java beans, the beans are often shared amongst different users, while in mobile app, the controller is used by one user. This is the intimacy nature of the mobile app — one reason making the app such an enjoyable way to consume and compute.</p><p>We will tackle disk and persistence in the next chapter, but let’s wrap this up — user states, given that we are adopting stateless HTTP micro-services, have to be well maintained in the app — and it is advisable to create bean-like singleton controllers to be injected on runtime for maximum decoupling, robustness, and reusability.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=626463aa04f3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Managing user session and business logic on mobile]]></title>
            <link>https://medium.com/@andrewchan_73514/managing-user-session-and-business-logic-on-mobile-b4542c0fb7f4?source=rss-88dcb25323f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/b4542c0fb7f4</guid>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Andrew Chan]]></dc:creator>
            <pubDate>Fri, 12 Jan 2018 04:22:25 GMT</pubDate>
            <atom:updated>2018-01-12T04:22:25.433Z</atom:updated>
            <content:encoded><![CDATA[<p>In J2EE, user session and business logic are often managed using Java Bean technology, private properties, no-arg constructor, and serialisable interface. These beans are persisted on server side, and often exist — most of the time — in memory and for reuse. Session bean shall be unique for each user while business logic beans can be shared amongst users.</p><p>As we migrate to a mobile architecture, I oppose to have beans, and states, on server any more — and in favour of a RESTful end points (in NodeJS or Java-REST). However, this does not mean business logic and user session disappear — which I am astonished to find that quite a few people believe that REST is simple because state is gone— actually all the states have to be reimplemented on the client side with distributed programming. And it actually becomes often more difficult and challenging — as this client-held state needs to take care of the latency across network, and the transient inconsistency thereon, and yet have to still maintain a sound state transition machine properly, often under multi-thread situation and with high performance requirement.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b4542c0fb7f4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What we learn from J2EE(or Spring Hibernate) for mobile]]></title>
            <link>https://medium.com/@andrewchan_73514/what-we-learn-from-j2ee-or-spring-hibernate-for-mobile-47fbe4de72eb?source=rss-88dcb25323f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/47fbe4de72eb</guid>
            <category><![CDATA[j2ee]]></category>
            <category><![CDATA[spring]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[java]]></category>
            <dc:creator><![CDATA[Andrew Chan]]></dc:creator>
            <pubDate>Thu, 11 Jan 2018 04:10:52 GMT</pubDate>
            <atom:updated>2018-01-11T04:10:52.490Z</atom:updated>
            <content:encoded><![CDATA[<p>In the enterprise cloud, most of the stacks either run Microsoft (.NET/VB) or Java technologies (which usually on top of Linux/Unix instances). I spend quite a lot of time coding in Android, with Java language recently, so it is natural for me to have more thinking and reflection on the enterprise Java technologies, and how this shall be viewed from mobile app point of view. I am sure some other developers who favours Microsoft stack could come up with similar points of views on a modern .NET way, like Entity Model, Code First and so on. But as myself being more familiar with Java, I will drill on this side of enterprise computing more as an example and inspiration.</p><p>Readers will remember that I would recommend nodeJS or Back-end as a service over the full-blown J2EE stack as your mobile backend — for reasons that J2EE is not really designed in mobile era for mobile app, so a large portions of the technologies would no longer be relevant. Yet, actually, I am myself a big fan of Java and I really like J2EE technology, and in particular its various standards and implementation, including JPA, JMS, Beans, etc. The trick here is to digest J2EE completely, and to go to source-level, so as to make it work in mobile. The Java Enterprise way is well proven for decades as a solid answer to the business requirements, on both functional and non-functional dimensions, and I advise all mobile developers working in mobile app to learn it well.</p><p>Sounds contradictory? Not quite. With J2EE here, we must not re-invent the wheels. Session management, data persistence, on top of solid Java philosophy, patterns, and all the literature on concurrency and performance — it would be unwise to discard all these. On the other hand, doing things up the sleeves and centralising everything is a terrible idea on mobile. So, the key here is to consult J2EE ways and implement its core, relevant functionality in a completely distributed manner.</p><p>With this angle, nothing rivals J2EE as a source of inspiration to a mobile enterprise developer, who wants to answer the many business requirements which have been answers in the last couple of decades, yet on different platform and form factor.</p><p>In the next couple of chapters, I would start on some business requirements and how J2EE tackles them in non-mobile space — and how we can implement the same on mobile, using Android as a solid example. I will try to keep things interesting to non-technical readers as well — so stay tuned.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=47fbe4de72eb" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mobile Pitfall #4: Vague concurrency and consistency requirement per feature]]></title>
            <link>https://medium.com/@andrewchan_73514/mobile-pitfall-4-vague-concurrency-and-consistency-requirement-per-feature-f16821dd3db0?source=rss-88dcb25323f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/f16821dd3db0</guid>
            <category><![CDATA[microservices]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <dc:creator><![CDATA[Andrew Chan]]></dc:creator>
            <pubDate>Thu, 11 Jan 2018 03:46:19 GMT</pubDate>
            <atom:updated>2018-01-11T03:46:19.926Z</atom:updated>
            <content:encoded><![CDATA[<p>So when you avoid all the previous pitfalls, and picked an approach which is agile and KPI-driven, focuses on getting job done and problems solved, congratulations! Your mobile journey is in great shape.</p><p>In subsequent chapters, I would propose a methodology to develop enterprise mobile app, based on the observations we have reached so far — and borrowed heavily from other well established frameworks, including J2EE, in certain enterprise non-mobile spaces, and show how we can twist it into a mobile equivalent.</p><p>But before reaching that, I would like to cover one final pitfall which I have seen repeatedly in my mobile career — a tendency to adopt a one-size-fit-all service level, concurrency and consistency requirement throughout the whole app.</p><p>In my last project, we have built a mobile commerce platform which runs entirely as mobile app on both Android and iOS — providing features in product discovery, product search, checkout and payment, order management, social blogging, newsfeed like Facebook, photo blogs like Instagram, hashtags like Twitter, and Instant Messenger used both between users and between user and merchant as customer service channel.</p><p>At the start of the project, there is no clear thinking, let alone definition, on the distinction of each of the feature’s concurrency and consistency requirement. As a result, even we are using micro-service architecture and been maximising the client role in providing performant distributed computing for the users, sometimes we still fall short in terms of expectation — due to the fact that we stick to one single service level. For example, the same concurrency/consistency trade off won’t apply for payment and photo-blogging — for checkout, you target high consistency as it involves potentially large sum of money transaction; whereas for blogging/tagging you definitely targets high concurrency and service level, so that it feels very smooth and people often tend to use the feature in a cluster.</p><p>And given all things equal, there always implies a trade off between the two: consistency and concurrency. We can have very high consistency writing the database, for example by using a RabbitMQ to synchronise all requests. But that request may not be reflected in the master database and thus all other users’ phone immediately — though the request would be reflected eventually — thus the name of eventual consistency. So by queueing the request we get to high concurrency and a slower consistency, and this type of trade off would be ideal for social networking features like blogging and tagging. Users won’t note the transient inconsistency if proper local caching, database and handling is done — and multi-device in most usage scenarios aren’t relevant.</p><p>On the other hand, we would have to block the user (this is the only case which we block the user’s UI completely) on payment — because an absolute real-time consistency has to be achieved. The concurrency and liveliness of the app is compromised — so this decision to favour absolute consistency over concurrency/liveliness must not be taken lightly.</p><p>If you follow all the advices which we made so far, you should have an micro-service architecture and a strong client computation/persistence/session. Thus applying different trade off on different features would simply involve putting these features onto different micro-services node and delivered through different infrastructure/cache/queue mechanism. A lot of efforts have to be done on the app, and we recommend abstracting and encapsulating such logic in singleton managers on the app — so here different feature singleton managers would apply different policy as to the consistency/concurrency trade off. In this way, the one-size-fit-all service level pitfall can be avoided.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f16821dd3db0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mobile Pitfall #3: Develop your mobile app strictly based on mock-up]]></title>
            <link>https://medium.com/@andrewchan_73514/mobile-pitfall-3-develop-your-mobile-app-strictly-based-on-mock-up-6150931aa974?source=rss-88dcb25323f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/6150931aa974</guid>
            <category><![CDATA[mobile-app-development]]></category>
            <dc:creator><![CDATA[Andrew Chan]]></dc:creator>
            <pubDate>Wed, 10 Jan 2018 09:33:51 GMT</pubDate>
            <atom:updated>2018-01-10T09:33:51.726Z</atom:updated>
            <content:encoded><![CDATA[<p>There are teams which approach mobile app as an wrapper, extension of an otherwise intact enterprise system, only to be slightly modified to allow for mobile usage. We have examined how this thinking can divert developers’ and business’ attention to get the job done. Yet on contrary, I have also seen teams going to yet another extreme and become very, very UI-driven — as contrast from having API written in stone, this pitfall is having its UI and all interaction fixed as mock-up before the app development. This happens more often with projects which are not driven by Enterprise IT, but by marketing departments</p><p>The problem with this mock-up, map-out-everything approach is self-explanatory once you start the development. It is usually straight-forward, though tedious, to get all the happy flow defined in clear mock up and railway chart. But as the saying goes, what could go wrong will go wrong — and mapping out all the exception flow is extremely challenging — and often counterproductive.</p><p>And what distinguishes an app from good and bad is often how it handles exception and recovers from errors. Happy flow is easy to visualise, and often makes app development looks trivial at some point — yet exception handling and error avoidance/recovery could often take 80%+ of time and efforts — and sometimes with your best, most experienced engineering resources.</p><p>Exceptions and errors are often used interchangeably in everyday conversation, but differs from each other in the software development world. In layman term, an exception is an expected situation in which the developer is supposed to handle and mitigate, whereas an error is an unexpected situation in which developers shall avoid — and when happening — shall recover if possible. Examples of exceptions are IO exceptions where the input keyboard, for example, does not support certain character sets, or permission exception where the phone user does not authorises certain actions; whereas examples of errors are out-of-memory errors where the app leaks memory such that the phone runs out of necessary memory to keep the app running, or segmentation faults where the memory of another process is wrongly written into.</p><p>It is almost impossible, and uttermost inefficient, to map out error/exception flow — for that the flow differs from OS to OS, from version to version, and from device to device. Chance is the environment, including network and the overall phone situation, may impact the occurrence, frequencies, extent, impact and manifest of the exception and error. Often it falls on development team, in particular mobile developers, to shoulder this burden — whereas development strictly on mock-up often focuses solely on tip of the iceberg, i.e., the happy flow — which is easiest to code and straightforward to test and deploy.</p><p>I have personally relied heavily on mock-up to size the project and plan the development in the past — and even nowadays it is a very good tool to think through the flow, visualise complexity, and communicate with stakeholders, to a certain extent, if properly used. Yet estimates based on mock up has lost much relevance on later stage of development, when we focuses on app quality, handle exception, and mitigate errors. It is especially frustrating, and counter productive, when we are at the stages of modifying the flows and “look and feel” of these mockups when we see ways to better implement the goal and get the job done. Here, if teams force itself strictly onto the mockup, teams become at least confused, and at most demoralised and frustrated, in getting around obstacles and delivering goals.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6150931aa974" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Mobile Pitfall #2: Retrospectively develop your app on a set of API written in stone]]></title>
            <link>https://medium.com/@andrewchan_73514/mobile-pitfall-2-retrospectively-develop-your-app-on-a-set-of-api-written-in-stone-d42f2eb3159f?source=rss-88dcb25323f6------2</link>
            <guid isPermaLink="false">https://medium.com/p/d42f2eb3159f</guid>
            <category><![CDATA[mobile-app-development]]></category>
            <dc:creator><![CDATA[Andrew Chan]]></dc:creator>
            <pubDate>Wed, 10 Jan 2018 03:27:30 GMT</pubDate>
            <atom:updated>2018-01-10T06:51:26.895Z</atom:updated>
            <content:encoded><![CDATA[<p>Waterfall or Agile, both models have its own legitimate place in the development world. And here I am not trying to answer or favour one over the other — as long as you get the jobs done, you shall care neither. I would like to focus more on a more platform-specific, technically driven mistake — retrospectively developing your app on a set of API written in stone.</p><p>In early days of mobile development, enterprise usually does not have an in-house managerial role for managing mobile projects, let alone its own mobile development team. Partially as a result of that, many projects which I have walked in as consultant are driven by good intention enterprise IT people, who have often prepared the full set of API to be “just rendered” on mobile.</p><p>Readers who followed my series of writing in mobile would know that this simply won’t work for a concurrent, performant app. But what’s worse, the API provided represents huge effort from a wide array of talented people involved in development and review, and often in enterprise setting, this means it is not supposed to be changed easily.</p><p>This leads to an awful, politically heated situation where the mobile team is trapped in the midst of a good-intention, diligent IT team who provided a set of API which is of limited use, and his/her enthusiastic sponsors and users on the business side of the enterprise dreaming up of all kinds of great functionality provided on the future app in a Facebook like experience.</p><p>For example, in a previous project, there was a customised newsfeed which was to be rendered per user. The business would like the newsfeed to be rendered based on the brands and products which the users have expressed the interest in, whereas the API is built and sealed like the newsfeed will only depend on the demographics data of the user. Obviously, in such case, there is a mismatch in what is provided on one hand, and what is to be achieved on the other hand. Reworking based on existing architecture can be frustrating and may not always be an trivial work — therefore I always recommend my client to develop the mobile end first.</p><p>The reverse of this pitfall, which is to develop the mobile end first, will make sure the users always solve their biggest problems, and finished their Job-to-be-Done (see Mobile Pattern chapters). This provides much more flexibility than coding against a provided API, for customers and users always long for a solution, while we engineers shall provide the data/resource/backend/API/schema/session/whatever needed. This also ensure the project sponsorship along even the hardest part along the development journey, because now a direct, causation link can be attained and perceived by the users and sponsors on the work done with the problems solved. In short, every dollar feels well spent.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d42f2eb3159f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>