For just over a year, I’ve been running the JTime project, which consists of a REST API, an Android app and a website, the latter two of which talk to the REST API. The JTime project allows me to see nearby prayer times. I don’t advertise it, as it is more of an educational project to try new technologies, and I just don’t want to be promoting it in case people think I’m clever for having made it (I don’t like showing off, and generally the people around me are non-programmers). So barely anyone uses it.
Initially, the REST API was written in Javascript, using the loopback framework which helped me to get moving fast and gave a good structure to the code. This api was run on Digital Ocean.
Fast-forward many months, and my DO free student credit was running out, so I looked for the cheapest place to host the api. After browsing around, NearlyFreeSpeech seemed like it could be the cheapest option. So I switched hosts.
After switching, I noticed that running the server was costing me more than I’d like. This was because the rest api consumed too much memory (~200MiB including a postgres instance), which cost me a bit too much money (NFS (NearlyFreeSpeech) charges directly based on CPU, RAM, disk and bandwidth usage). Loopback is a bit RAM-heavy, which I hadn’t had to think about before.
So, to save money, I decided to rewrite the api in Haskell.
I also considered:
I also decided to use Servant for routing, which in hindsight was a very good decision (IMO they’ve basically figured out how to define a rest api). If you haven’t heard of it, look into it. It’s really good. In a nutshell, you specify your API as a type. Servant then automagically does routing, serialisation/deserialisation and validation (ok, there is some stuff you need to do, but not too much).
As per usual, the rewrite took much longer than expected. And the haskell tooling itself let me down at times:
initINotify
kernel function thing would fail)) but it meant I could compile and deploy from Gitlab CI without jumping through cross-compilation hoops.Also, I underestimated just how lax loopback was regarding the input it would accept. Servant is very strict, which meant I had to add some code to deal with various types of input that loopback would have just happily accepted. I don’t see this as an issue with either library, more that they take different decisions about how strict to be about the kinds of input that are accepted.
Deploying the new server was a bit of fun. I planned and rehearsed the whole sequence, before switching off the old server, migrating the data across (using a Python script and sqlalchemy) and reconfiguring DNS entries (the new server was switched on beforehand to save time).
My main aim was to cut costs, which it did:
Category | March | April | May | June | July | August | Total | Average |
---|---|---|---|---|---|---|---|---|
Deposit Fee | $1.00 | $0.00 | $0.00 | $0.00 | $0.87 | $0.00 | $1.87 | $0.31 |
Resource Charge | $0.53 | $5.34 | $5.50 | $5.29 | $1.83 | $1.26 | $19.75 | $3.29 |
Site Charge | $0.03 | $0.30 | $0.31 | $0.35 | $0.35 | $0.28 | $1.62 | $0.27 |
Storage Charge | $0.02 | $0.08 | $0.09 | $0.08 | $0.13 | $0.12 | $0.52 | $0.09 |
Total | $1.58 | $5.72 | $5.90 | $5.72 | $3.18 | $1.66 | $23.76 | $3.96 |
Costs so far for NearlyFreeSpeech for 2017
Historical Resource Usage | |
---|---|
Month | RAUs |
2017-03 | 940.62 |
2017-04 | 9185.87 |
2017-05 | 9479.13 |
2017-06 | 9096.4 |
2017-07 | 1433.64 |
Resource usage for the old Javascript server
Historical Resource Usage | |
---|---|
Month | RAUs |
2017-06 | 25.86 |
2017-07 | 1708.9 |
This Month (so far) | 2214.54 |
Resource usage for the new Haskell server
Yeah, I know, the amount of money involved is peanuts, but I wanted to make this recurring bill as small as possible (and tbh, 4-5 cents a day sounds pretty decent to me). And I’m now down to ~57 Mib (including an nginx instance as I was having issues with serving static files with Haskell) although CPU usage is periodically spiking to 1%.
As with most rewrites, the new code is probably better structured. As it’s Haskell, it is less verbose. The Haskell code runs pretty fast too, but Phoenix, Arizona (where NFS have their servers) is an ocean away from me, so there is more latency than with the old server due to the speed of light.
I mentioned the cross-compilation issues above. Minus that, Haskell works well with NearlyFreeSpeech. Some issues/gotchas I have found with NearlyFreeSpeech have been:
Also, shoutout to Michael Snoyman. His blog post provided some encouragement in getting me to actually write this post.
30 Aug 2017 #Haskell #Javascript #JTime #Programming