Programming

Serverless web applications: what they are and how to create them for free with Cloudflare Pages, D1 and R2

What are Cloudflare Pages, D1 and R2: services to develop, deploy and deliver serverless web applications.

The modern approach to cloud computing enables you to build, deploy and deliver web applications which do not require major investments in hardware and networking resources.

Web applications serverless they arise from the idea of ​​proceeding with their distribution without maintenance costs, keeping costs low and benefiting from immediate and high scalability.

In the last period Cloudflare built a cloud platform full-stackthat is capable of meeting all the needs of developers and able to support the distribution of serverless applications.

The added benefit is that Cloudflare services for building and delivering serverless applications have been built on the platform of edge computing of the US company. That is, servers are physically close to end-user devices that require access to data so web applications can enjoy it performance always at the highest level.

Instead of distributing resources from a specific region, the web application always serves its content from closest server to each user. The web developer no longer has to deal with dividing the distribution of his application across multiple regions or sizing resources manually to meet demand.

Cloudflare’s Journey to Building a cloud platform full-stack it started with Cloudflare Workers.

Unlike AWS Lambda e Google Cloud Functions running code inside dedicated containers, Cloudflare Workers loads the code in the form of JavaScript. The codice JavaScript it can be performed without resorting to containers, in a simple and fast way, moreover using the APIs Web Platform common to all modern browsers.

The use of standards-compliant APIs makes serverless functions completely portable: this means that the code can be easily transported and managed elsewhere without binding to a specific vendor. The Workers can also be hosted and run on their own, if desired.

Obviously this approach brings with it some disadvantages: Cloudflare Workers does not offer support for runtimes other than V8, the same engine that integrate for example Chromium and Chrome.

Therefore, support for languages ​​other than JavaScript and TypeScript is available only for code that can be transformed into JavaScript or compiled into WebAssembly.

The Workers also do not support Node.js (they are compatible with a small subset of NPM packages) and do not allow direct connections via TCP, for example to a relational database server. It is therefore necessary to send requests via a proxy HTTP.

Cloudflare has prepared an entire articulated offer that rests precisely on the Workers and which allows you to easily develop versatile applications without leaving the platform.

Cloudflare Pages

build aweb application from scratch it requires courage, creativity, passion and determination.

Cloudflare Pages is a platform that allows the code that the user publishes in his code to be exposed on the Web repository GitHub connected or hosting directly on Cloudflare systems.

Within seconds, Pages allows you to distribute the code and deliver the site through a global platform, such as that of Cloudflare, located in over 270 data centers worldwide.

This way, by relying on Cloudflare’s cloud stack, you can enjoy the same performance and reliability of a network through which some of the largest websites in the world manage their traffic. Not only. The good news is that Cloudflare places no limits on number of requests manageable or at bandwidth that each application can commit.

With Pages you can then create and manage the front-end of applications: Since Cloudflare announced integration with Workers in November 2021, the company’s developers have integrated the system with Remix, Sveltekit, Qwik and Next.js.

By default, projects delivered through Pages use a URL pages.dev provided by Cloudflare. However, it is possible to support any domain name customized by editing DNS records and turn on HTTPS support.

Plus Cloudflare’s Web Analytics system is ready to deliver traffic data in real time.

The Pages documentation is full of examples: for example, it explains how to create an HTML form and collect data in a Worker or how to create a blog and put it online.

Database serverless con Cloudflare D1

We have already said that a relevant limitation of the Cloudflare Workers consists in the impossibility of establishing direct connections via TCP. This reduces the freedom of action of any programmer who fails to connect a relationship database and, for example, extract the data you need or add the information received after completing an online form.

Cloudflare D1 is the service that fills this void: also based on the concept of edge computing, it is a relational database built on SQLite. Cloudflare offers replication capabilities and redundant backups with the ability to restore data in case of need. In addition, the read and write performance is the best possible, without maintenance costs and without having to configure servers for long-term management.

The database itself, being in SQLite format, is stored in a single file, easily portable and usable elsewhere without having to start particular conversion operations. It is therefore itself the first database serverless absolutely.

Cloudflare D1 is currently only accessible through Cloudflare Workers: enabling external access to the data held in D1 would require creating anPublic API.

The Northwind Traders sample application was developed by Cloudflare using the historical demo database that Microsoft first started shipping together with Access in 1997.

Anyone who has dealt with programming and RDBMS knows Northwinda database that is often used in many courses and examples.

Here, that historic database Northwindthanks to Cloudflare services and in particular D1, turns into an enterprise application, with a realistic schema and many foreign keys involving a wide range of tables.

The demo of Northwind Traders it also features a dashboard where you can find details and metrics about query SQL D1 which are gradually used “behind the scenes”.

Cloudflare explains how to create your first D1 database and connect it to a Worker.

Cloudflare R2 – Data storage managed by web applications

To close the circle, Cloudflare R2 offers developers support for storing data managed by their web applications.

With the R2 service, Cloudflare has decided to remove the exit fees or the costs that are applied when the data leaves the provider’s cloud network. In fact, vendors usually charge twice: when information is written, read and stored, but also whenever data is sent elsewhere.

Il piano free of R2 provides 10GB free of storage per month and 1,000,000 monthly operations on stored data.

Overall, R2 appears significantly cheaper than AWS S3 to manage those data that you have to use frequently and although it lacks some features compared to Amazon’s service, it is compatible with S3 APIs.

In recent weeks we have presented an example of a self-hosted CMS on Cloudflare to publish free content: the project microfeed it takes advantage of Pages, D1 and R2.

Obviously, the Cloudflare platform does not meet all possible use cases but it is rapidly evolving and instead of replicating the competitors’ offer, it has been designed to take charge of the most difficult aspects that the programmer encounters during the development of a web application.

Leave a Reply

Your email address will not be published. Required fields are marked *