>_
Dating App theory part 4 - Cloudflare
This is the forth part in a random series Im writing about a dating app I would build (but Im not going to). Previously we looked at the
rules and requirements of the
app, then a look at the VPS solutions we could
use. In this part I shall look at the architecture and cost of building the dating app with Cloudflare.
Cloudflare is a provider of a global cloud platform, they begun with spam email and then moved into offering a large range of network products, including a developer platform. Their application service offers a firewall and CDN service which I have used before. In fact the Pro service at $20 per month would be a good consideration for this project, if not the free tier.
Anyway, we have an idea of our requirements, lets look at how we could build a solution using Cloudflare's developer platform.
Workers and KV
Workers is Cloudflares compute offering. While I have ignored free tiers previously, I would mention here that 100,000 requests per day are free with Workers and I would include this in estimations because I do believe that this is something Cloudflare will always keep free. Meanwhile the Pro tier has a minimum charge of $5 with every extra million requests costing 15 cents. This Pro tier also includes longer request times. We also have to pay for memory per request, so lets assume each of our requests averages 3 seconds. So our workers pricing would look something like this:
- (200K requests per day - 100K free) = 3million requests per month - 1 million free = $0.30
- (128Mb x 3 x 3 million) = 1152000000 / 1000 = 1152000 GB - 400,000 (free) = 752000 = $12.50
Not sure if I got lost in the maths there, but it looks like we would need to pay for one extra block of million GB-s and then a small amount of extra requests. The other thing to mention here is testing/development so these costs may well go up. However as mentioned before this is with maximum usage of the app in a month, so costs would most likely be lower even with development and testing.
For data storage CF provides Workers KV. This is a managed key value store that also has some free usage. In this case you have to pay for reads and writes every month. So here with our 200Mb of data and 200,000 requests per day, lets say that half of those requests involve our database and then 75% of them are reads. In this case lets look at cost:
- Storage: 200Mb - 1GB free = Free
- KV reads: (75,000 per day x 30) = 2250000 per month - 10M free = Free
- KV writes: (25,000 per day x 30) = 750000 per month - 1M free = Free
- KV other: 1M delete free, 1M list free
So here this looks free, the only thing I will query is the list requests. A request unit is classed as 4KB, which I dont think would be much of an issue with reads and writes however a list request depends on the amount of data that has been analysed. So our 1 million list requests would allow us to list 4GB of data. In our case, that means listing all of our data (200Mb) 20 times. I dont think we would need to do this though so it shouldnt be a concern, plus we have 7 million read requests still free anyway for this.
Images
Serving images is another requirement for us and for this CF provides Images. Images charges by the images served and images that are resized. The only thing I would say on that is that we could resize images that are a bit too large, but you get 50,000 a month so if we resized all of our user's images (3 x 10,000) we wouldnt hit this limit anyway. Charging by delivery per image, in our case we mentioned in part 2 that we expected a user to look at their matches daily and each of these would be 3 images each, so at a maximum 9 images per user per day multipled by 10,000. This gives us:
- Stored: (3 x 10,000) = 30,000 = $5
- Deliver: (9 x 10,000 x 30) = 2700000 / 100,000 = 27 x $1 = $27
Total: $32
Okay so if we had every user look at the three profiles they had available every day then we would be paying $27 a month to serve those images. The only defence I have here is that Cloudflare maybe aim this more at companies that want to serve large images, whereas that is not necessarily the case for us.
After further research I think it would be more reasonable to consider using
R2 over Images, as I cant see a reason to not use it. R2 requests are priced differently for create (class A) and reads (class B) while deletes are free. Pricing this up:
- Storage: 200Mb - 10GB free = Free
- Creates: 1,000,000 free = Free
- Reads: 10,000,000 free = Free
The deletes are free. Because its so high for free requests, theres no way we would use them all. For example, we have 30,000 photos at the maximum usage. If these all were viewed every day for a month this would be 900,000 requests. I cant see this happening anyway, never mind getting 10x this number of requests in a month. So this option is obviously a lot better than the Images option. The only query I have with this is if it definitely can be used for images and in the way we want to.
Cron
Another issue we have is scheduling our match task every day. For this Cloudflare has Cron
Triggers. This does not cost any extra, but we should consider the compute we would need to run our matching each day. We would need to split each of the Worker calls we make to fit within the CPU limits for each, then we would also need to consider the price of extra reads and writes to Workers KV. Previously I estimated a 10 minute call per day for the matching task, but its hard in this case to split this up, so it may be easier to say we make 3 seconds of compute per user, per day. Then lets also say that for each user we make about 5 reads and 3 writes to help find their new matches. These costs would come to:
- KV reads: (50,000 * 30) = Free
- KV writes: (30,000 * 30) = Free
- Workers Requests (10,000 * 30) = 300,000 = $0.15 (per mil)
- Workers GB: (10000 x 3 seconds x 128MB x 30 days) = 112500 GB-s = Free
Total: $0.15
With our Workers costs Ive kept the memory as free because I think the amount we need would keep within the first million that we have for compute above. But ofcourse there would be a risk of going over. Ive also put free for the KV reads and writes as they look to fall within the free tier too. So overall alot cheaper than expected, however there would be some complexity in getting this task to work per user perfectly.
Totals
Pages is a service CF offers to serve a frontend application. This is free for our requirements as we would not need to pay the extra $20 per month for more builds as 500 per month should be enough.
Here is a rundown of our total monthly costs.
- Workers: $12.80
- KV : Free
- R2: Free
- Cron: $0.15
- Pages: Free
Total: $12.80
Its worth noting here that because of the request based pricing there is always a risk of these prices increasing. Especially as mentioned before with Workers memory which we would borderline to paying an extra $12.50 a month for. I think this is the biggest downside even though the price is very reasonable. Say we have an error in code or a large amount of spam, this price is never a complete guarantee.
Another issue here would be skillset, it is recommended to use Typescript with CF Workers. Its doable and Im sure it would not be a problem, but learning a new language/framework would slow down development.
After looking at pricing even at this scale, this does look like a valuable option. But the downsides to this setup are that the price can change month to month and a lack of flexibility on the programming language. But these conclusions dont stop me from wanting to build solutions with Cloudflare, as their products do look very reasonable.
Next we shall look at AWS.
11 Nov 2022, 12:17 p.m.
Home