Search code examples
javascriptnode.jsredisdatabase-performancenode-redis

Fastest Finger First App Architecture - Redis?


I'm writing a "fastest finger first" Node.JS app using smartphones as buzzers. I intend to use Socket.IO to handle the answers coming into the app but don't know what is the best way to store the data for comparison at the end of the round.

My initial thought is to insert answer data on every incoming socket event, into Redis as it is RAM-based and my assumption is that it would be fast. However, would it be a better idea to sequentially push each answer into an array? Would this persist during incoming socket events?

Hope that sounds concise!


Solution

  • You are correct that an array would be faster. It depends on how many users you plan on supporting and how you plan on scaling. If you are trying to make a "service" that anybody can create a "game" and you are planning on supporting hundreds of thousands of "games" on your platform, go with Redis because you may need to scale your web tier and buzzes on one web server won't show up on another web server.

    If, however, you are doing this as a one-off thing, for supporting one game at a time, possibly even from friends connecting to your laptop over wifi (although a small Heroku server over the internet would work fine too) then I'd go with an array. Communicating with an external DB (even one as quick as Redis) adds complications. Node.js is evented and single-threaded meaning you don't have to worry about race conditions. You could write

    var firstPresser;
    socket.on('press', function (presser) {
        if (!firstPresser) {
            firstPresser = presser;
            // return "You pressed first"
        } else {
            // return "You didn't press first"
        }
    });
    

    Forgive my pseudo code, I'm not familiar with socket.io specifically, but I think you get the point.