A modern page view counter to see how unpopular my project is. Powered by a PRAwN stack (Postgres, React, AWs and Node) in the free tier, deployed using CDK.
Postgres.
React.
Aws.
Node.
We're also using Typescript and NextJS for the frontend.
We have a graph that shows the pageviews per hour and there's a few ways to make that happen.
Calculate the aggregates when they're requested. This is the easiest and will be up to date but it can be slow and resource intensive if it's requested often.
Setup a materialised view and refresh it on an interval. This is a natural extension of the first solution but will be much faster at the cost consistency because it might be stale. This also might be resource intensive if the table is big enough or the aggregates are complicated enough.
Iteratively calculate the aggregates and store them manually. The materialised view solution might recalculate aggregates that aren't going to change because they might be in the past. E.g the pageviews for last month aren't going to change to why recalculate them. This is the approach we've taken here.
I would recommend starting with the first one then escalating to more complicated solutions as required but I wanted to explore the third solution with this project.
I'm a serverless skeptic. A few timely things swayed me to try Lambda for this project.
serverless-express allows you to run an express app in Lambda, which means you can run the express part locally for a nice development experience.
I wanted to setup some cron jobs which are well suited to Lamdbda.
AWS CDK makes working with AWS ~~easy~~ less hard.
It turns out the simplest way to setup Lambda with RDS is quite expensive. You have to use a NAT Gateway which costs $0.059/h and $44.25/month. That's expensive!.
There's a few ways to get around this:
Make your database public. This isn't secure.
Make your Lambda private so it can't access the internet. The internet is useful though and you'll have to pay to access AWS services like Secret Manager.
With more load, our application can support a throughput of 570 requests per second on average or about 49.2 million per day at 350ms latency. 350ms is still pretty fast and if we had a higher tolerance for latency then this stack could probably do more.
The LAMP Stack (Linux, Apache, MySQL, PHP) is the inspiration for this project. I made a terrible chat app with it when I was in year 10 to get around the school's internet filter but it worked and it was really fun.