peck
General Strategies for Creating/Populating a SQLite Database as a static/read only asset
Hi everyone,
I’ve got a little hobby project (https://plaintexttides.com) that I’m working on and playing with sqlite a bit with it. I’m really only using a database to save myself some computation and API calls and speed up things, so I’m looking to use a sqlite database to essentially do that “work” ahead of time by bulk downloading the predictions at dev time vs runtime..
Essentially I’m bulk downloading and storing the tide predictions from NOAA for all the tide stations in the US for this calendar year. 4 tides, 365 days, and about 3300 stations. Not a huge amount of data, but instead of reaching out to NOAA for each time I have a user request to show the info, I have a little (~100Mb) sqlite database to query from using ecto.
I currently have it live in “priv/” and all seems to work nicely, but I don’t have a great strategy for how to “build” that database from the ground up. Using migrations, which is what I have now, doesn’t seem like quite like the “right” approach since I’ll never be doing it at runtime, but maybe it is.
Currently this is the only Repo in my app, but there’s a possibility that I could add a postgresql (or another sqlite) in the future so I want to keep options open, and since it’s a hobby project I don’t have to worry as much about YAGNI.
I was wondering if any of you had suggestions or approaches that you’ve had success with in building sqlite databases as something like static assets, maybe as a mix task or otherwise?
Thanks for any suggestions and thoughts.
Most Liked
evadne
axelson
I’d handle this with two parts:
- Use an in-memory write-through cache (such as cachex or nebulex) to to serve requests from memory and also write the results of the request to your sqlite db
- A GenServer that is part of your supervision tree that populates the sqlite db with all the results for this calendar year
Personally I like that better than a pure memory-only solution because when you restart the server you won’t need to make 3000 requests to NOAA’s API.
krasenyp
I would do what @axelson proposed but also think in a slightly different direction. If you can afford to use cached external resources, and if you expose an HTTP interface, why don’t you employ an HTTP cache, like Varnish. You can make use of cache-control and etag headers and lift the responsibility off your application.
Popular in Questions
Other popular topics
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
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









