dcrck

dcrck

Persistent real-time statistics for time series data

I’m working on a high-availability real-time application. Currently, it’s composed of a number of “bots”, each of which listens on WebSocket endpoints for data, responds to the data in various ways, and stores important events in CSV files. The system works great so far, and is able to run all day without problems.

I’d like to add the ability to compute statistics for these events, as they come in, to determine the performance of each “bot”.

I also have another, totally unrelated Phoenix web application which relies on very similar statistics. It stores the time series events in a Postgres database and re-calculates the needed statistics whenever someone visits a page. There aren’t any performance issues there – yet.

Ideally I’d like both of these applications to rely on the same solution. I don’t believe the re-calculation is the most appropriate solution for the real-time app, though…please correct me if I’m wrong!

I’ve read through the post on Creating Persistent Real-Time Analytics of Time Series Data, but that post is a few years old, plus my use case is much less intense – for now.

Here are the important data points for the real-time app:

  • fewer than 100 events per day
  • fewer than 20 bots running at a given time
  • the calculations themselves aren’t computationally intense (for now)

Here are the features I’d like the statistics-tracking application to have:

  • persistent: if the system crashes, I should be able to load in the last known stats or compute the stats from events generated so far.
  • lightweight: I’d like this to be easy to include in other projects that deal with similar data, so I’d like to keep dependencies to a minimum. I’m not opposed to using a database, though, if it proves to be the most appropriate solution.
  • configurable: I’d like it to be relatively easy to support new statistics for the system as needed. I’d also like to be able to choose a subset of the statistics to track if I want.

Right now, my idea is to use a Supervisor with an Agent for each statistic I want to calculate and track. I’m a bit lost when it comes to the persistence part, though. I’m also new to time-series calculations and tools in general.

Do you have any suggestions for libraries or built-in tools I can use to solve my problem without too much overkill? Am I overthinking this? Thanks in advance!

Marked As Solved

lucaong

lucaong

I don’t know enough about your case to determine whether you are looking for a centralized store like Postgres (where multiple app instances would use the same database) or a local one (each app instance having its own storage). If you need a centralized one, you can discard the rest of my post :slight_smile:

If you are looking for a local storage, you could check out CubDB (disclaimer: I am the author of the library). It is an embedded persistent storage written in Elixir, supporting key/value access and sorted ranges, kind of like a durable Map where you can select range of entries sorted by key. Storing time series and selecting ranges is quite easy and performant, you could follow the approach explained here but using timestamps instead of integer IDs):

{:ok, db} = CubDB.start_link(data_dir: "some/dir")

# Save a measurement (temperature for the
# sake of the example):
key = {:temperature, :os.system_time(:millisecond)}
value = measure_temperatue()
:ok = CubDB.put(key, value)

# Get measurements for a time range:
start_time = ...
end_time = ...
{:ok, measurements} = CubDB.select(db,
  min_key: {:temperature, start_time},
  max_key: {:temperature, end_time}
)

Of course, there are several other possible solutions out there, like DETS (but it does not support sorted collections, so it makes the time series case tricky), Mnesia, or embedded databases like LevelDB, LMDB, SQLite, etc. Here you can find a quick comparison with CubDB. I think CubDB would be a good choice if you are looking for something lightweight and idiomatic from Elixir.

Also Liked

hauleth

hauleth

As you are already using PostgreSQL then maybe TimescaleDB will be something that will fit your needs (it is PostgreSQL extension).

dcrck

dcrck

Thanks for the suggestions. CubDB is pretty close to what I was looking for. I may decide to move to something more centralized like Timescale with Postgres at some point, but this should work for now. Thanks @lucaong!

Where Next?

Popular in Questions Top

JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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
pgiesin
This should be a simple problem but I just can’t seem to figure it out. I have a standalone Elixir app that won’t find the database. Dep...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
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
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
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
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New

We're in Beta

About us Mission Statement