jaybe78

jaybe78

Hey,

I need help to find a scalable and efficient solution to:

  1. Store large number of elements (username)
  2. Table is not static => element gets removed from the table when process is demonitored
  3. Being able to return random elements efficiently

The context is that I’m storing some users based on criterias in different ETS tables and wants to be able to return those in my UI as part of a search.

First thing:

I’m dealing with username as keys, and at the same time, I think the easiest way to get random elements from an ETS table is to use sequential number as keys

i.e

:ets.lookup(tab, Enum.random(1..1000)) 

So I though about using 3 ETS tables:

  1. The first to store the username with corresponding index “usernames”
  2. The second to store all the user indexes “user_indexes”
  3. the last one to increment the total count of user added to get the next index “count”

Add user scenario

  1. Get the next index from “count”
  2. Add a new index in “user_indexes”
  3. Add corresponding username in “usernames” with that index
  4. Monitor the process

That’s my initial idea but the problem is that when users gets removed from the table, the indexes are not sequential anymore ({1..4,..6..400..})

In that situation the lookup function I use to get random elements would not return the expected results

:ets.lookup(tab, Enum.random(1..200)) 

There’s an other solution which is to work directly on the username keys

first = :ets.first(tab)
:ets.lookup(tab, first)
func = fn key->
    if function_that_may_return_true() do
        key = case :ets.next(tab, key) do
         :'$end_of_table' -> throw :reached_end_of_table
         key -> func.(key)
        end
    else 
        :ets.lookup(tab, key)
    end
end

Though that solution is not efficient for large tables.

At this point I’m not sure what I could do ?

Cheers

Showing Posts 1 to 10

tcoopman

tcoopman

Isn’t an ordered_set type of ets table good enough?
So have an ordered_set ets table, insert and remove usernames and randomly fetch an item based on the number of items you’ve got inserted?

jaybe78

jaybe78 OP

How would you fetch random keys in an ETS table whose keys you don’t know ?

The only way to do that to my knowledge is to use a combination of first and next to go through the table but that is not efficient.

Let’s say I have 1M in an ordered sets

How can I get stored usernames between 700_000 and 700_070 without going through all the usernames before ?

al2o3cr

al2o3cr

One way to deal with those gaps would be to “draw again” when you get an unoccupied integer.

The efficiency of that approach is going to depend on how often elements get deleted; lots of churn will make for lots of retries. How common are deletes in your application?

jaybe78

jaybe78 OP

By “draw again”, you mean re populate all those data in the table every time to fill those gaps ?

I’d say on average an user would appear in those ETS table less than a minute or so.
So those delete will be frequent, yes.

derek-zhou

derek-zhou

Assuming you have an ordered_set with integer key, You can do:

:ets.next(tab, Enum.random(1..1000))

If it returns end of table, do again, or do first.

al2o3cr

al2o3cr

I meant “draw” in the lottery sense:

  • get an index with Enum.random(1..current_max)
  • fetch the element with that key
  • if no element, try again

This would work fine for systems with infrequent deletions, since most of the time the first lookup succeeds.

It would be extremely BAD for a high-churn system, since current_max would be increasing constantly but the table is mostly unoccupied. The average runtime of a “pick a random user” would just get worse and worse…

jaybe78

jaybe78 OP

That would not solve the current_max increasing constantly while the table is mostly unoccupied as pointed out by @al2o3cr

LostKobrakai

LostKobrakai

What’s the usecase here? What do you need to fetch random users out of a huge ets table for?

jaybe78

jaybe78 OP

I’m building an app where users can search :

  1. Online users
  2. that play to certain video games
  3. Can challenge them on that game

When an user search for players, my goal is to return a list of users that is as random as possible for each search.
Because if same users are returned in search result too many times, the same users would be challenged, that would create spam, while others would be left out.

The end goal is to really create couple(1vs1) as fast as possible.

LostKobrakai

LostKobrakai

For matchmaking wouldn’t you want to prefer people having waited longer over people just having joined the queue? Also would there really be a million people waiting in the queue or rather be a million people playing? If you form couples quickly enought the queue might stay at a reasonable size.

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
mnkhod
So i have been using ash framework for a while and i love it. However currently the issue im having with ash framework is the error handl...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews