Skip to content

Commit 05a50f6

Browse files
committed
Update README.md
1 parent 6fd8dfd commit 05a50f6

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

README.md

+12-10
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ Thanks to this small library, you can create relatively simple code without the
2020
Normally a web application is using following pattern to modify data in the database:
2121

2222
1. **Load resource** from database. Resource is some portion of data
23-
such as record, document etc. Lock the entire resource pessimistically
24-
or optimistically (by reading version number).
23+
such as set of records from relational database, document from Document-oriented database or value from KV store.
24+
Lock the entire resource pessimistically or optimistically (by reading version number).
2525
2. **Apply change** to data
2626
3. **Save resource** to database. Release the pessimistic lock. Or run
2727
atomic update with version check (optimistic lock).
@@ -37,7 +37,7 @@ Because a single resource is loaded and saved thousands of times per second
3737
we can instead:
3838

3939
1. Load the resource **once** (let's say once per second)
40-
2. Execute all the requests from this period of time on an already loaded resource. Run them all sequentially.
40+
2. Execute all the requests from this period of time on an already loaded resource. Run them all sequentially to keep things simple and data consistent.
4141
3. Save the resource and send responses to all clients if data was stored successfully.
4242

4343
Such solution could improve the performance by a factor of 1000. And resource is still stored in a consistent state.
@@ -51,14 +51,15 @@ processor := batch.StartProcessor(
5151
batch.Options[*YourResource]{ // YourResource is your own Go struct
5252
MinDuration: 100 * time.Millisecond,
5353
LoadResource: func(ctx context.Context, resourceKey string) (*YourResource, error){
54-
// resourceKey uniquely identifies the resource
55-
...
54+
// resourceKey uniquely identifies the resource
55+
...
5656
},
5757
SaveResource: ...,
5858
},
5959
)
6060

61-
// Following code is run from http/grpc handler:
61+
// And use the processor inside http/grpc handler or technology-agnostic service.
62+
// ResourceKey can be taken from request parameter.
6263
err := s.BatchProcessor.Run(resourceKey, func(r *YourResource) {
6364
// Here you put the code which will executed sequentially inside batch
6465
})
@@ -72,18 +73,19 @@ For real-life example see [example web application](_example).
7273
# Add batch to your Go module:
7374
go get github.com/elgopher/batch
7475
```
75-
Please note that at least **Go 1.18** is required.
76+
Please note that at least **Go 1.18** is required. The package is using generics, which was added in 1.18.
7677

7778
## Scaling out
7879

79-
Single Go http server is able to handle up to tens of thousands of requests per second on a commodity hardware. This is a lot, but very often you also need:
80+
Single Go http server is able to handle up to tens of thousands of requests per second on a commodity hardware.
81+
This is a lot, but very often you also need:
8082

8183
* high availability (if one server goes down you want other to handle the traffic)
82-
* you want to handle hundred thousands or millions of requests per second
84+
* you want to handle hundred-thousands or millions of requests per second
8385

8486
For both cases you need to deploy **multiple servers** and put a **load balancer** in front of them.
8587
Please note though, that you have to carefully configure the load balancing algorithm.
86-
Round-robin is not an option here, because sooner or later you will have problems with locking
88+
_Round-robin_ is not an option here, because sooner or later you will have problems with locking
8789
(multiple server instances will run batches on the same resource).
8890
Ideal solution is to route requests based on parameters or URL.
8991
For example some http parameter could be a resource key. You can instruct load balancer

0 commit comments

Comments
 (0)