Micro Frontends, Role-Based Applications, and You
Tags: design, devops, programming
Published: 2020-09-16 13:31:15

---
title: Micro Frontends, Role-Based Applications, and You
published: true
description: Everyone has heard of microservices, but nobody talks about micro frontends. Discover how to incorporate a secure, highly iterable UI into your next app.
tags: 
  - design
  - devops
  - programming
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/29yfzkoik1wrcdstulae.jpg
canonical_url: https://www.readysetcloud.io/blog/allen.helton/micro-frontends-role-based-applications-and-you/
---

Microservices are great. Single-responsibility, domain specific APIs that are independently iterable. A major web application can be composed of tens to hundreds to even thousands of these little guys.

They can scale independently, give app teams the ability to code in [the most appropriate language](https://www.readysetcloud.io/blog/allen.helton/how-to-choose-the-best-programming-language-for-your-new-project-d72b2e586aac/) to get the job done, and leave you with a small blast radius if something fails (meaning the entire app stays up if a single microservice fails).

All the big guns are doing it, too. Uber has [~2,200 microservices](https://eng.uber.com/microservice-architecture/), Netflix has [around 700](https://medium.com/refraction-tech-everything/how-netflix-works-the-hugely-simplified-complex-stuff-that-happens-every-time-you-hit-play-3a40c9be254b/), and Amazon... well, Amazon has a lot.

But did you know you can apply the same basic principles to the front end? *Micro* doesn't have to apply only to the backend APIs.

## What is a Micro Frontend?
Micro frontends follow the same concepts and offer the same benefits as microservices. It takes your big UI monolith and slices it up into small, isolated, independently iterable chunks.

![Micro frontend architecture](https://readysetcloud.s3.amazonaws.com/microfrontend_1.png)
*Simple micro frontend architecture*

After the initial build, chances are high that your UI is going to iterate more often than your back end. Be it big or small, you'll go through more cycles making the user experience the best you possibly can.

As you should!

So doesn't it make sense to divide up your front end into bite sized chunks? **YES!**

Now I don't want you thinking that building your application with micro frontends will make the user experience feel disjointed. That is not the goal. That's an anti-goal. You want your app to continue to feel cohesive, while quickly iterating on small features.

If you're thinking about getting into [continuous delivery](https://www.readysetcloud.io/blog/allen.helton/are-you-really-ready-for-ci-cd/), I would highly recommend considering also getting into micro frontends. Delivering small, incremental updates is exactly what this architecture is about, and with micro frontends you're enabling yourself to do so.

## Role-Based Applications
If you decide to take the leap into micro frontends, **congratulations**! You've also opened yourself up to a pattern that will save you lots of headaches in the future: *role-based applications*.

What is a role-based application, you ask?

> A role-based application is a system composed of micro frontends and APIs with specific security restrictions.

Let's take an example.

You are the creator of a forum for celery enthusiasts, **The Stalk Stalker**. The architecture diagram for the forum looks like this:

![The Stalk Stalker architecture diagram](https://readysetcloud.s3.amazonaws.com/microfrontend_2.png)

In your forum, you have users with different sets of defined permissions. Each set of defined permissions is known as a *role*. You have four roles in *The Stalk Stalker*:

* **User** - view and create posts
* **Moderator** - view, edit, create, and delete posts
* **Admin** - view, edit, create, and delete posts, and manage moderators
* **Banned** - nothing

Each person who signs into your app is associated with a role. In your three micro frontends, you have clear boundaries on who can do what. So you set a restriction on each of your services to limit the scope down to specific roles.

![The Stalk Stalker architecture diagram with roles](https://readysetcloud.s3.amazonaws.com/microfrontend_3.png)

Anyone with the appropriate role can access that part of the application. More importantly, anyone **without** the allowed role is explicitly *denied* access to the micro frontend.

If your API is public, [which it totally should be](https://www.readysetcloud.io/blog/allen.helton/you-should-open-up-that-api-youve-been-working-on-b0313a4df9bc/), these permissions must also be enforced on the API.

## Securing Micro Frontends
There are many different ways to add security to a web application and supporting APIs. With role-based apps, you must include the role somewhere in the auth. Be it a separate header or a behind-the-scenes lookup, *you must validate the user has permission to do what they are trying to do*.

In your micro frontend, you need to validate the user has the role tied to that app before any of the content renders. The app is small and focused, so it's perfectly ok to [hardcode the allowed role](https://www.readysetcloud.io/blog/allen.helton/new-project-just-hardcode-it-bac72e1a231e/) in the source code. The flow could be as simple as:

**Login > Lookup user role > User has role? > Unauthorized/OK**

The API  will do something similar, except it shouldn't be tied to a login event. The user will already be logged in by the time any API calls are made. So be sure to have identifying information about the user in the authorization token. Something like [OAuth 2.0](https://oauth.net/2/) provides the context necessary to uniquely identify a user via a [JWT (JSON Web Token)](https://jwt.io/).

Adding a `Role` header to your API requests will allow you to easily add security tests to your application. Thorough, exhaustive security tests are a must have when building an application today. Malicious users are out there who will try to exploit your system, and you need the confidence that you've set up your micro frontends and API properly.

I have provided a [Postman](https://www.postman.com/) collection and environment in [my GitHub repo](https://github.com/allenheltondev/postman-security-test-generator/) that will automatically test your API for security holes. Use this to help make sure you have the proper micro frontend and [API design](https://www.readysetcloud.io/blog/allen.helton/api-first-development-with-postman/) to support your app.

## Accessing Micro Frontends
There really isn't a right or a wrong way to provide access to your micro frontend. I take that back. There are definitely wrong ways.

The two most common ways to give your users access to micro frontends are through **subdomains** and **routes**.

You see both patterns all over the internet and it is entirely up to you how you want to implement.

![Examples of subdomains vs routes](https://readysetcloud.s3.amazonaws.com/microfrontend_4.png)

If you are big into setting up DNS routing tables, subdomains might be your new best friend. If you avoid DNS routing like the plague, routes will be the way to go. Either way you choose, **providing your users with a seamless experience is your main objective**.

## Micro Conclusion
You are already building microservices for your API, it doesn't make sense to continue building your monolithic frontend.

Break it up, iterate independently, and push small, incremental changes. You don't want to redeploy the entire frontend because you misspelled a label, right?

Build security directly into your apps. Define roles with a known set of permissions. Lock it down.

Your goal as a software developer, manager, product owner, QA analyst, business analyst, tech writer, etc... is to make the best software for your customer. Provide the best experience by mitigating down time and improving security.

The software industry is moving at breakneck speed, and it isn't going to slow down any time soon. Expectations on agility, user experience, and security are at an all-time high, and you can keep up if you simply break it down.