Sequence Numbering in Serverless via API Gateway

Sheen Brisals
Jun 4 · 8 min read
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


LEGO Engineering

LEGO Engineering

Sheen Brisals

Written by

Engineer. Architect. Leader. Speaker. @sheenbrisals

LEGO Engineering

LEGO Engineering