List functional requirements for the system (Ask the chat bot for hints if stuck.)...
Able to generate unique IDs in a distributed environment
-
List non-functional requirements for the system...
Define what APIs are expected from the system...
GET /generateId
returns a json payload with only one attribute ID which is a 64 bit ID. We will pass the Oauth2 bearer token in the request and the response codes will be the the ones that are used in REST style APIS
200 for success respones
4XX for client side errors
5XX for server side errors
Defining the system data model early on will clarify how data will flow among different components of the system. Also you could draw an ER diagram using the diagramming tool to enhance your design...
DB design will be very simple . It will have a single table and that table will have entried
Though DB is as such not required for this design but it is only needed in case node fails and we want to know what is the last generated sequence
flowchart TD A[Client] -->|HTTP Request| B[Load Balancer] B -->|Forward Request| C{Nodes} C -->|Generate Unique ID| D[Node 1] C -->|Generate Unique ID| E[Node 2] C -->|Generate Unique ID| F[Node N] D -->|Response ID| B E -->|Response ID| B F -->|Response ID| B B -->|ID Response| A
Explain how the request flows from end to end in your high level design. Also you could draw a sequence diagram using the diagramming tool to enhance your explanation...
Dig deeper into 2-3 components and explain in detail how they work. For example, how well does each component scale? Any relevant algorithm or data structure you like to use for a component? Also you could draw a diagram using the diagramming tool to enhance your design...
So here we are thnking of using twitter snowflake style ID generator which will have 64 bits .1 bit could be for the sign. Next 41 bits could be for the timestamps meaning close to around 69 years. 10 bits for node id and remaining 12 bits for sequence .So in a millisecond one node can generate 2^12 around 4000 unique IDs which are considerably good
Explain any trade offs you have made and why you made certain tech choices...
We could have used UUID which is universally unique and there is no need to worry about collision but it is 128 bit so considering the storage if might not be a good solution
We could use auto-increment db sequence but that would become a single point of failure and we shard then there will be chances of collision
So we have gone for twitter snowflake type id generator
Try to discuss as many failure scenarios/bottlenecks as possible.
If we need a node to generate more number of Ids than the current range of 4000 then we can consider reducing the bits assigned to timestamp and giving it to the sequence portion .
What are some future improvements you would make? How would you mitigate the failure scenario(s) you described above?