‫All right, guys, so I have visual studio code here, the minimum requirements are going to have no
‫JASE'S installed and visual studio code and you can start working on this stuff right there.
‫So I'm going to go ahead and open and brand new folder that's going to create JavaScript Playground.
‫I'm going to call it Sharding.
‫Right and Open.
‫And what I'm going to do is create a file called Indexed, I guess the best file ever.
‫Then let's initialize and because it's a new project.
‫Right.
‫So MBM.
‫And that's why I mean, shut up.
‫I know what I'm doing.
‫Just create a package to Jayson's called Sharding.
‫Sweet.
‫All right.
‫So now remember, guys, we did a tutorial on how to work with postscripts from No Jass using a very
‫beautiful library called PGT.
‫Right.
‫And we're going to use the exact same library to connect to the database as it was initially.
‫And what we're building here is like we're building a Web application that have a post and a get right
‫post will create a new URL shortener, will shorten the oil, give any oil and get will.
‫Given that you will code, the shortest URL will give me that long URL.
‫It's very simple stuff, right.
‫So let's go ahead.
‫And obviously we're going to need express.
‫So again, do require express spun up that express beautiful stuff after get going to do something with
‫this thing.
‫Request response.
‫That's to get command post.
‫That's the post command.
‫That's what we will actually write.
‫Right.
‫Our beautiful beautiful.
‫You are shortener.
‫OK, and here's what we want.
‫We want the database's guys.
‫So the database is called const client.
‫Right.
‫And let's use this structure assignment.
‫I know sometimes it's confusing.
‫Right.
‫This is essentially called the library for the Postgres database called PJI.
‫Right.
‫You can either do it this way or just do the destructo assignment, whatever rocks your boat.
‫That's that's exactly the same.
‫All right.
‫And we have the client.
‫What do we do with a client?
‫We need to spin up multiple.
‫We have to spin up three clients.
‫So here's what we're going to do.
‫So we're going to create a client's right, Jason object and the JSON object.
‫The key will be the port, because I want to play this trick here where the key is the port and the
‫Jason value is the configuration here.
‫So so how do we do this as a five, four, three, two?
‫That's the first client.
‫And that value is new client.
‫And then you pass in that beautiful configuration.
‫So what do we do there?
‫Then we put the right to say, Mac.
‫Again, guys, I have done through all that stuff you want a detailed story about the Polska self,
‫I'm going to reference that video, but this will be just skimming through this stuff, right?
‫So port obviously, port five, four, three, two.
‫And what else?
‫User as well as to what is also Chris and database is also Postgres.
‫I'm just lazy guys.
‫Just use the same.
‫I have to do that thing right.
‫And then that's one client.
‫We need other two clients.
‫Right.
‫The client running on Port three three and the client running on four.
‫How about that, guys?
‫I think we're good here so that a bunch of clients.
‫Right.
‫Still, we didn't connect them.
‫How do we connect?
‫Right.
‫So we need a function called connect here.
‫So that's because I'm going to use the async away.
‫Tears are going to do a function called conduct function function if I can spell connect.
‫And what we're going to do essentially is just call this function immediately and then connect those
‫guys.
‫Right.
‫So we can do like fancy loop or just the old school way where just like literally five, four, three,
‫two, dot connect, connect those TCP connection, establish those beautiful deep connections.
‫All right.
‫I try catch would be nice here but I'm trying to go through the video real quick so that will actually
‫give us the kinds of connections.
‫Beautiful stuff.
‫What's next guys we need.
‫The next thing is let's start with the writing aspect of it.
‫Right.
‫So writing a new row.
‫Right.
‫So I'm going to write to the database again.
‫So how do we write?
‫Right.
‫So posting let's assume I'm going to get the all of that query parameter.
‫How are we going to.
‫I made a video about the query parameters versus resources for you.
‫Interested that.
‫Go watch that video.
‫I'm going to sit here, but are you going to request dot query parameter Dorte URL.
‫So just give that oil and once you get that you URL.
‫Here's the thing.
‫We need to consistently hash this thing, hash this to get a port, get that port.
‫So how do we do this.
‫This is the consistent hash that we talked about guys.
‫So let's do that.
‫So there is a library called Const Consists Tent.
‫Hash, and I think it's called literally consistent hash.
‫I think it's called that, right?
‫So it's called consistent dash hash.
‫So that's what it's called.
‫Right.
‫And then when you do that, what you essentially want to do is create a hash ring that the one we just
‫talked about.
‫Right.
‫So it's like a hash tag of how many nodes in this case.
‫We have three nodes.
‫It's really easy stuff.
‫So I'm going to create a hash ring.
‫Each are equal new consistent hash.
‫Right.
‫So we're going to create a hash ring.
‫Right.
‫And then we start adding our nodes.
‫Right.
‫But we're not going to get Intellisense, I think.
‫Yeah, that's what I thought.
‫So let's go ahead and just install all that stuff, because we have done a lot of stuff and we have
‫never installed any of that stuff.
‫Right.
‫So let's go ahead to NPM and install Xpress and we're going to install BGI.
‫We can install consistent dash hash.
‫Let's install all that stuff.
‫We have installed all of that stuff.
‫Right.
‫We're going to also need crypto, but we'll use it when we really need it.
‫OK, so a jawdat.
‫Yeah, you go.
‫There is a Intellisense edge ordered at OK.
‫And what are we adding here?
‫We're adding essentially these buppies.
‫We have three nodes to be consistently hash and and so easy to use.
‫So once we add all that stuff, here's the thing right for now.
‫I'm going to do a post, right, and if you give me the you are all right, I'm going to hash that your
‫URL and then get a result out of it.
‫Right.
‫And then based on that result, take the first five.
‫Correct.
‫And that becomes my essentially my code.
‫Right.
‫So let's go go ahead and actually do and still might it might as well just do a crypto as well.
‫Right.
‫Come on.
‫Crypto will require crypto.
‫All right.
‫We're going to use crypto to do the hashing guys that we talked about.
‫So what we want to do essentially is take that big URL so the oil could be, I don't know, developed
‫Wikipedia, dot com slash, sharding, like something like that.
‫Right.
‫And I want to take that string and make it into a beautiful hash base64.
‫So how do we do that?
‫So crypto dot, I think could create hash.
‫Right.
‫And then you have to specify the functions called QA Qaa 256, because that's what the hash I'm interested
‫in.
‫And you want to update that string of data you have.
‫So generate the hash and then and then you want to convert it to base64, which is called the I think
‫digest.
‫Right.
‫And then called base sixty four.
‫That's what do you do with that.
‫Gives you a beautiful hash guys.
‫That's just a string of hash and that hash is a little bit long.
‫So let's go ahead and actually run this thing guys so you can see.
‫So let's go ahead.
‫And just this response does send back.
‫I don't know, let's just send back the same information.
‫So the hash we got and so far we don't have any other information.
‫Right.
‫That's it.
‫So let's just go ahead and run and make sure everything is running right.
‫So app we have the app get.
‫We forgot to listen.
‫Let's go app dot listen at eighty one.
‫Right.
‫I want to listen to Paul.
‫Eighty eighty one.
‫That is just a number.
‫So let's just do it number and then do console total listing to eighty, eighty one.
‫All right.
‫So let's just go ahead and debug, make sure we don't have any errors then we're going to get an error
‫because we didn't install crypto.
‫Right.
‫We didn't install crypto.
‫Did we go going to install crypto test in case npm install crypto.
‫That's right, and again.
‫And we're listening to Port Eighty-One, so we want to do a post request to this puppy, right?
‫Let's see what will happen now.
‫OK.
‫Remember, guys, what we're going to do with it.
‫Give it give me a call.
‫And Fumero, I'm going to hash that thing and get a big hash string.
‫I'm going to take only the five characters again.
‫Guys, this is not the perfect implementation of your URL shortener that are way better guys than me
‫doing an actual design.
‫Thought to our one hour video just how to build a proper hash function and to do your elshaug.
‫And this is just to give you an idea of sharding.
‫Right.
‫All right.
‫So let's go to the browser.
‫All right, guys.
‫So we go to our favorite console, right?
‫I'm going to use the beautiful Fitch API and just do old school way.
‫It's eighty, eighty one.
‫And I am actually the Oros HGP is Wikipedia, dot coms, sharding, whatever.
‫And then dot.
‫Yeah, we're doing a method.
‫Right.
‫The method is post and I think that's all we need.
‫Right.
‫Dot, then I'm expecting Jason back.
‫Give me Jason baby and console that log.
‫We made a request immediately.
‫We did the breakpoint.
‫We did the sharding.
‫We get the URL and then hash the stuff and we get this beautiful string.
‫Look at that.
‫That's trying.
‫I'm only interested in the first five characters, right.
‫We're going to take the five characters and shove into the database.
‫Right.
‫As a moral code or YOLO idea or whatever we called it.
‫And then that hash will determine where are we going to hit, which server is going to hit.
‫We're going to talk about that now.
‫So we're going to do that research and we're going to obviously get what we're getting, getting an
‫error.
‫For some reason, axes differ from origin.
‫We're not delivering the original DA.
‫All right.
‫Sure.
‫Let's just do a localhost.
‫Eighty, eighty one happier now, OK?
‫Or in the same location.
‫You're happy now?
‫You happy now?
‫I think you're happy.
‫All right.
‫Good, good, good.
‫All right.
‫So it's just like this.
‫This is a course thing.
‫We talked about my daughter.
‫We talk about a lot of stuff in this channel.
‫Guys, how are you still here?
‫There's so much stuff, right?
‫So there's a lot of tech.
‫Right.
‫And just exploring all this stuff becomes you slightly become a soft bed ourselves and enjoy as you
‫hit these things and start understanding your course.
‫Beautiful core stuff, fun.
‫I could have had something in the server that allows other people to access this, but I don't I just
‫don't feel like it.
‫All right.
‫So we have the hash back.
‫This is no I to go, though.
‫I want you to code.
‫What is your code, guys, that your code is essentially you are.
‫I'd equal hash substory OMG 025.
‫That's why I'm just in the first five five characters.
‫Not sure computer science stuff.
‫Is this going to give me five characters or six.
‫Right.
‫This struggle.
‫Man I always struggle with these.
‫That's and you would think five, fifteen years experience in computer science is still get this wrong
‫man because every implementation good that sometimes like the strings get it or start from one side
‫from zero.
‫Everyone implements their own stuff man.
‫Right.
‫So let's just do hash again.
‫What do we do.
‫We get it right.
‫Do we get it right.
‫Yes.
‫Looks like five characters to me.
‫Right.
‫Zero.
‫One, two, three, four or five.
‫All right, so you would think that is going to give you six, correct?
‫Because zero to five is actually never mind.
‫All right.
‫So now we get that.
‫There you are, guys.
‫Look at that.
‫Give you that.
‫And if I do it again, let's just remove the brake lines.
‫Just getting annoying now at this point.
‫Getting annoying, getting annoyed, remove it.
‫Right.
‫And every time we get that, we're going to get the same key.
‫Right.
‫But let's say change that to something else and we get a completely different key.
‫Right.
‫That's not what we want.
‫We have the key.
‫Now, I can write to the debates were ready almost.
‫We're almost ready.
‫It's almost almost ready.
‫Let's just throw in there you are actually earlier.
‫How about that game?
‫Just throwing a real shot.
‫And then I'm going to throw in the server because I don't know which server I'm going to connect to.
‫That's the tricky part, right?
‫Right.
‫So how do I know which server to connect to?
‫Very easy.
‫E back to the consistent hashing.
‫Right.
‫Which are the hash rank.
‫Remember we added three notes to the hash rank.
‫Right.
‫All we need to do is ask the node, hey, hash rank, I have this input, which is called your ID,
‫give me one of the three nodes and it will do its magic.
‫It will I do a module, whatever the implementation is, and it's going to give you a server.
‫That's the beauty of this thing.
‫Let's do it.
‫So let's see what what server do we get.
‫Why do this let's clean this up and then do we look at that we got four sharding when we got silver,
‫five, four, three, two, and we'll Diski, right?
‫That's funny.
‫It's actually says key.
‫Oh, my God.
‫That's completely a coincidence.
‫Let's say sharding two, we still hang in the same server.
‫Sharding three, we go.
‫We had another server, five, four, three, four.
‫So completely almost random write of a random.
‫Know, I want to hit, please give me the server three three, we didn't hit that server yet.
‫Dot Beatch behind.
‫Five, four, three.
‫To admit I want five, four, three, three.
‫Can you give me five, four, three, three.
‫A random dude that gives me a five, four, three, two.
‫OK, that's what I'm talking about.
‫Just as you start running, this will almost balance that stuff in a consistent manner.
‫And every time you do the same exact way, you're going to hit the same exact server.
‫OK, we have all the pieces, guys, all the pieces here.
‫So how about we actually write this stuff to the database?
‫Right.
‫Let's write it.
‫How do you write stuff to the database?
‫Very simple.
‫We we need to get the client right.
‫The clients are in this bunch.
‫Right.
‫Clients sub.
‫Guess what, what's the sub here.
‫I want this up server.
‫Right.
‫Because this is the key.
‫Right.
‫Because now I have the server, which is a string of port that will give me back the object and the
‫object.
‫When I get that thing, I want to query that.
‫Right.
‫Why is it not showing me Intellisense?
‫I think it's unaware that is actually an object.
‫Well, I'm pretty sure that should work.
‫So select start that idea.
‫Don't ever do a star in your database lets you know what you're doing.
‫Right.
‫So it's called your underscore table.
‫I'd like to make things big.
‫That's what she said.
‫Select star from the oil and gas go table.
‫Yeah.
‫I don't know what sequel's all of that has to be caps lock.
‫All right.
‫It's just something we learned back when I do when I used to do Oracle stuff back in 2005.
‫Thousand four actually.
‫Yeah.
‫First job.
‫All right.
‫So where are we to where what?
‫Why are we selecting that's not what we're inserting insert into your RL table, right, inserting what
‫ID is going to be automatically generated the oral and the oral I.D. and then we do the values and then
‫the fancy stuff, one and two.
‫And the one is equal oral and the other one is the URL already.
‫OK.
‫Sounds good, right?
‫Let's look ahead and talk to me right now, I will insert, but you have to await that, right?
‫And since we're awaiting that function that we're in, better be async, right?
‫That's OK.
‫Now, we have to send that push always to recording.
‫Good.
‫Right.
‫So now we're sending that back and then we are sure that, hey, we got it.
‫OK.
‫I mean, I kind of had a try catch and be like a little bit more fancy, but yeah.
‫Let's just make sure that we actually made it right.
‫Otherwise we're going to get an error.
‫So let's do it, guys.
‫Let's do it.
‫Do it.
‫Refresh right.
‫And post.
‫Wow, that's bull.
‫Did that work.
‫I'll buy that.
‫What server is that.
‫Five, four, three, three.
‫So how do we go.
‫Which is five, four, five, four, three, three.
‫I -- I should have just put the right number here.
‫I don't know which was four four three three.
‫I think that's the one.
‫OK, charge to his four for safety.
‫OK, public and we got tables.
‫Do we have a roll guys.
‫Do we have a row.
‫Do we ever.
‫Oh yeah.
‫Sorry.
‫What the heck was that.
‫Printing PDF.
‫Why Jesus.
‫OK, Celle, look at that.
‫Beautiful stuff guys.
‫Wow.
‫Well how about we do it.
‫Another one.
‫OK, another one.
‫Did you got it right.
‫And it's same server.
‫OK, sure.
‫It's easier to me.
‫Right.
‫Would like that.
‫I'm not the one and that's the key.
‫Right, let's do it on Google dot com doesn't have to be Wikipedia, right?
‫Ali?
‫Refresh, which that we had another server, let's go to that server, five short three guys, I'm going
‫to I'm going to reference that could be a blow so you don't have to, like, pause and do all that stuff.
‫Right.
‫Goofy stuff.
‫You just you just go to the GitHub page.
‫Look at that, guys.
‫So we're consistently hashing the F out of this stuff, man.
‫Consistent, Beebs.
‫That's what we're talking about consistently.
‫All right.
‫About that.
‫That's just throwing some of that Yarl's equal.
‫How do I do you oil's right, let's just do it
‫I less than 100 or less, and then it is like you URL's to push this Google dot com slash in India because
‫why not?
‫Let's go there.
‫I search for Q equal test that I have to do the text.
‫Can you read guys.
‫You can read this book.
‫I'm going to zoom in a little bit.
‫Right.
‫Sorry about that.
‫So we added how many URLs we have a lot of your old guys.
‫So many URLs.
‫Oh my God.
‫Let's go and let's do it right this time, guys, for each you you doing it on me.
‫I like it when you do No.
‫One.
‫You believe God is a woman, right?
‫Well, all right, I think that's right.
‫Right, though, did I get it right?
‫I really doubt it, but sure, let's do it.
‫Oh, Mama.
‫I see.
‫Look at that.
‫Look at all that stuff.
‫OK, so we had like a bunch of servers.
‫We had five, four, three, three, five for all that jazz and Shailesh.
‫Oh, my God.
‫Look at all beautiful stuff, man.
‫All right.
‫We know the stuff is working.
‫Guys were distributed consistently.
‫All right.
‫How about that?