antoine
Resilient and battle tested event store with PostgreSQL
- We need to store a lot of events in a PostgreSQL database.
We can be spikes sometimes, so the rate of inserts can overload the database (2000/sec).
Those inserts are made in many places in our codebase, during important tasks, so an insert request should not be blocking.
I mean an EventStore.add(...) should not hang and lead to a Genserver timeout on the caller side.
defmodule Work do
use GenServer
def handle_call(...) do
...
EventStore.add(...)
...
end
end
Of course, we can not afford losing inserts, but we can trade by having a delay (being not realtime).
According to you, what is the best architecture for this?
- We also need to handle the case of the database crashing or not responding. Maybe by enqueuing inserts in RAM/disk and dequeue when database in up again.
According to you, what is the best solution for this?
Trending in Questions
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Hello,
I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New
Other Trending Topics
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #performance










First 7 of 7 Posts
Fl4m3Ph03n1x
Communication with DB
Batching
I understand that your main objective is not to lose information. So, IMO, the best way to do this is do send the information to the DB in batches. You would have a queue that would save data in RAM and every X seconds, write it to the DB.
This approach has the benefit that it will be harder to overload the DB with a billion petitions per minute, but it means that if your machine crashes, you lose the data in the queue (because it is in RAM). A possible solution would be to use DETS for this (write changes to disk) and clean the DETS table once the writes are done.
Pooling
Another approach would be to have a pool of workers which can write into the DB. If a process wants to write and there are no workers, it has to wait. The danger here is: what happens if I can’t wait indefnetly? That depends on your application.
Personal opinion
I would likely use a combination of the previous 2 approaches, having an infinite wait time for the process needing a worker to write to the DB but making sure that processes waiting are added to a queue. HTTPoison, an HTTP library, uses this algorithm with hackney.
What if the DB crashes?
DB crashes are hell. The only way to make sure your data is safe is to store it into disk. However, the longer the DB is down, the less space on your production machines you will have, which means they will eventually break down due to lack of space (it also happens with RAM). To avoid this I recommend a cleaner process, that deletes data older than X (seconds/minutes/etc) that runs permanently to clean your DETS, or files.
Other than that there is really nothing you can do, unless you are willing to streamline data into your DB via queues (Apache Kafka, RabbitMQ, etc).
Hope it helps!
Ciboulette
Interesting @Fl4m3Ph03n1x!
If I’m not wrong, when I read the question, it’s quite clear that the insertion in DB should not block the caller process. So I am not sure your pooling solution with the indefinitely wait will work.
I like the Batching solution! What about using GenStage beside building a pool of GenServer for building this feature?
slashdotdash
Have you considered using a purpose built event store, such as Greg Young’s Event Store?
I’ve written an Elixir Event Store using Postgres for persistence. The performance when running the benchmark suite on my laptop is 4,929 events/sec for a single writer and 8,586 events/sec for 50 concurrent writers.
To persist events without blocking you could do:
Alternatively you could send the
GenServerprocess a message to store the events outside of the request:You could even use an approach with two
GenServers. One to accept requests (e.g. store these events) which forwards the request to a second process to actually persist them to the event store. This allows the firstGenServerto immediately reply without any blocking as it is offloading the work to another process.How do you deal with storing events when the database is inaccessible? You could push them onto a queue and have one or more consumers taking events from the queue and writing them to the event store. However the same problem applies when the queue is unavailable.
Ciboulette
Interesting!
By using your implementation with:
You will inevitably spawn thousands/ hundred thousand of processes depending the number of caller processes. I think the second one is more controllable! WDYT?
alvises
If you can’t loose any event (but you don’t need realtime) and the spikes can cause timeouts, I would put something like RabbitMQ or Kafka between the application and the database.
app– event → kafka/rabbit —>event importer→ postgresRabbit/Kafka should handle much better the spikes and they could act as a durable buffer. If the importer (which takes the events from rabbit/kafka and stores them into postgres) has any timeout and crashes, or even if postgres locks, the event is not lost and will be reprocessed once the importer or postgres is healthy again.
slashdotdash
Depends whether it’s better to have thousands of processes doing one thing, or one process with a mailbox containing thousands of messages. The only way to reliably answer that question is to try both and benchmark the performance.
Fl4m3Ph03n1x
Great observation. In our production code we actually go around this issue by making our main process create a child that does all the work:
We use
Task.startbecause of two reasons:Your millage may vary
As for GenStage I would probably discard it and move directly to Flow which has a backpressure mechanism integrated to make sure no one is getting overwhelmed and uses GenStage on the background. Basically it would keep you from re-inventing the wheel.
Yes. And that is not an issue. You mentioned you do up to 2000 ops/sec. Assuming that a single DB write takes 3 seconds to save (which is a long time) you would have a top of 6000 Tasks active at any given second. Hell, let’s go crazy and say that thanks to some overhead you would have 10_000 Tasks (let’s say the cost of creating and killing Tasks is high). At this rate, you would be using less than 7% of the total number of process you can create with Elixir (and I am being evil, because the numbers would be lower iirc).
Not a problem basically, the way I see it.