Code Monkey home page Code Monkey logo

rest-api-concurrent-requests's Introduction

Rest API Demo With Concurrent Requests

API is implemented in NodeJS using express

In this repo, we are going to demonstrate the use of handling multiple requests of the same payload and how we can make sure to avoid duplicate creation of the same resource. Please refer to the Article for the explanation.

How to run

Make sure to have node installed

npm install -g nodemon => Make sure to install this globally.

git clone <repo> from Github

> npm i

> npm run dev

Go to Postman or any http client tool and try to perform the following request multiple times

POST http://localhost:3000/api/payments
{
    "user_id": 1234,
    "order_id": "1234-567-xq34343",
    "status": "SUCCESS",
    "amount": 100,
    "transaction_id": "T23232323"
}

In the database we should have only one payments entry for the above user and rest of them should be ignored. How we manage with ignored calls its left upto us. Either we can log them or store them in another table for auditing.

The concurrent rest calls are also demonstrated in the e2e tests. Check out test/e2e/payments.js file.

Tests

End-to-end (e2e) Tests

Make sure to install mochal globablly npm install mocha -g

Make sure to create a database name and configure the db credentials in config.js

> npm run test:e2e

In the e2e tests test/e2e/payments.js, there is a demonstration of making concurrent calls to the same endpoint.

References

https://60devs.com/synchronization-of-concurrent-http-requests-in-node.html

https://60devs.com/making-better-http-apis.html

https://stackoverflow.com/questions/129329/optimistic-vs-pessimistic-locking

https://blog.couchbase.com/optimistic-or-pessimistic-locking-which-one-should-you-pick/

rest-api-concurrent-requests's People

Contributors

manjufy avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.