Sequence Numbering in Serverless via API Gateway

Photo by Kolleen Gladden on Unsplash

Use of sequence numbers

Examples:Order numbers: 200 000 230, 200 000 231, 200 000 232, 200 000 234, …Candidate IDs: 1010, 1011, 1012, 1013, 1014, …User visits to a site: 100 123, 100 124, 100 125, …

RDBMS way of generating sequence numbers

CREATE SEQUENCE D_REQUEST_CATALOG_SINCREMENT BY 1MAXVALUE 900000000MINVALUE 600000000CACHE 10

DynamoDB to the rescue

Atomic counters

API for generating sequence numbers

Step 1: Setup a sequence number table in DynamoDB

{“id”: “100”,“sequence_number”: 250000}

Step 2: Create an API

Step 3: Setup the execution role

Step 4: Configure request mapping template

{  “TableName”: “order-sequence-number”,  “Key”: {    “id”: {      “S”: “100”    }  },  “ExpressionAttributeValues”: {    “:val”: {      “N”: “1”    }  },  “UpdateExpression”: “SET sequence_number = sequence_number + :val”,  “ReturnValues”: “UPDATED_NEW”}

Step 5: Testing the API

Step 6: Transform the response

{“sequence_number”: “$input.path(‘$.Attributes.sequence_number.N’)”}
{“sequence_number”: “250006”}

Enhance it yourself

Conclusion

Engineer. Architect. Leader. Writer. Speaker. At The LEGO Group. AWS Serverless Hero.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store