MemCP for Microservices

From MemCP
Jump to navigation Jump to search

MemCP is a neat database to implement microservices.


Here's a code example for a simple key value store:

/* microservice democase: a simple key value store with prepared statements */
(import "../lib/sql-parser.scm")
(import "../lib/queryplan.scm")

/* initialize database and prepare sql statements */
(createdatabase "keyvalue" true)
(eval (parse_sql "keyvalue" "CREATE TABLE IF NOT EXISTS kv(key TEXT, value TEXT, UNIQUE KEY PRIMARY(key))"))

(set item_get (parse_sql "keyvalue" "SELECT value FROM kv WHERE key = @key"))
(set item_set (parse_sql "keyvalue" "INSERT INTO kv(key, value) VALUES (@key, @value) ON DUPLICATE KEY UPDATE value = @value"))
/*(set item_list (parse_sql "keyvalue" "SELECT key, value FROM kv"))*/


(define http_handler (begin
        (lambda (req res) (begin
                (set session (newsession))
                (session "key" (req "path"))
                (if (equal? (req "method") "GET") (begin
                        /* GET = load */
                        (set resultrow (lambda (resultset) ((res "print") (resultset "value"))))
                        (eval item_get)
                ) (begin
                        /* PUT / POST: store */
                        (session "value" ((req "body")))
                        (eval item_set)
                        ((res "print") "ok")
                ))
        ))
))

(set port 1266)
(serve port (lambda (req res) (http_handler req res)))

The example can be found in apps/keyvalue.scm and can be run with:

./memcp apps/keyvalue.scm

Here's a benchmark of a mini-demo on a AMD Ryzen 9 7900X3D 12-Core Processor using abbenchmark tool:

Server Software:        
Server Hostname:        localhost
Server Port:            1266

Document Path:          /hi
Document Length:        5 bytes

Concurrency Level:      10
Time taken for tests:   42.083 seconds
Complete requests:      1000000
Failed requests:        0
Total transferred:      106000000 bytes
HTML transferred:       5000000 bytes
Requests per second:    23762.78 [#/sec] (mean)
Time per request:       0.421 [ms] (mean)
Time per request:       0.042 [ms] (mean, across all concurrent requests)
Transfer rate:          2459.82 [Kbytes/sec] received

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

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

It is noteworthy that abeven with a concurrency level of 10 took 100% CPU load on a single core while memcp took a cool portion of 20% of all other cores. So the benchmark tells more about abs performance than that of MemCP.