Skip to content

Commit 0922e61

Browse files
committed
docs: update introduction
1 parent d4b65e3 commit 0922e61

2 files changed

Lines changed: 29 additions & 33 deletions

File tree

docs/content/docs/extend/custom_lock_store.md

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,10 @@ Extending Verrou with your own cache driver is easy. What you need is a class th
88

99
```ts
1010
interface LockStore {
11-
/**
12-
* Save the lock in the store if not already locked
13-
*/
14-
save(key: string, owner: string, ttl: number | null | undefined): Promise<boolean>
15-
16-
/**
17-
* Delete the lock from the store
18-
*/
11+
save(key: string, owner: string, ttl: number | undefined): Promise<boolean>
1912
delete(key: string, owner: string): Promise<void>
20-
21-
/**
22-
* Check if the lock exists
23-
*/
2413
exists(key: string): Promise<boolean>
25-
26-
/**
27-
* Extend the lock expiration
28-
*/
29-
extend(key: string, duration: Duration): Promise<void>
30-
31-
/**
32-
* Disconnect the store
33-
*/
14+
extend(key: string, owner: string, duration: number): Promise<void>
3415
disconnect(): Promise<void>
3516
}
3617
```

docs/content/docs/introduction.md

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Verrou is a locking library for managing locks ( mutexes ) in a NodeJS applicati
1919
:::codegroup
2020

2121
```ts
22-
// title: Basic example
22+
// title: Configuration
2323
import { Verrou } from '@verrou/core'
2424
import { redisStore } from '@verrou/core/drivers/redis'
2525
import { memoryStore } from '@verrou/core/drivers/memory'
@@ -31,10 +31,6 @@ const verrou = new Verrou({
3131
memory: { driver: memoryStore() }
3232
}
3333
})
34-
35-
await verrou.createLock('my-resource').run(async () => {
36-
await doSomething()
37-
}) // Lock is automatically released
3834
```
3935

4036
```ts
@@ -54,6 +50,14 @@ try {
5450
}
5551
```
5652

53+
```ts
54+
// title: Automatic lock
55+
const lock = verrou.createLock('my-resource')
56+
57+
await lock.run(async () => {
58+
await doSomething()
59+
}) // Lock is automatically released
60+
```
5761
:::
5862

5963
## Why Verrou ?
@@ -83,16 +87,27 @@ router.get('/transfer', () => {
8387
})
8488
```
8589

86-
Okay cool. It works when we are trying it locally. But imagine something. What if two users are calling the same endpoint at the same time ? What will happen ?
90+
Okay cool. It works when we are trying it locally. But imagine something. What if two users try to transfer money to the same account at the same time ?
91+
92+
Let's consider the following scenario :
93+
- User A wants to transfer 100$ to Account C
94+
- User B also wants to transfer 100$ to Account C
95+
- Account C has a balance of 1000$
96+
97+
What will happen if both request are almost simultaneously executed ?
8798

88-
1. User A's request reads the balance of Account X.
89-
2. Concurrently, User B's request also reads the balance of Account X.
90-
3. User A's request deducts the transfer amount from Account X and saves it.
91-
4. Almost simultaneously, User B's request does the same, but it was unaware of the change made by User A's request because it read the old balance.
99+
1. User A's request reads the balance of Account C : **We get 1000$**
100+
2. Concurrently, User B's request also reads the balance of Account C: **We also get 1000$ since User A's request hasn't been fully executed yet**
101+
3. User A's request adds 100$ to Account C's balance : **Account C now has 1100$**
102+
4. Almost simultaneously, User B's request does the same. But remember, we stored the balance of Account C in a variable, and we added 100$ to it. So we also get **1100$ + 100$ = 1200$**.
92103

93-
As a result, Account X's balance end up totally incorrect. This is a classic example of what we call race condition.
104+
See the problem ? That means, Account C will end up with 1100$ instead of 1200$. And even worse, User A and User B have been debited 100$ each, but only 100$ has been credited to Account C.
105+
106+
As a result, we lost 100$ somewhere. And that's not good. This is what we also call a **race condition**.
107+
108+
---
94109

95-
They are multiple ways to solve this problem. A simple one would be to use a lock. By adding a lock, we are preventing concurrent requests from accessing the same piece of code at the same time :
110+
They are multiple ways to solve this problem. But let's use a lock here. By adding a lock, we are preventing concurrent requests from accessing the same piece of code at the same time :
96111

97112
```ts
98113
router.get('/transfer', () => {

0 commit comments

Comments
 (0)