---
title: "Discussion on \"The Tri-Z Architecture: a Pattern for Layering ZIO Applications in Scala\" | Hashnode"
description: "Discussion on \"The Tri-Z Architecture: a Pattern for Layering ZIO Applications in Scala\". After working on several different services and spending a lot of time improving the code to make it easier to use, I discovered a pattern for layering my applications that I found very useful.\nFirst, a disclaimer: this architecture is not a one-size..."
url: "https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [The Tri-Z Architecture: a Pattern for Layering ZIO Applications in Scala](https://ghostdogpr.hashnode.dev/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala?utm_source=hashnode&utm_medium=feed)

After working on several different services and spending a lot of time improving the code to make it easier to use, I discovered a pattern for layering my applications that I found very useful. First, a disclaimer: this architecture is not a one-size...

Aug 5, 2024 · 12 min read · ghostdogpr.hashnode.dev  
Updated Jan 30  
Read the article

-   [#scala](https://hashnode.com/tag/scala)
-   [#backend](https://hashnode.com/tag/backend)
-   [#architecture](https://hashnode.com/tag/architecture)
-   [#layered-architecture](https://hashnode.com/tag/layered-architecture)
-   [#zio](https://hashnode.com/tag/zio)

## Written by

[Pierre Ricadat](https://hashnode.com/@ghostdogpr)

@ghostdogpr · Seoul, South Korea

  
[View profile](https://hashnode.com/@ghostdogpr)

Software Architect

Software Architect | Scala Lover | Creator of Caliban and Shardcake

## Responses 8

Markdown supported · ⌘↵ to post

[ZHE ZHANG](https://hashnode.com/@zhangjiji)

[May 9, 2025](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/681e24bee20965f5d3d4af0f)

Sorry to bother you again, but I'd like to ask about cross-user processing, such as leaderboards and server-wide emails. These are very similar to singleton services. Can sharding help with this? How do you handle the pressure on these types of services?

[Pierre Ricadat](https://hashnode.com/@ghostdogpr)

[May 9, 2025](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/681e24bee20965f5d3d4af0f#reply-681e8b99063d4ea22731e501)

How about discussing on Discord instead of here? Comments are not ideal for long conversations. There’s a Shardcake channel on the ZIO Discord.

[ZHE ZHANG](https://hashnode.com/@zhangjiji)

[May 10, 2025](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/681e24bee20965f5d3d4af0f#reply-681ebb24175d41087b05b130)

Is it convenient to use your Gmail? [Pierre Ricadat](https://hashnode.com/@ghostdogpr)

[Pierre Ricadat](https://hashnode.com/@ghostdogpr)

[May 10, 2025](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/681e24bee20965f5d3d4af0f#reply-681ebc73822a1dabc03ebd3c)

[ZHE ZHANG](https://hashnode.com/@zhangjiji) I’d rather use Discord since other people can benefit from reading it. Potentially a GitHub issue if it’s specifically about Shardcake.

[ZHE ZHANG](https://hashnode.com/@zhangjiji)

[Apr 29, 2025](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/68101fa388ebcac2ef43f1c1)

Hello, I saw in your "Beautiful Domain Logic" slide that you discussed Event Sourcing, and it seems like events are written to both a database and a message queue like Kafka. My questions are: Is Event Sourcing actually used in games? Does the database encounter performance issues due to the large number of event records? How is Event Sourcing integrated with sharding (like Shardcake)?

[Pierre Ricadat](https://hashnode.com/@ghostdogpr)

[May 8, 2025](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/68101fa388ebcac2ef43f1c1#reply-681c7574aadd6478cc7915ca)

Right, we are not using Akka persistence and instead use our own code. We're just using [github.com/typelevel/doobie](https://github.com/typelevel/doobie) to communicate with the database.

We don't use STM in this part of the code actually, it's all one big state object in memory. When we load an actor, we load the latest snapshot and journals, replay those to have the latest state in memory. Then we serve reads and writes from that.

When we need a "fast access" to some data without loading the actor, we have some view tables in the database that are updated via Kafka: after the state is updated and saved in DB we publish some Kafka messages and a separate component reads those and update the views.

[ZHE ZHANG](https://hashnode.com/@zhangjiji)

[May 8, 2025](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/68101fa388ebcac2ef43f1c1#reply-681c78ed061ee51da5de0fe7)

I'm also curious, if we use Kubernetes rolling updates, will the database read pressure be very high when the in-memory actor state is rebuilt? [Pierre Ricadat](https://hashnode.com/@ghostdogpr)

[Pierre Ricadat](https://hashnode.com/@ghostdogpr)

[May 9, 2025](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/68101fa388ebcac2ef43f1c1#reply-681d459dd3a3a23a3443d218)

In our case it has never been a problem (since rolling updates start pods sequentially the pressure is not that high).

[ZHE ZHANG](https://hashnode.com/@zhangjiji)

[Feb 13, 2025](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/67ada0aa241e6d265ffd7ee3)

If ZPure is used together with Event Sourcing, because Event Sourcing has an EventHandler that folds events and applies them to the state, and ZPure also describes the change from the oldState to the newState, does this mean that the change function is provided by the State model and is called in both ZPure and the EventHandler? But is it still necessary to coordinate the changes in ZPure and the changes in Event Sourcing to be consistent?

[Pierre Ricadat](https://hashnode.com/@ghostdogpr)

[Feb 13, 2025](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/67ada0aa241e6d265ffd7ee3#reply-67adaaa70034f0e755a0e09d)

The way we do it is that we have a function `liftEvent` that "logs" the event but also runs a "transition" that modifies the state. We use a typeclass for `Transition[Event]`. When running the ZPure we get the new state with the list of lifted events and we can write those event to our event store. On replay, we run the same transitions for each event.

[ZHE ZHANG](https://hashnode.com/@zhangjiji)

[Feb 13, 2025](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/67ada0aa241e6d265ffd7ee3#reply-67adb7a9b47c0c25401869c2)

Many Thanks! I feel like these tricks are worth writing a new blog post about～

[ZHE ZHANG](https://hashnode.com/@zhangjiji)

[Feb 13, 2025](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/67ad975695477e0a716474d9)

I have one more question. If there are two programs, for example, Guild and Inventory, with their states being GuildState and InventoryState respectively, and if I want to combine them to write new logic, do I need some kind of "glue" program?

[Feb 13, 2025](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/67ad975695477e0a716474d9#reply-67ad9c3fc13bd8ce98ecd3f5)

I usually keep the State common for all my programs. If they are really different entities, I keep the logic completely separate and have them communicate with each other (e.g. via messages).

[ZHE ZHANG](https://hashnode.com/@zhangjiji)

[Jan 9, 2025](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/677fa9708649df594c2445bf)

How to mix Random/Clock(ZIO side effects) with ZPure(pure domain)? While generating some random rewards or properties for equipments in business logic.

[Pierre Ricadat](https://hashnode.com/@ghostdogpr)

[Jan 9, 2025](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/677fa9708649df594c2445bf#reply-677fac5778b7eabfce0eb273)

Since they return `ZIO`, you can't use them directly. In our case we often include the current time in the environment R (so we get it before running the ZPure and then the value is consistent during the execution since the environment is read-only). For random, we use Scala Random directly wrapped inside `ZPure.suspend`.

[ZHE ZHANG](https://hashnode.com/@zhangjiji)

[Feb 13, 2025](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/677fa9708649df594c2445bf#reply-67ad9342e6b0672ff7045137)

I apologize, I'm still a bit confused about the Random part.

I understand it's like defining something like: def nextInt: Program\[Int\] = ZPure.suspend(ZPure.attempt(rng.nextInt()))?

Does using Scala's Random directly lead to a loss of purity? Could you please explain it a bit more concretely? Thank you.

[Pierre Ricadat](https://hashnode.com/@ghostdogpr)

[Feb 13, 2025](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/677fa9708649df594c2445bf#reply-67ad9ba2a14d61a28ab25a65)

That's right, that's how we do if we need it. It's non-deterministic but I think it's acceptable since it doesn't cause any actual side effects.

[Walmart](https://hashnode.com/@topsmminusa)

[Sep 2, 2024](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/66d5f71b1318e4880cc4aaf7)

Buy Verified Cash App Accounts [topsmminusa.com/product/buy-verified-cash-app-acc…](https://topsmminusa.com/product/buy-verified-cash-app-accounts/) Buy Verified Cash App Accounts - Buy 100% Verified BTC OR NON BTC Cash App Account, You can buy verified cash account without any doubt. Our company TopSMMinUSA is the best cash app service provider. 24 Hours Reply/Contact Email: topsmminusa@gmail.com WhatsApp: +1 425-287-2237 Telegram: @topsmminusa Skype: topsmminusa@gmail.com

[Walmart](https://hashnode.com/@topsmminusa)

[Sep 2, 2024](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/66d5f717252d0d61c2ca9252)

Buy Aged LinkedIn Accounts [topsmminusa.com/product/buy-aged-linkedin-accounts](https://topsmminusa.com/product/buy-aged-linkedin-accounts/) Buy Aged LinkedIn Accounts - Buy new and old LinkedIn accounts to enhance your profile Buying old and new LinkedIn accounts from TopSMminUSA is very easy and safe. We sell high quality LinkedIn accounts. 24 Hours Reply/Contact Email: topsmminusa@gmail.com WhatsApp: +1 425-287-2237 Telegram: @topsmminusa Skype: topsmminusa@gmail.com

[Walmart](https://hashnode.com/@topsmminusa)

[Sep 2, 2024](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/66d5f711a6b5dd2ce854747f)

Buy USA Facebook Accounts [topsmminusa.com/product/buy-usa-facebook-accounts](https://topsmminusa.com/product/buy-usa-facebook-accounts/) Buy USA Facebook Account - Buying USA Facebook account from TopSMMinUSA is very easy and sustainable. We deliver in very short time. 24 Hours Reply/Contact Email: topsmminusa@gmail.com WhatsApp: +1 425-287-2237 Telegram: @topsmminusa Skype: topsmminusa@gmail.com

[john dizuza](https://hashnode.com/@johnkapoor)

[Aug 28, 2024](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/66cf6b22a7c7bee090d0e917)

Is there now a new version made for Android users? Players often face difficult challenges in the game. There are many characters available in Garena Free Fire. A novice player cannot beat a professional opponent in a game. So Girngo xp new update feature will help new players to beat their enemies in the game. However, you can increase your battle points to have a chance to advance in the game. Additionally, players are given in-game magic weapons.

[Harold Roninski](https://hashnode.com/@Tonny643)

[Aug 21, 2024](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/66c60236ad77d0dcbdeb4483)

Hi, I recently discovered [getmp3.one](https://getmp3.one/) and it has completely changed the way I enjoy my favourite content on YouTube. The conversion process is fast and the sound quality is top-notch. Whether it's music, podcasts or interviews, I can now listen to everything offline without any hassle. The interface is user-friendly, so converting and downloading files is quick and easy, with just a few clicks. I highly recommend this service for anyone who wants to build their music library effortlessly!

[Lilian Hunter](https://hashnode.com/@Lilianhunter)

[Aug 17, 2024](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/66bfed5d2abe193a7c42830d)

SPEAK WITH A REASONABLE, LICENSED BTC/USDT RECOVERY HACKER - ALPHA KEY

After being unable to withdraw or send money from my block chain wallet address, I became a victim of a crypto investment scam. I found online reviews of Alpha Key Recovery's hacker expert services and how they have been assisting victims of scams involving cryptos. If you have fallen victim to any kind of scam, please contact them using the information below. They continue to be the most dependable crypto hacker that I can recommend to anyone.

WhatsApp Messenger: +15714122170 Signal Message: +18622823879 Alpha Key Recovery Telegram channel

[Liam Grayson](https://hashnode.com/@LiamGrayson)

[Aug 16, 2024](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/66bfe0b00fba798a8bf48102)

ASSISTING VICTIMS OF CRYPTO/USDT FRAUD HIRE\\FOLKWIN EXPERT RECOVERY.

I received a direct message via Reddit from a friend who told me that he had invested $1,000 and got a return of $5,000, and that I could earn money too, by contacting "Cm\_fx" on Instagram. Intrigued by the possibility of such high returns, I reached out to “Cm\_fx” and expressed my interest in investing. Cm\_fx appeared very knowledgeable and claimed to mine Bitcoin while also being an account manager who could offer substantial returns on my investment. Believing this opportunity to be legitimate, I sent $2,000 worth of Bitcoin to Cm\_fx, who assured me that she had set up an account with her trading company, 24trading net. my account balance on the platform displayed a profit of $21,800. This substantial gain made the investment seem promising. However, when I tried to cash out, I was informed that I had to pay a withdrawal fee of $3,650. Cm\_fx explained that this fee was necessary to process the withdrawal. Although it felt unusual, I complied with the request, believing it to be a standard procedure for the platform. Encouraged. Encouraged by the success, Cm\_fx convinced me to invest another $1,000 worth of Bitcoin, promising that the additional investment would yield even higher returns. After a week, my account balance showed a profit of $45,800. Once again, Cm\_fx informed me that a withdrawal fee was required to access my funds. This time, the fee was $3,700. To cover this cost, I had to borrow money, which I sent following Cm\_fx’s detailed instructions. Despite my payment, the withdrawal was marked as "pending." Growing concerned, I tried to contact Cm\_fx for clarification. In response, she instructed me to give the “Support Team” access to my Instagram account, including backup codes. This request seemed odd, but Cm\_fx assured me it was a necessary step to facilitate the transaction. Soon. Soon. Soon after granting access, I received a message from the Support Team, claiming that I needed to pay an additional $12,400 to upgrade my account to comply with SEC regulations. They insisted that this fee was mandatory due to the high balance in my account. This escalating demand for money was alarming. Desperate to retrieve my funds, I called Cm\_fx, pleading for assistance, but she failed to respond. It became clear that I was dealing with a scam. The consistent demands for more money and the fraudulent activities were unmistakable indicators of a scam. I quickly reset my Instagram account to regain control and prevent further access. Upon. Upon further investigation, I discovered that the friend who had initially referred me to Cm\_fx had also been scammed. Their account had been hacked, revealing the scam's extensive reach and its manipulative tactics. Realizing the gravity of the situation, I sought help from "FOLKWIN EXPERT RECOVERY" Immediately. They assisted me in recovering my stolen funds .Their intervention was crucial in mitigating the damage and protecting my identity from further exploitation. This experience underscored the importance of being cautious with unsolicited investment opportunities, especially those involving unfamiliar individuals or platforms. Scammers often use convincing personas and fabricated success stories to lure victims into their schemes. It’s essential to verify the legitimacy of any investment opportunity, avoid sharing personal information, and seek professional help if you suspect fraud. my encounter with this scam serves as a stark reminder of the risks associated with online investments and the need for vigilance. The lessons learned from this experience emphasize the significance of conducting thorough research and exercising caution to avoid falling victim to similar scams in the future. Contact "FOLKWIN EXPERT RECOVERY" with the details below; Info.. E-Mail: FOLKWINEXPERTRECOVERY @ TECH-CENTER dot COM  
Info.. W-App: +1 (740)(705)-(0711).  
God bless Regards, Mr Liam Grayson.

[Elvis Lee](https://hashnode.com/@Elvis5566)

[Aug 16, 2024](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/66bf817c59e4257e325b582a)

Very Interesting, but I am still confused... If Shardcake entity handling messages one at a time, just like Akka actor does. Why do you need to handle race condition?

[Pierre Ricadat](https://hashnode.com/@ghostdogpr)

[Aug 17, 2024](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/66bf817c59e4257e325b582a#reply-66c021508ba5cee8f7e6b7e6)

This allows you to handle the actor messages in parallel instead of sequentially (e.g. using mapZIOPar or just forking each message handling). Now depending on the use case that may or may not be desirable. If there would be a lot of contention (each request handling accessing the same objects), sequentially is probably better. In that case you can still use the ZIO/ZPure separation and skip the ZSTM layer. In my use cases (games), we often want to respond to client requests ASAP so parallelizing is better.

[Elvis Lee](https://hashnode.com/@Elvis5566)

[Aug 17, 2024](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/66bf817c59e4257e325b582a#reply-66c0bb5c1fedc1d759896b4c)

Thank you for the comprehensive explanation. I learned a lot. Much appreciated.

[Eka Intan](https://hashnode.com/@Bagas31pro)

[Aug 22, 2024](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/66bf817c59e4257e325b582a#reply-66c725e8e39cd88fc3e1cd43)

[Elvis Lee](https://hashnode.com/@Elvis5566)[SketchUp Pro 2024 v24.0.553 Unduh Pra-Aktivasi | Bagas31](https://bagas31.pro/sketchup-pro-unduh-pra-aktivasi/)

[David K.](https://hashnode.com/@dejvid)

[Aug 10, 2024](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/66b7470c45c56c8e398ed9ff)

Thank you for this insightful review of stacked technologies. But I still wonder if guilds are contained in one server but other things are not. I guess the ZIO effect code needs to access multiple guilds from different servers. How do you solve this communication issue?

[Elvis Lee](https://hashnode.com/@Elvis5566)

[Aug 7, 2024](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/66b3098bc035a93b10be0e83)

Hi, For the ZPure type W, you mentioned "I've used it for storing events in an event sourcing application, for feeding a stream of messages to players in a game, but also for storing actual logs" Isn't logging or firing events a side effect? And should no be in ZPure?

[Aug 7, 2024](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/66b3098bc035a93b10be0e83#reply-66b31881ee899826043ad5d3)

That's right, but in the ZPure layer I only accumulate them. After running the ZPure, if the program was successful only then I actually log them or do something with them.

[Elvis Lee](https://hashnode.com/@Elvis5566)

[Aug 7, 2024](https://hashnode.com/posts/the-tri-z-architecture-a-pattern-for-layering-zio-applications-in-scala/66b07c222ac672d4672fe731/comment/66b3098bc035a93b10be0e83#reply-66b3300c0788c291edaa78dd)

I See, Thank you :)
