Certain applications encounter challenges in efficiently allocating entities, such as events, to designated stores or nodes. Consider the scenario where a request needs to be directed to a server from a pool of available servers. The crucial question arises: which server should be selected, and what are the underlying reasons behind the choice?
I have previously addressed these inquiries in a project that can be accessed at this link: https://github.com/kounkou/hasherprovider. Within this project, I offer an educational implementation of the Hasherprovider in a cloud environment.
The objective is to enhance understanding of the allocation process and its significance within such applications.
The following is the architectural overview of the application. Please note that this architecture is intended solely for educational purposes and does not encompass all the best practices typically employed when designing such a project.
To install the entire stack locally, you will need to have :
- Docker
- Localstack
bash install.shAt the root of the project there is a send-requests.sh script that sends the following basic requests for testing purposes :
bash send-requests.sh blfglg7v2p # replace with corresponding API Gateway endpoint IDThis command sends a valid payload with all required fields:
curl -X POST \
-H "Content-Type: application/json" "https://$1.execute-api.localhost.localstack.cloud:4566/prod" \
-d '{"nodes":["node1","node2"],"hashKeys":["node1"],"hashingType":"CONSISTENT_HASHING", "replicas":3}' | jqResponse:
{
"statusCode": 200,
"body": "[\"node1\"]"
}curl -X POST \
-H "Content-Type: application/json" "https://$1.execute-api.localhost.localstack.cloud:4566/prod" \
-d '{"nodes":[],"hashKeys":["key1"],"hashingType":"CONSISTENT_HASHING", "replicas":3}' | jqExpected Response:
{
"statusCode": 400,
"body": "node list is empty"
}curl -X POST \
-H "Content-Type: application/json" "https://$1.execute-api.localhost.localstack.cloud:4566/prod" \
-d '{"nodes":["node1"],"hashKeys":[],"hashingType":"CONSISTENT_HASHING", "replicas":3}' | jqExpected Response:
{
"statusCode": 400,
"body": "hash keys list is empty"
}curl -X POST \
-H "Content-Type: application/json" "https://$1.execute-api.localhost.localstack.cloud:4566/prod" \
-d '{"nodes":["node1"],"hashKeys":["key1"],"hashingType":"UNKNOWN", "replicas":3}' | jqExpected Response:
{
"statusCode": 400,
"body": "unknown hashing type"
}curl -X POST \
-H "Content-Type: application/json" "https://$1.execute-api.localhost.localstack.cloud:4566/prod" \
-d '{"nodes":["node1"],"hashKeys":["key1"],"hashingType":"CONSISTENT_HASHING", "replicas":-1}' | jqExpected Response:
{
"statusCode": 400,
"body": "replicas number should be positive or 0"
}To perform test for the stack please run the following command
$ npm testSample result
> cloud-hasher@0.1.0 test
> jest
PASS test/cloud-hasher.test.ts (9.265 s)
CloudHasherStack
✓ SQS Queue Created (219 ms)
✓ Lambda Created (114 ms)
✓ APIGateway Created (112 ms)
Test Suites: 1 passed, 1 total
Tests: 3 passed, 3 total
Snapshots: 0 total
Time: 9.481 s
Ran all test suites.