alex-db-server 0.1.0

AlexDB server.
Documentation
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# alex-db-server

Database server application offering REST API for communication.

## Running in the Development Mode

```sh
cd alex-db-server/
cp .env .env.local
# Edit the '.env.local' file now using your preferred editor.
set -a
source .env.local
set +a
RUST_LOG=alex-db-server=trace,info,debug,tokio=trace,runtime=trace cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 1.30s
     Running `/home/michal/projects/alex-db/target/debug/alex-db-server`
2023-02-09T14:21:02.746448Z  INFO alex_db_server::config: data_dir = Some("/home/michal/data/")
2023-02-09T14:21:02.746504Z  INFO alex_db_server::config: enable_security_api_keys = true
2023-02-09T14:21:02.746530Z  INFO alex_db_server::config: port = 10240
2023-02-09T14:21:02.746555Z  INFO alex_db_server::config: save_triggered_after_ms = 27000
2023-02-09T14:21:02.746575Z  INFO alex_db_server::config: save_triggered_by_threshold = 4
2023-02-09T14:21:02.746599Z  INFO alex_db_server::config: sleep_time_between_gc_ms = 900
2023-02-09T14:21:02.746622Z  INFO alex_db_server::config: sleep_time_between_saves_ms = 9000
2023-02-09T14:21:02.749852Z  INFO alex_db_server::app: initial api key created: Some(63545360-301e-482f-93fc-84e6d11d8aee)
2023-02-09T14:21:02.760592Z  INFO alex_db_server: listening on 0.0.0.0:10240
```

## Example Requests

Access the API documentation by navigating to http://localhost:10240/swagger-ui/ in your web browser.

Please substitute '63545360-301e-482f-93fc-84e6d11d8aee' with your 'initial API key' in this instance.

### Database stats

Execute the command

```sh
curl --location --request GET 'http://localhost:10240/stats' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee'
```

and you will receive the result

```sh
{"reads":0,"requests":0,"saved_at":"2023-02-09T14:26:00.865051741Z","saved_writes":0,"writes":0}
```

### Create

Execute the command

```sh
curl --location --request POST 'http://localhost:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
    "key": "test1-key",
    "value": "test1-value"
}'
```

and you will receive the result

```sh
{"key":"test1-key","value":"test1-value"}
```

### List

Execute the commands

```sh
curl --location --request POST 'http://localhost:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
    "key": "test2-key",
    "value": true
}'

curl --location --request GET 'http://localhost:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee'
```

and you will receive the result

```sh
[{"key":"test1-key","value":"test1-value"},{"key":"test2-key","value":true}]
```

There are additional parameters that you can use for sorting and paginating.

- direction:
  - asc
  - desc
- sort:
  - created_at
  - delete_at
  - key
  - updated_at
- page - page number
- limit - limit of items per page

Execute the command

```sh
curl --location --request GET 'http://localhost:10240/values?sort=created_at&direction=asc&page=1&limit=1' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee'
```

and you will receive the result

```sh
[{"key":"test1-key","value":"test1-value"}]
```

### Read

Execute the commands

```sh
curl --location --request POST 'http://localhost:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
    "key": "test3-key",
    "value": 10
}'

curl --location --request GET 'http://localhost:10240/values/test3-key' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee'
```

and you will receive the result

```sh
{"key":"test3-key","value":10}
```

### Update

Execute the commands

```sh
curl --location --request POST 'http://localhost:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
    "key": "test4-key",
    "value": ["test4-value"],
    "ttl": 120
}'

curl --location --request PUT 'http://localhost:10240/values/test4-key' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
    "value": "test4-value-updated",
    "ttl": 200
}'
```

and you will receive the result

```sh
{"key":"test4-key","value":"test4-value-updated"}
```

### Delete

Execute the commands

```sh
curl --location --request POST 'http://localhost:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
    "key": "test5-key",
    "value": ["test5-value", true, 10]
}'

curl --location --request DELETE 'http://localhost:10240/values/test5-key' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee'
```

### Append

Execute the commands

```sh
curl --location --request POST 'http://localhost:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
    "key": "test6-key",
    "value": ["test6-value"]
}'

curl --location --request PUT 'http://localhost:10240/values/test6-key/append' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
    "append": ["test6-value-appended"]
}'
```

and you will receive the result

```sh
{"key":"test6-key","value":["test6-value","test6-value-appended"]}
```

### Prepend

Execute the commands

```sh
curl --location --request POST 'http://localhost:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
    "key": "test7-key",
    "value": ["test7-value"]
}'

curl --location --request PUT 'http://localhost:10240/values/test7-key/prepend' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
    "prepend": ["test7-value-prepended"]
}'
```

and you will receive the result

```sh
{"key":"test7-key","value":["test7-value-prepended","test7-value"]}
```

### Increment

Execute the commands

```sh
curl --location --request POST 'http://localhost:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
    "key": "test8-key",
    "value": 1000
}'

curl --location --request PUT 'http://localhost:10240/values/test8-key/increment' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{}'

curl --location --request PUT 'http://localhost:10240/values/test8-key/increment' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
    "increment": 10
}'
```

and you will receive the result

```sh
{"key":"test8-key","value":1011}
```

### Decrement

Execute the commands

```sh
curl --location --request POST 'http://localhost:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
    "key": "test9-key",
    "value": 5000
}'

curl --location --request PUT 'http://localhost:10240/values/test9-key/decrement' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{}'

curl --location --request PUT 'http://localhost:10240/values/test9-key/decrement' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
    "decrement": 10
}'
```

and you will receive the result

```sh
{"key":"test9-key","value":4989}
```

### Pop front

Execute the commands

```sh
curl --location --request POST 'http://localhost:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
    "key": "test10-key",
    "value": ["test10-value1", "test10-value2", "test10-value3", true, false, true, 10, 11, 12]
}'

curl --location --request PUT 'http://localhost:10240/values/test10-key/pop-front' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{}'

curl --location --request PUT 'http://localhost:10240/values/test10-key/pop-front' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
    "pop_front": 3
}'
```

and you will receive the result

```sh
["test10-value2","test10-value3",true]
```

### Pop back

Execute the commands

```sh
curl --location --request POST 'http://localhost:10240/values' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
    "key": "test11-key",
    "value": ["test11-value1", "test11-value2", "test11-value3", true, false, true, 10, 11, 12, ["test11-a-value1", "test11-a-value2", "test11-a-value3"], ["test11-b-value1", "test11-b-value2", "test11-b-value3"], ["test11-c-value1", "test11-c-value2", "test11-c-value3"]]
}'

curl --location --request PUT 'http://localhost:10240/values/test11-key/pop-back' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{}'

curl --location --request PUT 'http://localhost:10240/values/test11-key/pop-back' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
--data-raw '{
    "pop_back": 3
}'
```

and you will receive the result

```sh
[["test11-b-value1","test11-b-value2","test11-b-value3"],["test11-a-value1","test11-a-value2","test11-a-value3"],12]
```

## Performance

Presently, the server displays satisfactory performance on its API endpoints.

Execute the command

```sh
curl --location --request GET 'http://localhost:10240/values/test3-key' \
--header 'Content-Type: application/json' \
--header 'X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee' \
-w ' Total: %{time_total}s\n'
```

and you will receive the result

```sh
{"key":"test3-key","value":10} Total: 0.001278s
```

and

```sh
{"key":"test3-key","value":10} Total: 0.000617s
```

when you run project in release mode

```sh
cargo run --release
```

The response time for the API endpoint has been found to be slightly above 1ms in the development mode and below 1ms in the production mode, based on our evaluation on an Ubuntu 22.04 system hosted on VirtualBox.

Execute the command

```sh
ab -c 16 -n 100000 -H "X-Auth-Token: 63545360-301e-482f-93fc-84e6d11d8aee" http://localhost:10240/values/test3-key
```

and you will receive the result

```sh
This is ApacheBench, Version 2.3 <$Revision: 1879490 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 10000 requests
Completed 20000 requests
Completed 30000 requests
Completed 40000 requests
Completed 50000 requests
Completed 60000 requests
Completed 70000 requests
Completed 80000 requests
Completed 90000 requests
Completed 100000 requests
Finished 100000 requests


Server Software:
Server Hostname:        localhost
Server Port:            10240

Document Path:          /values/test3-key
Document Length:        30 bytes

Concurrency Level:      16
Time taken for tests:   12.560 seconds
Complete requests:      100000
Failed requests:        0
Total transferred:      13800000 bytes
HTML transferred:       3000000 bytes
Requests per second:    7961.69 [#/sec] (mean)
Time per request:       2.010 [ms] (mean)
Time per request:       0.126 [ms] (mean, across all concurrent requests)
Transfer rate:          1072.96 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.1      0      14
Processing:     0    2   0.6      2      17
Waiting:        0    2   0.6      2      17
Total:          0    2   0.6      2      17

Percentage of the requests served within a certain time (ms)
  50%      2
  66%      2
  75%      2
  80%      2
  90%      2
  95%      2
  98%      2
  99%      3
 100%     17 (longest request)
```

This indicates that the server is capable of handling over 7,900 requests per second on the testing machine.

The performance of the internal database has not been measured yet. The numbers above reflect the performance when accessing the database through the HTTP protocol.

Please note that there is an overhead associated with the HTTP server. The server must process the HTTP request, query the database, serialize the data, and send the response.