Implementing match-making with Ruby on Rails

I’m using Ruby on Rails to build a real-time game web app. When a player
taps “Play Now”, I want to match them up with another player that is
looking for a game (match-making). With my basic understanding of RoR,
one immediate way I think I can do this is create a DB table that
contains the players that are looking for a game. When a new player
wants to play, I can just query the DB for the two most recent players
and connect them together, and remove them from the DB. But this doesn’t
sound optimal since the list of players can potentially be large and the
query could be slow.

Are there more efficient ways to implement this kind of match-making in
RoR? Or a more efficient way to implement a persistent queue?

Other ideas?

Thanks in advance for your wisdom!

I think this approach will be fine. The database is really good at
getting
you a single result quickly if that’s all you ask for (e.g.
PlayerQueue.where.not(player_id:
current_player_id).order(:created_at).first). Some psuedocode:

if the first player in the queue is still looking for a match
match current player with first player
else if the first player in the queue is disconnected
delete first record in queue
start over
else
insert the current player into the queue
end

Obviously, this will need to be restructured when translated to code,
but I
think you get the idea. There is a race condition here that only matters
are very small scale. You could also implement a worker that cleans up
the
table when a client is no longer connected. This table should really be
quite small, but with high turnover, and the database will do fine job
of
giving you a record quickly from it when using ORDER and LIMIT.

By the way, I would probably opt for “find me the oldest unmatched
player
that is still looking for a match” rather than the most recent one.
Searching for the most recent one treats the players that have been
looking
the longest as the least likely to receive a match.

On Sep 14, 2014, at 1:35 AM, Joe C. [email protected] wrote:

I’m using Ruby on Rails to build a real-time game web app. When a player
taps “Play Now”, I want to match them up with another player that is
looking for a game (match-making). With my basic understanding of RoR,
one immediate way I think I can do this is create a DB table that
contains the players that are looking for a game. When a new player
wants to play, I can just query the DB for the two most recent players
and connect them together, and remove them from the DB. But this doesn’t
sound optimal since the list of players can potentially be large and the
query could be slow.

Did you test that hypothesis? With proper indexing on the field you are
searching by there’s no reason to think that the query should be slow,
particularly if you put the LIMIT into the query. (Generally speaking,
object instantiation in Rails is relatively slow, but the actual
querying happens at the speed the database can handle it)

Are there more efficient ways to implement this kind of match-making in
RoR? Or a more efficient way to implement a persistent queue?

You could try Redis as a data store, but if I were you I’d just try it
with a normal SQL query and fix your ‘scale’ issues when you can
identify where the bottleneck is.