# Supporting multi-tenancy with Keycloak

**URL:** <https://forum.keycloak.org/t/supporting-multi-tenancy-with-keycloak/1492>\
**Category:** Getting advice\
**Tags:** authentication\
**Created:** [March 2, 2020, 6:12pm UTC](https://forum.keycloak.org/t/supporting-multi-tenancy-with-keycloak/1492 "2020-03-02T18:12:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![moiz](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@moiz](https://forum.keycloak.org/u/moiz)\
**Post date:** [March 2, 2020, 6:12pm UTC](https://forum.keycloak.org/t/supporting-multi-tenancy-with-keycloak/1492/1 "2020-03-02T18:12:49Z")

</div>

Hi All,

I need some advice on how to use keycloak for a multi-tenant application. To start off I used what would be a natural way to use keycloak for a multi-tenant application and that is I created 1 realm per tenant. This works pretty well except that this approach has a scale issue. As soon as we go beyond the 400 realm mark we start seeing extreme slowness. With about 450 realms restarting keycloak did not work - it got stuck in some migration loop and would not come out of it.

Now I want to switch to some alternate scheme to achieve the same result. I am contemplating using a single realm and segregating tenant users by group - is this something that will work or would there be issues?

If someone has successfully built a multi-tenant application with keycloak I would like to hear from you.

Thanks.

---

<div class="post-metadata">

**Author:** ![Robinyo](https://avatars.discourse-cdn.com/v4/letter/r/dec6dc/32.png) [@Robinyo](https://forum.keycloak.org/u/Robinyo)\
**Post date:** [March 2, 2020, 10:33pm UTC](https://forum.keycloak.org/t/supporting-multi-tenancy-with-keycloak/1492/2 "2020-03-02T22:33:14Z")

</div>

I found the following Keycloak Multi Tenancy [example](https://github.com/keycloak/keycloak/tree/master/examples/multi-tenant):

> This example demonstrates the simplest possible scenario for Keycloak Multi Tenancy support. Multi Tenancy is understood on this context as a single application (WAR) that is deployed on a single or clustered application server, authenticating users from different realms against a single or clustered Keycloak server.

Ref: [Securing Apps - Multi Tenancy](https://www.keycloak.org/docs/latest/securing_apps/#_multi_tenancy)

Also, see:

- [SaaS (Software as a Service) using Keycloak](http://www.janua.fr/using-keycloak-in-a-microservice-saas-architecture/)
- [Emulating Tenants using Roles](https://stackoverflow.com/questions/55641667/emulating-tenants-using-roles)

---

<div class="post-metadata">

**Author:** ![moiz](https://avatars.discourse-cdn.com/v4/letter/m/8797f3/32.png) [@moiz](https://forum.keycloak.org/u/moiz)\
**Post date:** [March 5, 2020, 1:07pm UTC](https://forum.keycloak.org/t/supporting-multi-tenancy-with-keycloak/1492/3 "2020-03-05T13:07:39Z")

</div>

Thanks @Robinyo for the pointers.

---

<div class="post-metadata">

**Author:** ![wvdhaute](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/wvdhaute/32/1907_2.png) [@wvdhaute](https://forum.keycloak.org/u/wvdhaute)\
**Post date:** [February 22, 2021, 3:01pm UTC](https://forum.keycloak.org/t/supporting-multi-tenancy-with-keycloak/1492/4 "2021-02-22T15:01:52Z")

</div>

See also [https://issues.redhat.com/browse/KEYCLOAK-4593](https://issues.redhat.com/browse/KEYCLOAK-4593)  
I found a hack to get RH-SSO working with 700+ realms in an acceptable way
