Rich, Real-time Mobile User Experiences @Devoxx UK
My presentation at Devoxx UK on some ways to increase interactivty in mobile applications. Many of these techniques are embodied in Diffusion, Push Technology's premier data-distribution engine.
2. @andyp1per#RealTimeMobile
Delivering Rich, Real-Time Mobile User
Experiences
• Dr Andy Piper
• CTO at Push Technology
• Ex-BEA/Oracle
• Architect for WebLogic Server Core
• Architect and then Engineering Director for Oracle Event Processing
• Spring contributor (Spring DM) and Author
• Contributed to many standards – OMG, JCP, OSGi Alliance
• PhD, Cambridge, Distributed Systems
• MBA, Warwick Business School
3. @andyp1per#RealTimeMobile
What we do
Software that enables rich real-time user experiences,
where the Right Content is delivered to the Right User,
at the Right Time, on any device, platform or application,
regardless of connectivity or location.
5. @andyp1per#RealTimeMobile
The problem with mobile
10Gbps pipe, reliable 99.9999%
Dedicated Network
2-50Mbps pipe
Internet
Unreliable speed
0.5-4Mbps pipe
Mobile Network
Unreliable speed latency
8. @andyp1per#RealTimeMobile
Why does it work?
• We want UIs that engage us
• Immediacy is key
• Interactivity is key
• Response is based on rapid, high fidelity inbound and outbound data flows
• We want Reactive applications!
10. @andyp1per#RealTimeMobile
It’s all about the Network
• Interactive applications interact with something
• Its not all angry birds
• The quality of the interaction depends on connectivity and latency
• That data gets through
• The time it takes
• The time to process
• That it returns
• The time to return
11. @andyp1per#RealTimeMobile
It’s all about the Data
• The network is a precious, uncontrolled resource
• What you can control is how much you use
• How much you use depends on
• How much data you transmit
• How frequently you transmit it
• You may be able to cheat!
• Only deal with what you can see
• Data often has a time dimension
• The data value decay curve
• Its not just about what you can see
• But WHEN you can see it
Value
Time
12. @andyp1per#RealTimeMobile
It’s all about the User Experience
• Go Reactive!
• http://www.reactivemanifesto.org/
• Event-driven, scalable, resilient, responsive
• Latency over mobile networks (and the internet) is inevitable
• Asynchronous models hide latency
• Asynchronous models are better overall
• Although harder to implement
13. @andyp1per#RealTimeMobile
Introducing Velocity Voting – “Vloting™”
• Stream of control data from device
• Stream of feedback data from server
• Server calculates “velocity” of votes and aggregates results
• Aggregate results are pushed back to devices
15. @andyp1per#RealTimeMobile
Reactive UI frameworks
• Vue - http://vuejs.org/
• Component hierarchy communicates via events
• Data model declared as POJOs (JavaScript)
• Vue manages data model changes
• Async DOM updates
• Smart batching and compression
• Also considered
• React - http://facebook.github.io/react/
• Om - https://github.com/swannodette/om
17. @andyp1per#RealTimeMobile
Key Features of the Vloting App
• Built reactively on an asynchronous, event-driven model
• Latency is obscured by asynchronicity
• Consider a plane
• Highly interactive both individually and in aggregate
• Real-time data flowing in both directions
• Asynchronous is key
23. @andyp1per#RealTimeMobile
Optimize the data?
• Small is beautiful
• Prefer compact binary protocols
• HTTP not great (~300+ bytes/message), XML awful
• STOMP – ~100+ bytes/message
• COAP – ~4+ bytes/message
• MQTT – ~10+ bytes/message
• Binary protocols also happen to be faster – scale better
• Serialization is the killer – both for performance and the battery
• Protobuf, SBE, dpt
• Chatty protocols also poor - setup costs can be high
• Burst data in large chunks - reduces TCP/IP header cost
24. @andyp1per#RealTimeMobile
Making data smaller
• Information dense
• Don’t send the same information more than once
• Only send differences – snapshot-delta
• Minimum viable encoding
• Interoperability should not be a concern
• Mobile applications are an extension of the server
• Controlling both ends is important
• Coordinate optimizations
27. @andyp1per#RealTimeMobile
Making data smaller
• Send less
• Just because you have data doesn’t mean you should send it
• Just because you have bandwidth doesn’t mean you should use it
• Throttle based on ability to consume
• Both device and human
• Throttling reduces data costs
• Throttling conserves bandwidth
• Being reactive means only sending data when you need to
• Much more efficient
28. @andyp1per#RealTimeMobile
What happens when the network lets you down?
• It WILL let you down
• Insufficient bandwidth
• Inconsistent bandwidth
• High latency
• Loss of network on a regular basis
• You can’t optimize but you can respond
• Go Reactive!
• You must respond to the conditions of the network
• “Mobile sympathy™”
29. @andyp1per#RealTimeMobile
Responding to the network
• You must get feedback – but how?
• Instrumentation is key
• What, when, how much?
• Use any and every means to understand the network conditions
• TCP is your friend!
• Reliable in the face of congestion and packet loss
• Fast connection loss detection
• Back pressure is informative
30. @andyp1per#RealTimeMobile
Responding to feedback
• Need in-memory working set
• Queue / buffer on the server
• Availability of the data may not match the network availability
• Fill the queue at the speed of data availability
• Drain the queue at the speed of the network
• Queuing also copes nicely with network loss – replay queue at reconnection
• Queuing has to be per-client – not all connections created equal
• Maps well to a lock-free architecture
• You need a really good queue!
• Disruptor - http://lmax-exchange.github.io/disruptor/
• JCTools - https://github.com/JCTools/JCTools
32. @andyp1per#RealTimeMobile
Optimizing queuing - memory
• Store per-client references not messages
• Share messages as much as possible
• Garbage collection remove when all references expired
• Sharing references also reduces copying
• Memory churn can be a huge problem in high velocity systems
• Vital that messages are immutable – otherwise you have to mediate access
• Locking will kill you
33. @andyp1per#RealTimeMobile
Optimizing queuing – timeliness
• What do you do about stale data?
• Revolves around whether it is important
• If every message counts then you have no choice but to deliver
• Snapshot-delta means final state is dependent on initial conditions and
changes
• But changes != All deltas
• S + D1 + D2 – D1 = S + D2
• Why send D1 – D1 as it contributes nothing?
• Delete messages that contribute nothing
• Remove duplicates
• Merge multiple deltas into single delta
• Conflation
37. @andyp1per#RealTimeMobile
Rich to richer User Experiences
• Real-time allows user experiences that would not otherwise be possible
• Truly rich experiences are contextual
• Contextual data often originates from the device itself
• Location
• Movement
• etc.
• Contextual information often requires access to the native capabilities of the
device
• Hard to access from JavaScript
• Apache Cordova is a framework that gives JavaScript access to native
capabilities
38. @andyp1per#RealTimeMobile
Apache Cordova
• Develop html/css/js apps for mobile platforms
• https://cordova.apache.org
• Run as a native (hybrid) app
• iOS, Android, Windows Phone, Blackberry, and more
• Native Web View displays the html/css/js code
• Interface with native components
• Accelerometer, Compass, GPS, etc.
• Simple command line interface
• Run on device (even iOS!) from a single command
• cordova run —device [ios|android|…]
• Largely for testing
• Deployment/packaging from command line
• Easy to use external js libraries (like Diffusion JS Client)
41. @andyp1per#RealTimeMobile
Other considerations
• Like the network, device capabilities are not something necessarily to be
accessed
• Even though you can
• Battery-life is a killer
• Only want to access information you need at the fidelity you need it
• High precision location information is power intensive
• Throttling and “good enough” metrics are key
43. @andyp1per#RealTimeMobile
Conclusion
• Rich, real-time experiences can be provided on mobile platforms through
• Data optimization
• Network optimization
• Reactive interfaces
• Contextualization
• Good experiences are dictated as much by what you don’t do as what you do
do
• Products and frameworks can take the pain out of development
• Cordova
• Diffusion
We can see the effects of sending the full data versus sending a snapshot and deltas in these animations. Here’s the full data going through every time.
And now here’s the same thing with using snapshot and deltas.
As you can see substantial bandwidth savings can be made.
So the network is slow or non-existent – now what?