There were a couple of aspects of my previous experiments building networked ReactVR experiences with Redux that were unsatisfactory: there wasn’t a clean separation between the application logic and network code and, while the example exploited idempotency to reduce latency for some actions, actions which could generate conflicts used a conservative consistency mechanism which added at least a network round trip to those actions. So, I did some more hacking.

In an attempt to create a clean separation between application and network logic I kept the network code in a redux middleware and moved the application logic to an isValid callback which returns true if an action can be safely reduced:

With this in place the simple, conservative, dumbTerminalConsistency policy can be implemented in a few lines of code:

Clients generate actions in response to UI interactions and send those actions to the master. The master returns valid actions which are then reduced. Conflicts are resolved by serializing actions through the master client: isValid will return true for the first event, which will be distributed to all clients, but false for subsequent conflicting actions which are discarded. All clients see a single, consistent view at the cost of a round-trip to the master for all actions.

The same isValid method can be reused to implement an optimistic clientPredictionConsistency policy which treats the clients as decoupled simulations:

When using this middleware, clients immediately reduce actions which are valid given their local state and distribute local actions to all other clients. If a conflict is detected, the master client uses the same setState mechanism used to allow late joining to reset the decoupled simulations to the master state. The effects of actions are seen immediately at the cost of occasionally seeing the effects of actions roll back when the state is reset. By designing reducers to be idempotent and making the isValid callback as permissive as possible the number of state reset actions can be minimized. In the case of pairs, state resets only occur when two clients try to get the points for the same pair, or if a client tries to hide half of a pair which has been scored.

Testing pairs with the two different consistency policies over a high latency ngrok connection gives wildly different experiences. With dumbTerminalConsistency introducing 500ms round-trip latencies between clicking and seeing results the experience feels laggy, slow and clumsy. With clientPredictionConsistency the effects of local actions are seen with 0 latency and the experience is fast, snappy and frantic. Glitches caused by state resets are occasionally jarring, but often go unnoticed as the focus of attention is on the board during the game before switching to the scores at the end once they are eventually consistent. While it may make sense to use conservative consistency for some applications, pairs definitely benefits from an optimistic approach.

Being able to independently develop application logic and consistency mechanisms is extremely valuable. While developing the pairs example I was able to get the dumbTerminalConsistency middleware working, then the pairs game logic and then switch between the dumbTerminalConsistency and clientPredictionConsistency policies to determine whether I had a problem with the game logic or middleware while getting optimistic local updates working. I could imagine a similar approach being valuable for other applications. Conservative consistency could be used during development, then optimistic consistency policies could be experimented with to find the right trade off between latency and consistency without worrying about breaking the application logic by mixing in tightly coupled local update logic.

Its easy to imagine more sophisticated optimistic consistency mechanisms: middleware which generates anti-events to avoid full state resets when the state becomes too large, approaches which use Redux time travel approaches to rewrite history when conflicts are detected or policies which extrapolate predictions or interpolate corrections to avoid discontinuities for example. Many of these approaches could be implemented in generic ways, but developers would still have the option to build middleware which exploits application specific knowledge where appropriate.

The Redux approach of defining the next state as a function of an action applied to the current state lends itself to building sophisticated decoupled simulations. I hope to see these approaches become standard in networked ReactVR applications in the near future. Modern VR hardware provides incredibly low motion to photon latency and it would be a shame to see the sense of presence it can create broken by the network round-trips inherent in client-server architectures. Optimistic updates, client prediction and zero latency should be the default.

If you’d like to play the ReactVR version of pairs or see the rest of the code, it’s available on github here.

All code in this post is made available under the ReactVR examples license.


Generation JPod

Sat 03 June 2017 by Jim Purbrick

I’ve just got back from Kaş where I spent a lovely few days celebrating Pinar and Simon’s wedding and while there spent a few hours reading Now We Are 40: a thoughtful and entertaining look at everything from house music to house prices from the perspective of Generation ...

read more

2² Decades

Thu 20 April 2017 by Jim Purbrick

Several years ago when we were in 100 robots together, Max was celebrating his 40th birthday. When I said that mine would be in 2017, it felt like an impossibly far future date, but, after what feels like the blink of an eye, here we are.

Along with many other ...

read more

VR Redux

Wed 04 January 2017 by Jim Purbrick

Mike and I have been talking about how to easily build simple networked social applications with ReactVR for a while, so I spent some time hacking over the Christmas break to see if I could build a ReactVR version of the pairs game in Oculus Rooms. Pairs is simple and ...

read more

Creating A Safe Environment For People In VR

Mon 31 October 2016 by Jim Purbrick

I was very happy that Oculus found time at OC3 to host a panel on creating a safe environment for people in VR. As social VR becomes more popular over the next few years it will quickly have to learn how to keep people safe together in shared environments. Some ...

read more

crestexplorer

Sun 21 August 2016 by Jim Purbrick

At the 3rd Party Dev State of the Union at EVE Fanfest 2016 earlier this year, CCP FoxFour drew my attention to a limitation of the current approach used by crestmatic to generate CREST documentation: it only discovers resources always reachable from the API root from the perspective of the ...

read more

Strange Tales From Other Worlds

Tue 10 May 2016 by Jim Purbrick

At the end of last year, Michael Brunton-Spall and Jon Topper asked me if I would like to give the opening keynote at Scale Summit as I had “lots of experience scaling weird things”, by which they meant Second Life and EVE Online. I immediately thought of The Corn Field ...

read more

Towards A Generic Media Type System

Sun 17 April 2016 by Jim Purbrick

The early days of RESTful hypermedia API design tends to involve lots of homogeneous collections. In the case of CREST vnd.ccp.eve.Api-v1 pointed to the logged in vnd.ccp.eve.ccp.Capsuleer-v1 which pointed to a vnd.eve.ccp.CharacterCollection-v1 of contacts which pointed to many vnd.ccp ...

read more

#recordstoreday

Fri 15 April 2016 by Jim Purbrick

StoryBirdAlbum

3 weeks ago I spent a few hours with photoshop working on the Story Bird logo that Linda made a while ago to make it suitable for print. 2 weeks ago I spent a few hours researching the best way to convert the 24 bit 48 Khz Story Bird mixes ...

read more

#bandcampday

Sat 26 March 2016 by Jim Purbrick

Black barn mixing desk

I love record shops. Whenever I had pocket money it would go on Metallica and Nirvana CDs bought from Our Price or black t-shirts to match. When I lived in Nottingham I bought Boards Of Canada CDs from the same Selectadisc that my Dad bought a rare Fairport Convention single ...

read more

crestmatic

Sun 03 January 2016 by Jim Purbrick

A year ago I gave a talk at EVE Vegas about building RESTful CREST applications. My #1 recommendation was to specify representations in requests, but that’s hard to do when there is little documentation on which representations are available and what they contain.

Fortunately CREST is self describing: send ...

read more

Free Tests For Everyone!

Thu 11 June 2015 by Jim Purbrick

Modern software development is sometimes colourfully described as being similar to firing tracer bullets at a target. Rather than spending time doing a lot of research, design and specification up front, the smallest, simplest version of the software is built and the feedback gathered from its use is used to ...

read more

Investing In Testing

Wed 10 June 2015 by Jim Purbrick

Droidcon London

Last year I was talking to an engineer at Droidcon London who was working on an Android app with 100% test coverage. I immediately asked whether he thought 100% test coverage was worthwhile: many software engineering teams strive to achieve 100% test coverage, but few succeed because it’s an ...

read more

buckd

Mon 18 August 2014 by Jim Purbrick

BuckGraffiti

One of the things I’ve been working on since joining Facebook is Buck, an open source Android & Java build tool which is significantly faster than many other Java build tools for a number of reasons.

As well as being fast, Buck gains a lot of power and flexibility by ...

read more

Organisational Structures

Thu 20 March 2014 by Jim Purbrick

There have been a number of blog posts recently about exciting new organisational structures. As Cory points out “Every early stage company thinks it has reinvented management”: a very dangerous belief when betting on a new organisational structure can be much riskier than betting on the wrong product.

It starts ...

read more

Beyond Time Dilation?

Wed 29 January 2014 by Jim Purbrick

The Battle of B-R5RB

EVE online is a remarkable game. On Monday over 2000 people spent over 20 hours destroying virtual spaceships worth 200,000 USD in real money in what was the likely the largest battle in a video game ever. That EVE is capaple of supporting such large engagements is an amazing ...

read more

Osprey Therian

Sun 15 December 2013 by Jim Purbrick

In mid-2004 I first started exploring Second Life. Version 1.4 had just been released and Philip Rosedale had said in the press release “My fantasy is to be Uma Thurman in Kill Bill, and now I can. I’d pay $10 for her yellow jumpsuit and sword moves and ...

read more

Parse By The Sea

Sat 19 October 2013 by Jim Purbrick

#parsebythesea

A few weeks ago Facebook London hosted the Parse By The Sea hackathon at the Brighton Dome as part of the Brighton Digital Festival. The idea was to take one of our internal hackathons on the road and invite members of the public to join us, turning a hackathon in ...

read more

Facebook Hackathons

Mon 16 September 2013 by Jim Purbrick

I’ve been a big fan of hackathons since one of the first Yahoo! Hack Days I attended at Alexandra Palace was struck by lightning. The lightning caused the fire alarms to go off which opened the roof to let the torrential rain pour on to hundreds of geeks and ...

read more

Brighton Digital Festival

Wed 04 September 2013 by Jim Purbrick

The Brighton Digital Festival starts this week and I’m very happy to be helping out with Facebook London‘s contributions: Parse By The Sea, a mobile app Hackathon featuring Parse on the 26th of September, and helping to Connect The Brighton Digital Festival by sponsoring Metranet to provide high ...

read more
Fork me on GitHub