-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
126 lines (101 loc) · 3.33 KB
/
main.go
File metadata and controls
126 lines (101 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"context"
"fmt"
"strconv"
"time"
"github.com/RhoNit/optimistic_locking_in_redis/config"
"github.com/RhoNit/optimistic_locking_in_redis/model"
"github.com/redis/go-redis/v9"
)
var ctx = context.Background()
func createProduct(client *redis.Client, productId string, initialStock int) {
client.HSet(ctx, productId, map[string]interface{}{
"stock": initialStock,
"version": 1,
})
}
func fetchProduct(client *redis.Client, productId string) *model.Product {
stock1, err := client.HGet(ctx, productId, "stock").Result()
if err != nil {
fmt.Printf("Error while fetching the stock of: %q\n", err)
}
v1, err := client.HGet(ctx, productId, "version").Result()
if err != nil {
fmt.Printf("Error while fetching the version of: %q\n", err)
}
stock, _ := strconv.Atoi(stock1)
version, _ := strconv.Atoi(v1)
return &model.Product{
ID: productId,
Stock: stock,
Version: version,
}
}
func updateStock(client *redis.Client, product *model.Product, changeInStock int) error {
txf := func(tx *redis.Tx) error {
// fetch the current version inside the transaction
productData, err := tx.HGetAll(ctx, product.ID).Result()
if err != nil {
return fmt.Errorf("failed to fetch product: %q", err)
}
currentStock, _ := strconv.Atoi(productData["stock"])
currentVersion, _ := strconv.Atoi(productData["version"])
if currentVersion != product.Version {
return fmt.Errorf("version mismatch: requested %d, got %d\n", product.Version, currentVersion)
}
// update the stock.. and version as well
stockAfterChange := currentStock + changeInStock
if stockAfterChange < 0 {
return fmt.Errorf("insufficient stock: requested a change of %d unit, but available stock: %d\n", changeInStock, currentStock)
}
_, err = tx.Pipelined(ctx, func(pipe redis.Pipeliner) error {
pipe.HSet(ctx, product.ID, map[string]interface{}{
"stock": stockAfterChange,
"version": currentVersion + 1,
})
return nil
})
return err
}
// execute the transaction
for i := 0; i < 3; i++ {
err := client.Watch(ctx, txf, product.ID)
if err == nil {
fmt.Printf("Product: %s is updated successfully.. new stock: %d units\n", product.ID, product.Stock+changeInStock)
return nil
}
if err == redis.TxFailedErr {
fmt.Printf("Transaction failed. Retrying...\n")
time.Sleep(10 * time.Millisecond)
continue
}
}
return fmt.Errorf("failed to update product after retries")
}
func simulateConcurrentStockHandling(client *redis.Client, productId string) {
// product accessed by the client #1
product1 := fetchProduct(client, productId)
// product accessed by the client #2
product2 := fetchProduct(client, productId)
// client 1 tries to update the stock value
err := updateStock(client, product1, -5)
if err != nil {
fmt.Printf("Client 1 has failed to update stock: %q\n", err)
}
// client 2 is trying to update the stock value
err = updateStock(client, product2, -10)
if err != nil {
fmt.Printf("Client 2 has failed to update stock: %q\n", err)
}
}
func main() {
// initialize redis connection
rdb := config.InitCache()
// store initial stock value of a product in the inventory
productID := "product:917:stock"
stock := 50
createProduct(rdb, productID, stock)
// simulate concurrency: by trying to access the same product by 2 clients
simulateConcurrentStockHandling(rdb, productID)
}