Skip to content

Commit 3d3cc5f

Browse files
committed
microservices tutorials prisma update (except interservice communication tutorial)
1 parent 2e3dd3f commit 3d3cc5f

File tree

14 files changed

+211
-209
lines changed

14 files changed

+211
-209
lines changed

docs/howtos/solutions/caching-architecture/write-behind/index-write-behind.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import mongoCompassImage from './images/mongo-compass.png';
1616

1717
<Authors frontMatter={frontMatter} />
1818

19+
<SourceCodeMovieApp />
20+
1921
## What is write-behind caching?
2022

2123
Imagine you've built a movie streaming app. You used MongoDB as your data store, and as you needed to scale you implemented caching using Redis. This allows you to drastically speed up reads. However, now you are experiencing slowness when writing to MongoDB.

docs/howtos/solutions/microservices/api-gateway-caching/index-api-gateway-caching.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ While this works, you end up with a fair amount of duplicated code. Plus, it's d
3232

3333
Once you decide to handle authentication at the API gateway layer, you must decide where to store sessions.
3434

35-
Imagine you're building an e-commerce application that uses MongoDB as the primary data store. You could store sessions in MongoDB, but think about how many times the application needs to hit MongoDB to retrieve session information. If you have millions of customers, you don't want to go to MongoDB for every single request made to the API.
35+
Imagine you're building an e-commerce application that uses MongoDB/ any relational database as the primary data store. You could store sessions in primary database, but think about how many times the application needs to hit primary database to retrieve session information. If you have millions of customers, you don't want to go to database for every single request made to the API.
3636

3737
This is where Redis comes in.
3838

@@ -60,7 +60,7 @@ Use a **Redis Enterprise Cluster** to get the benefit of linear scaling to ensur
6060

6161
<SourceCode />
6262

63-
## API gateway caching in a microservices application with Redis and MongoDB
63+
## API gateway caching in a microservices application with Redis
6464

6565
What's nice about a microservice architecture is that each service is set up so it can scale independently. Now, seeing as how each service might require authentication, you likely want to obtain session information for most requests. Therefore, it makes sense to use the API gateway to cache and retrieve session information and to subsequently pass the information on to each service. Let's see how you might accomplish this.
6666

docs/howtos/solutions/microservices/caching/index-caching.mdx

Lines changed: 37 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import cacheHitImage from './images/redis-cache-aside-cache-hit.png';
2424
Have you ever been in a situation where your database queries are slowing down?
2525
Query caching is the technique you need to speed database queries by using different caching methods while keeping costs down! Imagine that you built an e-commerce application. It started small but is growing fast. By now, you have an extensive product catalog and millions of customers.
2626

27-
Thats good for business, but a hardship for technology. Your queries to MongoDB are beginning to slow down, even though you already attempted to optimize them. Even though you can squeak out a little extra performance, it isnt enough to satisfy your customers.
27+
That's good for business, but a hardship for technology. Your queries to primary database (MongoDB/ Postgressql) are beginning to slow down, even though you already attempted to optimize them. Even though you can squeak out a little extra performance, it isn't enough to satisfy your customers.
2828

2929
## Why you should use Redis for query caching
3030

@@ -99,7 +99,7 @@ If you use **Redis Enterprise** and a database that uses a JDBC driver, you can
9999

100100
<SourceCode />
101101

102-
## Caching in a microservices application with Redis and MongoDB
102+
## Caching in a microservices application with Redis and primary database (MongoDB/ Postgressql)
103103

104104
In our sample application, the products service publishes an API for filtering products. Here's what a call to the API looks like:
105105

@@ -114,32 +114,27 @@ In our sample application, the products service publishes an API for filtering p
114114

115115
### Get products by filter response (cache miss)
116116

117-
```json {30}
117+
```json {25}
118118
{
119119
"data": [
120120
{
121-
"_id": 11000,
122-
"data": {
123-
"id": 11000,
124-
"price": 3995,
125-
"productDisplayName": "Puma Men Slick 3HD Yellow Black Watches",
126-
"variantName": "Slick 3HD Yellow",
127-
"brandName": "Puma",
128-
"ageGroup": "Adults-Men",
129-
"gender": "Men",
130-
"displayCategories": "Accessories",
131-
"styleImages": {
132-
"default": {
133-
"imageURL": "http://host.docker.internal:8080/images/11000.jpg"
134-
}
135-
},
136-
"productDescriptors": {
137-
"description": {
138-
"value": "Stylish and comfortable, this motor sport inspired wrist watch from puma is designed with a plastic case and ..."
139-
}
140-
}
141-
},
142-
"productId": 11000
121+
"productId": "11000",
122+
"price": 3995,
123+
"productDisplayName": "Puma Men Slick 3HD Yellow Black Watches",
124+
"variantName": "Slick 3HD Yellow",
125+
"brandName": "Puma",
126+
"ageGroup": "Adults-Men",
127+
"gender": "Men",
128+
"displayCategories": "Accessories",
129+
"masterCategory_typeName": "Accessories",
130+
"subCategory_typeName": "Watches",
131+
"styleImages_default_imageURL": "http://host.docker.internal:8080/images/11000.jpg",
132+
"productDescriptors_description_value": "<p style=\"text-align: justify;\">Stylish and comfortable, ...",
133+
"createdOn": "2023-07-13T14:07:38.020Z",
134+
"createdBy": "ADMIN",
135+
"lastUpdatedOn": "2023-07-13T14:07:38.020Z",
136+
"lastUpdatedBy": null,
137+
"statusCode": 1
143138
}
144139
//...
145140
],
@@ -160,66 +155,36 @@ In our sample application, the products service publishes an API for filtering p
160155
}
161156
```
162157

163-
### Implementing cache-aside with Redis and MongoDB
158+
### Implementing cache-aside with Redis and primary database (MongoDB/ Postgressql)
164159

165-
The following code shows the function used to search for products in MongoDB:
160+
The following code shows the function used to search for products in primary database:
166161

167162
```typescript title="server/src/services/products/src/service-impl.ts"
168-
async function getProductsByFilter(productFilter: IProductFilter) {
169-
const mongo = getMongodb();
170-
const filter: Document = {
171-
statusCode: {
172-
$eq: DB_ROW_STATUS.ACTIVE,
173-
},
163+
async function getProductsByFilter(productFilter: Product) {
164+
const prisma = getPrismaClient();
165+
166+
const whereQuery: Prisma.ProductWhereInput = {
167+
statusCode: DB_ROW_STATUS.ACTIVE,
174168
};
175169

176170
if (productFilter && productFilter.productDisplayName) {
177-
filter['data.productDisplayName'] = {
178-
$regex: productFilter.productDisplayName,
179-
$options: 'i',
171+
whereQuery.productDisplayName = {
172+
contains: productFilter.productDisplayName,
173+
mode: 'insensitive',
180174
};
181175
}
182176

183-
const projection: IProduct = {
184-
productId: 1,
185-
data: {
186-
id: 1,
187-
price: 1,
188-
productDisplayName: 1,
189-
variantName: 1,
190-
brandName: 1,
191-
ageGroup: 1,
192-
gender: 1,
193-
displayCategories: 1,
194-
styleImages: {
195-
default: {
196-
imageURL: 1,
197-
},
198-
},
199-
productDescriptors: {
200-
description: {
201-
value: 1,
202-
},
203-
},
204-
},
205-
};
177+
const products: Product[] = await prisma.product.findMany({
178+
where: whereQuery,
179+
});
206180

207-
const limit = 100;
208-
const sort = {};
209-
const products = await mongo.find(
210-
COLLECTIONS.PRODUCTS.collectionName,
211-
filter,
212-
projection,
213-
limit,
214-
sort,
215-
);
216181
return products;
217182
}
218183
```
219184

220-
If you're familiar with MongoDB, this code should be pretty straightforward. You simply make a call to MongoDB to find products based on a filter on the products `displayName` property. You also define a projection object to specify which properties to get out of MongoDB. You can set up multiple columns for better fuzzy searching, but we simplified it for the purposes of this tutorial.
185+
You simply make a call to primary database (MongoDB/ Postgressql) to find products based on a filter on the product's `displayName` property. You can set up multiple columns for better fuzzy searching, but we simplified it for the purposes of this tutorial.
221186

222-
Using MongoDB directly without Redis works for a while, but eventually it slows down. That's why you might use Redis, to speed things up. The cache-aside pattern helps you balance performance with cost.
187+
Using primary database directly without Redis works for a while, but eventually it slows down. That's why you might use Redis, to speed things up. The cache-aside pattern helps you balance performance with cost.
223188

224189
The basic decision tree for cache-aside is as follows.
225190

@@ -228,7 +193,7 @@ When the frontend requests products:
228193
1. Form a hash with the contents of the request (i.e., the search parameters).
229194
1. Check Redis to see if a value exists for the hash.
230195
1. Is there a cache hit? If data is found for the hash, it is returned; the process stops here.
231-
1. Is there a cache miss? When data is not found, it is read out of MongoDB and subsequently stored in Redis prior to being returned.
196+
1. Is there a cache miss? When data is not found, it is read out of primary database and subsequently stored in Redis prior to being returned.
232197

233198
Here’s the code used to implement the decision tree:
234199

@@ -256,7 +221,7 @@ router.post(API.GET_PRODUCTS_BY_FILTER, async (req: Request, res: Response) => {
256221
result.data = docArr;
257222
result.isFromCache = true;
258223
} else {
259-
// get data from mongodb
224+
// get data from primary database
260225
const dbData = await getProductsByFilter(body); //method shown earlier
261226

262227
if (body && body.productDisplayName && dbData.length) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
The e-commerce microservices application discussed in the rest of this tutorial uses the following architecture:
2+
3+
1. `products service`: handles querying products from the database and returning them to the frontend
4+
1. `orders service`: handles validating and creating orders
5+
1. `order history service`: handles querying a customer's order history
6+
1. `payments service`: handles processing orders for payment
7+
1. `digital identity service`: handles storing digital identity and calculating identity score
8+
1. `api gateway`: unifies services under a single endpoint
9+
1. `mongodb`: serves as the primary database, storing orders, order history, products, etc.
10+
1. `redis`: serves as the **stream processor** and caching database

docs/howtos/solutions/microservices/common-data/microservices-arch-with-redis.mdx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,11 @@ The e-commerce microservices application discussed in the rest of this tutorial
66
1. `payments service`: handles processing orders for payment
77
1. `digital identity service`: handles storing digital identity and calculating identity score
88
1. `api gateway`: unifies services under a single endpoint
9-
1. `mongodb`: serves as the primary database, storing orders, order history, products, etc.
10-
1. `redis`: serves as the stream processor and caching database
9+
1. `mongodb/ postgresql`: serves as the primary database, storing orders, order history, products, etc.
10+
1. `redis`: serves as the **stream processor** and caching database
11+
12+
:::info
13+
14+
You don't need to use MongoDB/ Postgresql as your primary database in the demo application; you can use other <u>[prisma supported databases](https://www.prisma.io/docs/reference/database-reference/supported-databases)</u> as well. This is just an example.
15+
16+
:::

docs/howtos/solutions/microservices/common-data/microservices-arch.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ You eventually land on the following architecture:
55
1. `order history service`: handles querying a customer's order history
66
1. `payments service`: handles processing orders for payment
77
1. `api gateway`: unifies the services under a single endpoint
8-
1. `mongodb`: serves as the write-optimized database for storing orders, order history, products, etc.
8+
1. `mongodb/ postgresql`: serves as the write-optimized database for storing orders, order history, products, etc.
99

1010
:::info
1111

12-
You dont need to use MongoDB as your write-optimized database; you can use other databases such as a SQL database as well. This is just an example.
12+
You don't need to use MongoDB/ Postgresql as your write-optimized database in the demo application; you can use other <u>[prisma supported databases](https://www.prisma.io/docs/reference/database-reference/supported-databases)</u> as well. This is just an example.
1313

1414
:::
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
The e-commerce microservices application consists of a frontend, built using [Next.js](https://nextjs.org/) with [TailwindCSS](https://tailwindcss.com/). The application backend uses [Node.js](https://nodejs.org). The data is stored in
2+
[Redis](https://redis.com/try-free/) and MongoDB. Below you will find screenshots of the frontend of the e-commerce app:
3+
4+
- `Dashboard`: Shows the list of products with search functionality
5+
6+
![redis microservices e-commerce app frontend products page](images/design-dashboard.png)
7+
8+
- `Shopping Cart`: Add products to the cart, then check out using the "Buy Now" button
9+
![redis microservices e-commerce app frontend shopping cart](images/design-cart-2.png)
10+
11+
- `Order history`: Once an order is placed, the `Orders` link in the top navigation bar shows the order status and history
12+
13+
![redis microservices e-commerce app frontend order history page](images/design-order-history.png)

docs/howtos/solutions/microservices/common-data/microservices-ecommerce.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
The e-commerce microservices application consists of a frontend, built using [Next.js](https://nextjs.org/) with [TailwindCSS](https://tailwindcss.com/). The application backend uses [Node.js](https://nodejs.org). The data is stored in [MongoDB](https://www.mongodb.com/) and [Redis](https://redis.com/try-free/). Below you will find screenshots of the frontend of the e-commerce app:
1+
The e-commerce microservices application consists of a frontend, built using [Next.js](https://nextjs.org/) with [TailwindCSS](https://tailwindcss.com/). The application backend uses [Node.js](https://nodejs.org). The data is stored in
2+
[Redis](https://redis.com/try-free/) and MongoDB/ Postgressql using [Prisma](https://www.prisma.io/docs/reference/database-reference/supported-databases). Below you will find screenshots of the frontend of the e-commerce app:
23

34
- `Dashboard`: Shows the list of products with search functionality
45

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
:::tip GITHUB CODE
2+
3+
Below is a command to the clone the source code for the application used in this tutorial
4+
5+
git clone --branch v1.0.0 https://github.com/redis-developer/redis-microservices-ecommerce-solutions
6+
7+
:::

docs/howtos/solutions/microservices/common-data/microservices-source-code-tip.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
Below is a command to the clone the source code for the application used in this tutorial
44

5-
git clone --branch v1.0.0 https://github.com/redis-developer/redis-microservices-ecommerce-solutions
5+
git clone --branch v4.2.0 https://github.com/redis-developer/redis-microservices-ecommerce-solutions
66

77
:::

0 commit comments

Comments
 (0)