tirana
Let’s say:
- I have 1 millions of records in a DB and I want to process them daily via something like Oban.
- there’s an Oban job which is executed hourly
- an Oban job is able to process around 70k-200k hourly
- it can’t be known in advance how many it’ll process each tiime
The key here is to process the records evenly, that is, simply running one job per 24 hours to have it process all the 1 mln of records at once, one by one, won’t do for me.
How would I split the records into parts and also memorize how many of them have been processed so far, daily? Meaning, in an easy manner.
I could create a DB table with a counter → id, date_time, counter
But it appears to be an overkill.
Won’t there be a better way?
Trending in Questions
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
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
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
Other Trending Topics
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
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
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
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Latest Oban Threads
Latest on Elixir Forum
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Eiji
Then why not using something similar, but in memory like
:etstable?benwilson512
Great question! The basic principle we’re gonna use here is remainder math. At a basic level, you could spawn two jobs, one of which handled records that had even numbered ids, and another job that handled odd numbered ids. If you want to do more than just two jobs you can extend this concept by using the modulus operator
%.If you want to do this on a cron, my recommendation is to basically do a two phase job enqueuing process.
Phase 1: A regular Oban job that is configured to run hourly. That job spawns say 10 jobs to do the actual work, and it gives each job a number between 1 and 10 in the args, as well as the total number (for easier config)
Phase 2: The partition job runs, and it takes the arg given to it and queries your table of records.
And there you go! If a job is given say partition 7 then it will get all rows where
schema.id % 10 = 7which will be roughly 1 10th of the rows.The main upside to this approach is that you have a ton of control over the transactional characteristics because all of the relevant info ends up in the DB. It also handles node failure or cluster size changes reliably due to Oban unique jobs.
Eiji
Is it a good practice? I have no idea about
oban, but in worst case it could take even100%of rows as long as there is possibility to callRepo.delete/1onSomeSchemaand 90% rows are deleted. Of course nobody expects deletion of 90% rows in real case - I wrote that just to show the problem by providing an exaggerated example.What may be worth to mention is that all rows matching
schema.id % 10 = 7could be in worst case all first or all last rows, sosortcall with such code may have an unexpected behaviour.If possible I would advice to use
ROW_NUMBER()function which should solve be better solution forPostgreSQLdatabase. Regardless of how your data looks, how much is deleted and finally how you sort them it would always work exactly the same way. For more information please take a look at:dimitarvp
I’d probably go for a single Oban job that fetches records via
Repo.streamand then multiplex the record batches to other Oban jobs or just use good oldTask.async_streambut I admit I am not aware of the possible complexities or gotchas of using sub-workers and such.benwilson512
ROW_NUMBER() isn’t stable across different queries though. If a single row is deleted between process A doing the query and process B doing the query then the whole count is off and it won’t partition properly. The value of the modulus approach is that you’re using a value intrinsic to the row, which ensures that all parties can agree on which partition it belongs to just by looking at it knowing the partition counts.
I’m not really sure I follow your argument with respect to the modulus logic. It doesn’t really have an issue with deleted rows, unless for some reason all rows with a specific modulus value were specifically deleted, which seems hard to imagine in a regular use case. If you’re using auto incrementing IDs, then the probability distribution of modulus values is equal. So unless there is a bias in deletes towards specific modulus results I don’t really see what you’re saying.
Concrete example:
We generate a million auto incrementing keys, partition them modulo 10, and we see how many go to each partition. As expected it’s perfectly even. Let’s delete the first 90%:
Still even, as expected. Let’s delete a random 90%:
Still basically even. To get a non even result you have to have some sort of delete pattern that is more common in rows with ids ending with
9than rows ending with7and that just seems very rare to me, and is in any case easy to check in your own dataset to see if it’s happening.Eiji
Oh, did I missed that information somewhere? Could you please share more about it? It would be a good resource to learn.
benwilson512
From the link you provided:
The key words there are
result set. It’s the row_number within the set of things returned by a given query. Suppose there are 5 total rows with ids[1,2,3,4,5]. If Pid 1 queries all rows, sorts by id, and returns both the ID and row number it’s basically like doing:If ID number 2 is deleted between when the first pid runs this query, and the second pid runs this query, pid 2 will get a totally different row number for ids 3,4,5
Eiji
Ah, right - my bad. I was focused on count of rows in each group and therefore suggested a solution where said count is not different by more than one comparing to count in other groups. That part was good standalone, however I completely forgot that in such case we cannot accept changing count of rows in each group when one or more rows were deleted, because some rows who changed groups may not be processed at all.
tirana
Yes, it’s one of the solutions.
tirana
Ok. That would work.
However, what if
a) ID-s were not more or less sequential? So much so that that the ratio odd/even was 60%..40%? Or even 70%…30%?
b) ID-s weren’t integers but GUID-s?