knav_negi

knav_negi

Multiple Job acessessing database

We have three docker instances for our application and one MySQL database. We also have background jobs running in each instance which read from the database.
Having three instances mean, three background jobs reading from the database and if one job picks a row from the database, we want other jobs to skip it.

We tried to mitigate this issues with optimistic lock (using lock_version in ecto). we have a column as job_staus which is set once the job begins and if there is lock_version mismatch update would fail to indicate that some other job has already started for given row.

  row
      |> Ecto.Changeset.change(%{status: “job_started”})
      |> Ecto.Changeset.optimistic_lock(:lock_version)
      |> Repo.update!()
      |> start_job()
      |> update_status(%{status: “job_finished”})
    rescue
      Ecto.StaleEntryError ->
            #job was started in another process.
              :ignore
    end

Is there any better way of handling this ?

Most Liked

peerreynders

peerreynders

MySQL: locking reads

Given the above information within a short transaction I’d fetch a single job through a SELECT with a pessimistic lock (i.e. SELECT ... FOR UPDATE) and update it’s status.

You wouldn’t be having this problem if you could re-architecture the background processing - i.e. one process going through the table assigning work to available workers (i.e. get rid of the concurrency issue altogether).

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New

We're in Beta

About us Mission Statement